mrcly
Run TCP client in a background/non-blocking process
Hi,
i am running a TCP client using gen_tcp inside a GenServer, which is called by DynamicSupervisor using Supervisor.start_client(). Whenever I call this method, the output (which is logged with IO.puts) is blocking the REPL. I start the application via iex -S mix. In the future, it is planned to execute some actions when a specific message is received via TCP, but that’s not implemented currently. However, I need to perform multiple TCP-clients at once and after calling Supervisor.start_client() once, the REPL is blocked and another TCP client cant be spawned.
How can I run this TCP-Client in a background/non-blocking process?
Thanks in advance.
Most Liked
evadne
Do not do the work in init/1. Init is supposed to return quickly. Instead use continuation. See GenServer — Elixir v1.20.2. Return {:ok, state, {:continue, continue}} in your init/1 callback and continue your setup / whatever other sort of blocking operation in handle_continue/2.
entone
Yeah, without seeing your client code, it’s hard to say why it would be blocking. Even a long running init callback shouldn’t block indefinitely.
My guess would be that your making a blocking call from within your init callback.
Popular in Questions
Other popular topics
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









