Python 多核ZeroMQ?

Python 多核ZeroMQ?,python,multithreading,multiprocessing,zeromq,Python,Multithreading,Multiprocessing,Zeromq,ZeroMQ用于接收输入参数 def server(): rep = context.socket(zmq.REP) rep.bind('tcp://*:{}'.format(PORT)) while True: data = rep.recv_json() result = calculate(data) rep.send_json(result) 计算方法称为calculate,完成后,结果将通过ZMQ发送给客户端

ZeroMQ用于接收输入参数

def server():
    rep = context.socket(zmq.REP)
    rep.bind('tcp://*:{}'.format(PORT))

    while True:
        data = rep.recv_json()
        result = calculate(data)
        rep.send_json(result)
计算方法称为
calculate
,完成后,
结果将通过ZMQ发送给客户端

根据我的测试,它目前只使用机器的一个核心,现在我想使用其他核心。我已经阅读了一些关于
多处理
多线程
的文档,但它们主要关注固定输入,这不是我的情况


因此,我现在需要一些善意的帮助。

以下是如何使用
多处理
使多个工作进程能够处理并发客户端连接:

import zmq
from multiprocessing import Process

def calculate(data):
    return {"output": data['ok']}

def handle_request(url):
    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.connect(url)
    while True:
        data = socket.recv_json()
        print("received {}".format(data))
        out = calculate(data)
        socket.send_json(out)

def server():
    context = zmq.Context()
    # Set up socket for clients to connect to.
    clients = context.socket(zmq.ROUTER)
    clients.bind('tcp://*:{}'.format(5556))

    # Set up ipc socket for workers to connect to
    url_worker = 'ipc:///tmp/workers'
    workers = context.socket(zmq.DEALER)
    workers.bind(url_worker)

    # Start 4 worker processes    
    for _ in range(4):
        p = Process(target=handle_request, args=(url_worker,))
        p.start()

    # Forward requests from clients to the workers via a Queue
    zmq.device(zmq.QUEUE, clients, workers)


if __name__ == "__main__":
    server()
现在,如果您将示例客户端指向它:

import zmq
from threading import Thread

def send_req(request):
    context = zmq.Context()

    print("Connecting to hello world server...")
    socket = context.socket(zmq.REQ)
    socket.connect("tcp://localhost:5556")
    print("Sending request %s ..." % request)

    socket.send_json({"ok" : "Hello"})

    message = socket.recv()
    print("Received reply %s [ %s ]" % (request, message))

#  Do 10 requests in parallel
for request in range(10):
    Thread(target=send_req, args=(request,)).start()
您将获得以下输出:

Connecting to hello world server...
Sending request 0 ...
Connecting to hello world server...
Sending request 1 ...
Connecting to hello world server...
Sending request 2 ...
Connecting to hello world server...
Sending request 3 ...
Connecting to hello world server...
Sending request 4 ...
Connecting to hello world server...
Sending request 5 ...
Connecting to hello world server...
Sending request 6 ...
Connecting to hello world server...
Sending request 7 ...
Connecting to hello world server...
Sending request 8 ...
Connecting to hello world server...
Sending request 9 ...
<5 second delay>
Received reply 0 [ {"output":"Hello"} ]
Received reply 1 [ {"output":"Hello"} ]
 Received reply 3 [ {"output":"Hello"} ]
Received reply 2 [ {"output":"Hello"} ]
<5 second delay>
Received reply 4 [ {"output":"Hello"} ]
Received reply 5 [ {"output":"Hello"} ]
 Received reply 6 [ {"output":"Hello"} ]
Received reply 7 [ {"output":"Hello"} ]
< 5 second delay>
Received reply 8 [ {"output":"Hello"} ]
Received reply 9 [ {"output":"Hello"} ]
连接到hello world服务器。。。
正在发送请求0。。。
正在连接到hello world服务器。。。
正在发送请求1。。。
正在连接到hello world服务器。。。
正在发送请求2。。。
正在连接到hello world服务器。。。
正在发送请求3。。。
正在连接到hello world服务器。。。
正在发送请求4。。。
正在连接到hello world服务器。。。
正在发送请求5。。。
正在连接到hello world服务器。。。
正在发送请求6。。。
正在连接到hello world服务器。。。
正在发送请求7。。。
正在连接到hello world服务器。。。
正在发送请求8。。。
正在连接到hello world服务器。。。
正在发送请求9。。。
收到答复0[{“输出”:“你好”}]
收到回复1[{“输出”:“你好”}]
收到回复3[{“输出”:“你好”}]
收到回复2[{“输出”:“你好”}]
收到回复4[{“输出”:“你好”}]
收到回复5[{“输出”:“你好”}]
收到回复6[{“输出”:“你好”}]
收到回复7[{“输出”:“你好”}]
<5秒延迟>
收到回复8[{“输出”:“你好”}]
收到回复9[{“输出”:“你好”}]

我在中尝试了
多线程Hello World服务器
,但它只使用单核。您希望
计算
本身使用多核,还是希望能够在多个输入上同时运行
计算
?根据
calculate
的详细信息,执行前者可能很困难,执行后者应该非常简单。@dano两者都有。。这有多简单?(稍后我可能会处理
calculate
本身)需要注意的是@dano并不同时使用套接字,而是将消息转发到多个后端套接字。这是因为ZeroMQ套接字不是线程安全的。谢谢!我想知道
4
是什么意思?尝试了多处理。cpu_count(),它返回4,看起来像cpu线程计数。。。Python进程将使用一个内核还是一个线程?@SnoopyGuo我只是随意选择了4个
multiprocessing.cpu_count()
如果
calculate
是cpu绑定的(根据名称,可能是cpu绑定的),那么它将是一个不错的选择。每个进程都是单线程的,但它们可以跨CPU核心扩展。因此,如果您有4个内核,那么4个进程可以并行使用其中一个内核。