How to listen to a ZMQ subscriber in the separate thread or in parallel?

I have a ZMQ subscriber socket

I want to constantly listen to this socket and react (call a callback method) whenever a message is received

Perhaps like this:

while true
 msg = ZMQ.recv(socket)
 callback(msg)
end

How can I do this without blocking my main thread?

Do I use Threads somehow or Tasks or what?

Thanks!

The simplest way is to use Tasks, e.g. via @async macro:

@async while true
 msg = ZMQ.recv(socket)
 callback(msg)
end
2 Likes