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
Latest Phoenix Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- 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.mrcasals
For clarity, here’s my code.
My
seeds.exsfile:The problem right now is that when the
Enum.mapsection of the seeds file finishes, all otherTasks are deleted.I read that the
Task.asyncneeds to be used together withTask.await, and I don’t need to check the result during the normal life cycle of the app, only in the seeding part. That’s why I didn’t want to use it, but maybe I understood it wrong?I finally moved to a mini-loop that checks if the seed data is complete to let the tasks run correctly:
I don’t find it a bad solution, although I guess mine is not the most Elixir-esque one…
Thanks to both, @idi527 @peerreynders
dimitarvp
What about Task.yield?
You will immediately avoid the very ugly pattern of waiting fixed amounts and then re-checking.