本文介绍了Python zeromq——单个订阅者的多个发布者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 python 脚本(称为 parent)来执行以下操作:

(1)定义了一个多维的numpy数组

(2) forks 10 个不同的 python 脚本(称它们为 children).他们每个人都必须能够在任何一个时间点从(1)读取numpy 数组的内容(只要他们还活着).

(3)每个脚本都会做自己的工作(不要互相分享任何信息)>

(4) 在任何时间点, 脚本都必须能够接受来自其所有 的消息.这些消息将被父级解析并导致 (1) 中的 numpy 数组发生变化.

Linux 环境中使用 python 时,我该如何处理?我想使用 zeroMQ 并让 parent 成为单个 subscriberchildren 将都是 出版商;这样做有意义还是有更好的方法?

另外,我如何允许所有子级连续读取定义的numpy数组的内容父母 ?

解决方案

sub 通道不必是要绑定的通道,因此您可以绑定订阅者,并且每个儿童 pub 频道可以连接到该频道并发送他们的消息.在这种特殊情况下,我认为 multiprocessing 模块更合适,但我认为它很无聊:

导入zmq进口螺纹# 为了您可以将其复制并粘贴到交互式会话中,我是# 使用线程,但显然这不是你会使用的# 我是多个客户正在写入的订阅者定义父():上下文 = zmq.Context()socket = context.socket(zmq.SUB)socket.setsockopt(zmq.SUBSCRIBE, 'Child:')# 即使我是订阅者,我也可以参加这个派对# 从`bind` 开始socket.bind('tcp://127.0.0.1:5000')# 我期望 50 条消息对于范围内的我(50):打印 'Parent 收到:%s' % socket.recv()#我是儿童出版商定义孩子(数量):上下文 = zmq.Context()socket = context.socket(zmq.PUB)# 即使我是发布者,我也可以进行连接#比绑定socket.connect('tcp://127.0.0.1:5000')对于范围(5)中的数据:socket.send('Child: %i %i' % (number, data))socket.close()线程 = [threading.Thread(target=parent)] + [threading.Thread(target=child, args=(i,)) for i in range(10)]对于线程中的线程:线程开始()对于线程中的线程:线程连接()

特别是,文档的核心消息传递模式部分讨论了事实上,对于模式,任何一方都可以绑定(另一方可以连接).

I'd like to write a python script (call it parent) that does the following:

(1) defines a multi-dimensional numpy array

(2) forks 10 different python scripts (call them children). Each of them must be able to read the contents of the numpy array from (1) at any single point in time (as long as they are alive).

(3) each of the child scripts will do it's own work (children DO NOT share any info with each other)

(4) at any point in time, the parent script must be able to accept messages from all of its children. These messages will be parsed by the parent and cause the numpy array from (1) to change.


How do I go about this, when working in python in a Linux environment? I thought of using zeroMQ and have the parent be a single subscriber while the children will all be publishers; does it make sense or is there a better way for this?

Also, how do I allow all the children to continuously read the contents of the numpy array that was defined by the parent ?

解决方案

The sub channel doesn't have to be the one to bind, so you can have the subscriber bind, and each of the children pub channels can connect to that and send their messages. In this particular case, I think the multiprocessing module is a better fit, but I thought it bore mentioning:

import zmq
import threading

# So that you can copy-and-paste this into an interactive session, I'm
# using threading, but obviously that's not what you'd use

# I'm the subscriber that multiple clients are writing to
def parent():
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.SUBSCRIBE, 'Child:')
    # Even though I'm the subscriber, I'm allowed to get this party
    # started with `bind`
    socket.bind('tcp://127.0.0.1:5000')

    # I expect 50 messages
    for i in range(50):
        print 'Parent received: %s' % socket.recv()

# I'm a child publisher
def child(number):
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    # And even though I'm the publisher, I can do the connecting rather
    # than the binding
    socket.connect('tcp://127.0.0.1:5000')

    for data in range(5):
        socket.send('Child: %i %i' % (number, data))
    socket.close()

threads = [threading.Thread(target=parent)] + [threading.Thread(target=child, args=(i,)) for i in range(10)]
for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

In particular, the Core Messaging Patterns part of the documentation discusses the fact that for the patterns, either side can bind (and the other connect).

这篇关于Python zeromq——单个订阅者的多个发布者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:57
查看更多