mrcly

mrcly

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.

First 6 of 6 Posts Switch mode

evadne

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

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.

mrcly

mrcly OP

Hey, thanks for the answer.

So in general, in my GenServer implementation, inside init/1 i call :gen_tcp.connect, a couple of :gen_tcp.send for authentication and other stuff, and at the end :gen_tcp.recv which calls itself recursively to receive data all the time until the client is stopped, which is possible the reason why its blocking the repl.

hauleth

hauleth

Yes, you should never do any “heavy lifting” stuff in init/1, and for sure no infinite loops. You in general shouldn’t do any infinite loops in gen_server as there is already an internal loop for that and you should rely on messages instead.

entone

entone

Here’s an example of TCP Client I’ve been using lately, this uses active: :once and receives messages in handle_info, it also uses SSL instead of TCP, but the interfaces are the same, should be able to swap out :ssl for :gen_tcp pretty easily.

In addition it uses a Registry to publish the messages once they’ve been properly parsed.

One thing to note about receiving your messages, this one can receive multiple messages per handle_info, so the parser is actually recursive. If your messages are null terminated, or some other termination, you can set that when you connect to the tcp socket. Check out all the options when connecting. gen_tcp — OTP 29.0.2 (kernel 11.0.2)

derek-zhou

derek-zhou

If everything is done in init/1, then you don’t need a GenServer. Just spawn_link/3 it:, ie:

def start_link(args) do
    pid = :erlang.spawn_link(__MODULE__, :init, [args])
    {ok, pid}
end

def child_spec(args) do
    %{id: __MODULE__,
      start: {__MODULE__, :start_link, [args]},
      type: :worker}
end

Now you have a supervise-able worker process just like a GenServer, except everything is in init/1

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement