zulkris
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}
]}
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
joey_the_snake
It looks like a race condition. The messages aren’t being sent before the main process finishes sleeping. What happens if you change this:
To have lower numbers? like
1..10joey_the_snake
Actually my bad. There is a bug in your code. The
Workeris sending the message to itself instead of the main process. You have to do thisrvirding
The reason is that in
Worker.start/the function defined insingle_api_callis called before a message is sent. This is because that function is called when creating the message to be sent{self(), fun.()}and the send requires all ita arguments to be evaluated which means it waits until the function returns which is after it has slept.zulkris
yep, i set Sleep(3000) at main process to wait all children finished
zulkris
checked, now we sent it to the correct process, but still no messages at the main process at the end of the script:
returns:
joey_the_snake
I think it should work…I get stuff like this consistently
I’m not sure how you’re running it, is it possible the module needs to be recompiled?
zulkris
I’m running directly in terminal via
elixir script.exs…added: I’m running inside docker! it works at system directly… O_o than its solved, but still want to know WHY docker affects
docker-compose.yml:
joey_the_snake
pure guessing, but maybe the number of cpus assigned to the docker container is too small and so the spawned tasks don’t get scheduled until after the main process is finished
dimitarvp
I’ll be the guy to ask:
What is it that you’re truly aiming at?
There are good job queue / worker pool libraries, we can point you at some of them if you don’t want to roll your own.
bdarla
As it is also recommended in the hex docs, using sleep shall be avoided.
If you want to use the first
sleepto “simulate” something, leave it there for your testing.However, the sleep of the parent process can be relaxed with a receive as suggested in the aforementioned Hexdocs, e.g. with a
receive:In this example from Hexdocs, replace
do_somethingwithsingle_api_calland theTaskwith yourProcess.