gpt4 book ai didi

multithreading - Erlang计时器:sleep(1000) cause dead in thread

转载 作者:行者123 更新时间:2023-12-03 13:07:46 24 4
gpt4 key购买 nike

我用Erlang编写了一个代码,并使用了timer.sleep(),但是当我编写timer.sleep(100)时,它是不错的,并且可以正常工作,但是我需要更多的时间来停止线程以及增加时间并运行计时器。使用spawn调用的函数中的sleep(100),然后代码不起作用。

在代码中,第二个io不在控制台中打印,因此该线程在时间time:sleep(1000)中死了。

request_handler ( Name, Amount, Banks ) ->
io:format("~p ~p ~p ~n", [Name, Amount, Banks]),
timer:sleep(1000),
io:format("~p ~p ~p ~n", [Name, Amount, Banks]),
if Amount < 50 ->
Ask_amount = rand:uniform(Amount);
true ->
Ask_amount = rand:uniform(50)
end,
Bank = lists:nth(rand:uniform(length(Banks)), Banks),
Pid = whereis(Bank),
Pid ! {self(), Ask_amount},
receive
{accept, Ask_amount} ->
request_handler(Name, (Amount - Ask_amount), Banks);
{reject} ->
request_handler(Name, Amount, lists:filter(fun (Elem) -> not lists:member(Elem, [Bank]) end, Banks ))
end.


任何人都知道我怎样才能使erlang中的线程休眠1000毫秒?

最佳答案

In the code the second io does not print in console, so the thread dead in the line time:sleep(1000).



首先,在erlang中,我们称它们为进程,而不是线程。如果正在运行 handle_request()的进程在 sleep 时死亡,即在第二个format语句显示任何输出之前,则应该在某处看到错误消息。

您没有提供足够的信息来帮助您。您的问题本质上是这样的:

I have the following function running in a process:

go() ->
io:format("hello"),
timer:sleep(1000),
io:format("goodbye").

I don't see the second output. Why is my process dying during the sleep?



答案是:因为某些东西杀死了您的过程。

这是一个示例,显示了如何使 request_handler()进程休眠1秒:

handlers.erl:
-module(handlers).
-compile([export_all]).

request_handler ( Name, Amount, Banks ) ->
io:format("Format1: ~p ~p ~p ~n", [Name, Amount, Banks]),
timer:sleep(1000),
io:format("Format2: ~p ~p ~p ~n", [Name, Amount, Banks]),

% This is how you write an if statement when you want to assign
% the result to a variable:
AskAmount = if
Amount < 50 -> rand:uniform(Amount);
true -> rand:uniform(50)
end,
Bank = lists:nth(rand:uniform(length(Banks)), Banks),
Pid = whereis(Bank),
Pid ! {self(), AskAmount},

receive
{accept, AskAmount} ->
request_handler(Name, (Amount - AskAmount), Banks);
{reject} ->
request_handler(
Name,
Amount,
lists:filter(fun (Elem) -> not lists:member(Elem, [Bank]) end,
Banks
)
)
end.

上面的if语句编写方式将摆脱警告:

Warning: variable 'Ask_amount' exported from 'if



bank.erl:
-module(bank).
-compile(export_all).

init(Name) ->
register(Name, spawn(?MODULE, loop, [ok])).

loop(State) ->
receive
{From, Amount} ->
From ! {accept, Amount},
loop(State)
end.

create_banks(Names) ->
lists:foreach(
fun(Name) -> init(Name) end,
Names %=> [bank1, bank2, bank3]
).

my.erl:
-module(my).
-compile([export_all]).

go() ->
BankNames = [bank1, bank2, bank3],
bank:create_banks(BankNames),
handlers:request_handler("Hello", 500, BankNames).

在 shell 中:
~/erlang_programs$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)

1> c(bank).
bank.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,bank}

2> c(handlers).
handlers.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,handlers}

3> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}

4> my:go().
Format1: "Hello" 500 [bank1,bank2,bank3]
Format2: "Hello" 500 [bank1,bank2,bank3]
Format1: "Hello" 499 [bank1,bank2,bank3]
Format2: "Hello" 499 [bank1,bank2,bank3]
Format1: "Hello" 488 [bank1,bank2,bank3]
Format2: "Hello" 488 [bank1,bank2,bank3]
Format1: "Hello" 468 [bank1,bank2,bank3]
Format2: "Hello" 468 [bank1,bank2,bank3]
Format1: "Hello" 460 [bank1,bank2,bank3]
Format2: "Hello" 460 [bank1,bank2,bank3]
Format1: "Hello" 456 [bank1,bank2,bank3]
Format2: "Hello" 456 [bank1,bank2,bank3]
Format1: "Hello" 419 [bank1,bank2,bank3]
...
...
^C^C

对评论的回复:

I want to run function 'request_handler' more than one time concurrently. ...but when I run the request handler:

spawn(handlers, request_handler, [hello, 450, Banks])

the second io not working.



这个对我有用:
-module(my).
-compile([export_all]).

go() ->
BankNames = [bank1, bank2, bank3],
bank:create_banks(BankNames),
lists:foreach(
fun(N) ->
spawn(handlers, request_handler, [N, 500, BankNames])
end,
lists:seq(1, 5) %=> [1, 2, 3, 4, 5]
).

在 shell 中:
~/erlang_programs$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)

1> c(bank).
bank.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,bank}

2> c(handlers).
handlers.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,handlers}

3> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}

