Have code:
single_api_call = fn ->
sleep_time = Enum.random(554..2944)
Process.sleep(sleep_time) # this line affects results
sleep_time
end
defmodule Worker do
def start(fun) do
pid = self()
send(pid, {self(), fun.()})
end
end
Enum.to_list(1..10)
|> Enum.map(
fn _num ->
spawn Worker, :start, [single_api_call]
end)
Process.sleep(3000)
Process.info(self(), :messages) |> IO.inspect()
please see at line with Process.sleep
.
Now code works this way:
[#PID<0.103.0>, #PID<0.104.0>, #PID<0.105.0>, #PID<0.106.0>, #PID<0.107.0>,
#PID<0.108.0>, #PID<0.109.0>, #PID<0.110.0>, #PID<0.111.0>, #PID<0.112.0>]
{:messages, []}
messages is empty! why?
after deleting line with Process.sleep()
it works fine:
[#PID<0.103.0>, #PID<0.104.0>, #PID<0.105.0>, #PID<0.106.0>, #PID<0.107.0>,
#PID<0.108.0>, #PID<0.109.0>, #PID<0.110.0>, #PID<0.111.0>, #PID<0.112.0>]
{:messages,
[
{#PID<0.103.0>, 2049},
{#PID<0.104.0>, 619},
{#PID<0.105.0>, 2854},
{#PID<0.106.0>, 2584},
{#PID<0.107.0>, 706},
{#PID<0.109.0>, 2700},
{#PID<0.110.0>, 2397},
{#PID<0.111.0>, 2303},
{#PID<0.112.0>, 1863}
]}