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

question

#1

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!


#2

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

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