mrcasals
Hi all!
I’m starting my first project with Elixir and Phoenix. I have a function in a context that creates a record and fires some asynchronous code using Task.start. This task fetches data from a local URL and updates the newly created record.
I want to reuse this code for my seed data, but the process seems to finish before the Task.start has finished running, so the seed records are not being updated with the complete data.
The code works as expected from iex -S mix.
Is there any way to force the process to wait until the Tasks are finished, without using Task.async? The only thing that comes to mind is a loop that counts the records in the DB that have not yet been updated, and sleep for a second if there’s any, but I’d like to try to find a better solution.
Thanks!
Trending in Questions
Other Trending Topics
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Most Liked- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
You can enter a
receiveblock, which is whatTask.awaitdoes. But for that you’d need to send your process a message on task completion to leave that block. The easiest way to do that would probably beTask.async, so why don’t you want to use it?peerreynders
Refactoring the code so that it can be used with either
Task.start/3orTask.async/3andTask.await/3should be the preferred approach - that wayasync/awaitcould be used in the seeding script.A more “hackey” solution:
startreturns{:ok, pid}; monitor thepidwithProcess.monitor/1and at the end of the script put areceive/1for the{:DOWN, ref, :process, object, reason}message.Last Post!
dimitarvp
What about Task.yield?
You will immediately avoid the very ugly pattern of waiting fixed amounts and then re-checking.