Erlang timer:sleep(1000) 导致线程死机

Posted

技术标签:

【中文标题】Erlang timer:sleep(1000) 导致线程死机【英文标题】:Erlang timer:sleep(1000) cause dead in thread 【发布时间】:2019-10-27 12:22:31 【问题描述】:

我在 Erlang 中编写了一个代码并使用 timer.sleep(),但是当我编写 timer.sleep(100) 时它是好的,代码工作,但是我需要更多的时间来停止线程并且当我增加时间和在使用 spawn 调用的函数中运行 timer.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 毫秒?

【问题讨论】:

timer:sleep(1000)! 我怎么能等一秒钟? @7stud timer:sleep(1000)! --> 1,000 毫秒 == 1 秒 【参考方案1】:

在代码中第二个 io 不在控制台打印,所以线程 在线时死:sleep(1000)。

首先,在 erlang 中,我们称它们为进程——而不是线程。如果运行 handle_request() 的进程在睡眠时死掉,即在第二个格式语句显示任何输出之前,那么您应该会在某处看到一条错误消息。

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

我在一个进程中运行了以下函数:

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

我没有看到第二个输出。为什么我的进程在睡眠期间死亡?

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

这是一个示例,展示了如何让您的 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语句的写法会去掉警告:

警告:从“if”导出的变量“Ask_amount”

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]
    ).

我的.erl:

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

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

在外壳中:

~/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

对评论的回应

我想同时运行函数“request_handler”不止一次。 ...但是当我运行请求处理程序时:

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

第二个 io 不工作。

它对我有用:

-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]
     ).

在外壳中:

~/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

【讨论】:

是的,我知道这段代码可以运行,但我有一些客户,我想同时运行函数 'request_handler' 不止一次。感谢您的帮助,但是当我运行请求处理程序 ``` spawn(handlers, request_handler, [hello, 450, Banks]) ``` 第二个 io 不工作。 @KamranKoupayi,在我的答案底部查看我对您的评论的回复。 感谢您的帮助,问题是 Erlang 插件“InteliJ IDEA”。当我使用带有 Erlang 插件的 inteliJ IDEA 运行时,我在 shell 中运行它并且它运行完美。谢谢你的帮助。

以上是关于Erlang timer:sleep(1000) 导致线程死机的主要内容,如果未能解决你的问题,请参考以下文章

Python - 准确的 time.sleep

couchdb erlang reduce - 聚合对象

Erlang C 节点相关问题

Erlang中的重复元素列表

用erlang解析ejabberd数据包

Erlang,尝试制作 gen_server: 调用有很多响应