Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wake a process from sleeping - Erlang

Tags:

erlang

Is it possible to wake a process externally in Erlang after it has been sent to sleep for infinity?

I would like to wake it from a different process which does hold the process ID of the process which is asleep.

I used this within the process which I want to sleep:

timer:sleep(infinity)

If it is not possible to wake it externally, what other options are available to me?

like image 671
Haych Avatar asked Jan 12 '23 00:01

Haych


2 Answers

Rather than using timer:sleep/1, put the process into a receive so that it waits for a message. When the other process wants it to proceed, it can simply send it a message. Assuming the message matches what the receive is looking for, the first process will then exit the receive and continue.

like image 52
Steve Vinoski Avatar answered Jan 17 '23 19:01

Steve Vinoski


From the source code of this function:

-spec sleep(Time) -> 'ok' when
  Time :: timeout().
sleep(T) ->
    receive
    after T -> ok
    end.

you can find that sleep just let the process wait, do nothing.

if you want wake the process, you need send message to the sleeping process and do receive.

like image 42
BlackMamba Avatar answered Jan 17 '23 20:01

BlackMamba