Yves
Yves

Reputation: 12431

julia: is it possible to play multithreading

I'm working with Julia.

There is a networking library: ZeroMQ.

What I need is to create a julia project which can receive multi-zeroMQ-sockets. Here is a simple explanation:

s1 = Socket();
ZMQ.bind(s1, ip1:port1);
s2 = Socket();
ZMQ.bind(s2, ip2:port2);
s3 = Socket();
ZMQ.bind(s3, ip3:port3);
ZMQ.recv(s1, msg0, 0); // it's blocking
ZMQ.recv(s2, msg1, 0); // it's blocking
ZMQ.recv(s3, msg2, 0); // it's blocking

So here I have three blocking recv, meaning that I should have a new thread for each of them.

But I don't know how to play multithreading with Julia.

Upvotes: 3

Views: 418

Answers (1)

StefanKarpinski
StefanKarpinski

Reputation: 33290

You don't need threads for this, you just need non-blocking I/O, which in Julia is how all I/O works, but it's exposed with a blocking API via tasks. So what you need to do is receive each message in its own task:

@sync begin
    @async ZMQ.recv(msg0, 0);
    @asycn ZMQ.recv(msg1, 0);
    @async ZMQ.recv(msg2, 0);
end

However, unless you are receiving from three different ZMQ sockets, this seems like a strange thing to do since you can only receive a single message on a socket anyway, and by making them async, you won't know which message is which. But assuming you're getting messages from different sources, this is how you do it.

Upvotes: 4

Related Questions