茱莉亚:是否有可能玩多线程 [英] julia: is it possible to play multithreading

查看:83
本文介绍了茱莉亚:是否有可能玩多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和朱莉娅一起工作.

I'm working with Julia.

有一个网络库:ZeroMQ.

There is a networking library: ZeroMQ.

我需要创建一个julia项目,该项目可以接收多个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

所以在这里我有三个阻塞recv,这意味着我应该为每个阻塞一个新线程.

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

但是我不知道如何与Julia一起玩多线程.

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

推荐答案

您不需要线程,只需要非阻塞I/O,在Julia中这是所有I/O的工作方式,但是它是公开的通过任务使用阻塞API.因此,您需要做的是在自己的任务中接收每条消息:

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

但是,除非您从三个不同的ZMQ套接字接收,否则这样做似乎很奇怪,因为无论如何您只能在套接字上接收单个消息,并且通过使它们异步,您将不知道哪个消息是哪个.但是,假设您从不同的来源获取消息,这就是您的处理方式.

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.

这篇关于茱莉亚:是否有可能玩多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