vrod
Hello! I am studying the Task module and I am trying to teach myself some patterns of concurrency in Elixir. I found something like this while reading the documentation for Process.sleep/1:
Task.start_link(fn ->
Process.sleep(:timer.seconds(1))
send(parent, :work_is_done)
end)
receive do
:work_is_done -> IO.puts("Task complete!")
after
2_000 -> IO.puts("Operation timed out.")
end
My question is: is this the same as using Task.async?
async_task = Task.async(fn ->
Process.sleep(:timer.seconds(1))
:work_is_done
end)
val = Task.await(async_task)
IO.inspect(val)
# :work_is_done
So this gives me 3 questions (related) that I hope someone can explain to me:
- Does
Task.awaitfunction the same way as thesend+receiveexample? (I like the syntax ofTask.asyncandTask.await, but I want to make sure this is only syntax difference) - I tried doing send/recieve from
Task.start/1and it seems to work the same asTask.start_link/1. Is there case whereTask.start/1must be used or case whereTask.start_link/1must be used? - If I have a list of tasks, is it always better to use
Task.async_stream? I tried usingEnum.mapto put manyTask.asyncs together and it seems likeTask.async_streamis a better way.
Thank you! I want to try to understand this well before I study GenServers.
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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
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
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
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lovyou
sendandreceive. The same information you can find in the documentation:Yes, you use
start_linkinstead ofstartwhen you want to start a process linked to the current process. You can find more information about linking here: https://elixir-lang.org/getting-started/processes.html#links.Yes,
Task.async_streambe first thing to consider. If it doesn’t meet your requirements, then you can combineTask.asyncwithEnum/Streamfunctions.vrod
Thank you for your response! I think this is beginning to make sense.
ityonemo
It’s not exactly the same. Task.async is a bit smarter than that, because when you call it it saves a reference. When the await catches the message, it pattern matches against the reference to make sure it’s matching against the right task.
This protects against the situation where tasks cross streams. In the Task implementation, you can do this:
And your 1..10 will come back in order; they will not with your await implementation.
Check the elixir code for task.await: elixir/lib/elixir/lib/task.ex at v1.10.4 · elixir-lang/elixir · GitHub
This pattern will reappear in gen_server.call, so understanding it is probably a good idea
ityonemo
Also, if you’re still early on, Task is kind of still only the first layer on top of spawn. I use it a lot in tests, but I think for anything that hits prod you should consider Task.Supervised functions. It may not be necessary to fully understand yet depending on where you are in elixir learning.