4> my:go().
process(<0.84.0>): Format1: 1 500 [bank1,bank2,bank3]
process(<0.85.0>): Format1: 2 500 [bank1,bank2,bank3]
process(<0.86.0>): Format1: 3 500 [bank1,bank2,bank3]
process(<0.87.0>): Format1: 4 500 [bank1,bank2,bank3]
process(<0.88.0>): Format1: 5 500 [bank1,bank2,bank3]
ok
process(<0.84.0>): Format2: 1 500 [bank1,bank2,bank3]
process(<0.85.0>): Format2: 2 500 [bank1,bank2,bank3]
process(<0.86.0>): Format2: 3 500 [bank1,bank2,bank3]
process(<0.87.0>): Format2: 4 500 [bank1,bank2,bank3]
process(<0.88.0>): Format2: 5 500 [bank1,bank2,bank3]
process(<0.84.0>): Format1: 1 467 [bank1,bank2,bank3]
process(<0.87.0>): Format1: 4 471 [bank1,bank2,bank3]
process(<0.85.0>): Format1: 2 465 [bank1,bank2,bank3]
process(<0.86.0>): Format1: 3 460 [bank1,bank2,bank3]
process(<0.88.0>): Format1: 5 453 [bank1,bank2,bank3]
process(<0.84.0>): Format2: 1 467 [bank1,bank2,bank3]
process(<0.87.0>): Format2: 4 471 [bank1,bank2,bank3]
process(<0.85.0>): Format2: 2 465 [bank1,bank2,bank3]
process(<0.86.0>): Format2: 3 460 [bank1,bank2,bank3]
process(<0.88.0>): Format2: 5 453 [bank1,bank2,bank3]
process(<0.84.0>): Format1: 1 422 [bank1,bank2,bank3]
process(<0.86.0>): Format1: 3 441 [bank1,bank2,bank3]
process(<0.87.0>): Format1: 4 461 [bank1,bank2,bank3]
process(<0.88.0>): Format1: 5 413 [bank1,bank2,bank3]
process(<0.85.0>): Format1: 2 442 [bank1,bank2,bank3]
process(<0.84.0>): Format2: 1 422 [bank1,bank2,bank3]
process(<0.86.0>): Format2: 3 441 [bank1,bank2,bank3]
process(<0.87.0>): Format2: 4 461 [bank1,bank2,bank3]
process(<0.88.0>): Format2: 5 413 [bank1,bank2,bank3]
process(<0.85.0>): Format2: 2 442 [bank1,bank2,bank3]
process(<0.84.0>): Format1: 1 405 [bank1,bank2,bank3]
process(<0.86.0>): Format1: 3 416 [bank1,bank2,bank3]
process(<0.87.0>): Format1: 4 439 [bank1,bank2,bank3]
process(<0.88.0>): Format1: 5 376 [bank1,bank2,bank3]
process(<0.85.0>): Format1: 2 419 [bank1,bank2,bank3]
process(<0.84.0>): Format2: 1 405 [bank1,bank2,bank3]
process(<0.86.0>): Format2: 3 416 [bank1,bank2,bank3]
process(<0.87.0>): Format2: 4 439 [bank1,bank2,bank3]
process(<0.88.0>): Format2: 5 376 [bank1,bank2,bank3]
process(<0.85.0>): Format2: 2 419 [bank1,bank2,bank3]
process(<0.84.0>): Format1: 1 397 [bank1,bank2,bank3]
process(<0.86.0>): Format1: 3 394 [bank1,bank2,bank3]
process(<0.85.0>): Format1: 2 389 [bank1,bank2,bank3]
process(<0.87.0>): Format1: 4 401 [bank1,bank2,bank3]
process(<0.88.0>): Format1: 5 340 [bank1,bank2,bank3]
process(<0.84.0>): Format2: 1 397 [bank1,bank2,bank3]
process(<0.86.0>): Format2: 3 394 [bank1,bank2,bank3]
process(<0.85.0>): Format2: 2 389 [bank1,bank2,bank3]
process(<0.87.0>): Format2: 4 401 [bank1,bank2,bank3]
process(<0.88.0>): Format2: 5 340 [bank1,bank2,bank3]
process(<0.84.0>): Format1: 1 367 [bank1,bank2,bank3]
process(<0.87.0>): Format1: 4 400 [bank1,bank2,bank3]
process(<0.86.0>): Format1: 3 355 [bank1,bank2,bank3]
process(<0.85.0>): Format1: 2 370 [bank1,bank2,bank3]
process(<0.88.0>): Format1: 5 313 [bank1,bank2,bank3]
process(<0.84.0>): Format2: 1 367 [bank1,bank2,bank3]
process(<0.87.0>): Format2: 4 400 [bank1,bank2,bank3]
process(<0.86.0>): Format2: 3 355 [bank1,bank2,bank3]
process(<0.85.0>): Format2: 2 370 [bank1,bank2,bank3]
process(<0.88.0>): Format2: 5 313 [bank1,bank2,bank3]
process(<0.84.0>): Format1: 1 337 [bank1,bank2,bank3]
process(<0.87.0>): Format1: 4 381 [bank1,bank2,bank3]
process(<0.88.0>): Format1: 5 299 [bank1,bank2,bank3]
process(<0.86.0>): Format1: 3 329 [bank1,bank2,bank3]
process(<0.85.0>): Format1: 2 367 [bank1,bank2,bank3]

^C^C

如您所见,两个 io:format()语句都显示其输出。 okmy:go()的返回值,它返回 lists:foreach()返回的所有内容,即 ok

关于multithreading - Erlang计时器:sleep(1000) cause dead in thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56571479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com