简体   繁体   English

树上的生产者和消费者排队

[英]Queue for producers and consumers in a tree

I am reading up on how to utilize Python Queues to send and receive short messages between nodes. 我正在阅读有关如何利用Python队列在节点之间发送和接收短消息的信息。 I am simulating a set of nodes that are in a nice tree structure. 我正在模拟一组树状结构的节点。 I want some of these nodes to send a fixed-size data to its parent. 我希望其中一些节点将固定大小的数据发送到其父级。 Once this parent receives data from some of its child-nodes, it will "process" it and send a "aggregate" packet to its parent...and so on. 一旦父母从它的一些子节点接收数据,这将“过程”,并发送一个“总”包其父 ...等等。

To do this, I was told that queues would be useful to pass messages and a quick readup on it tells me that it will suit my needs. 为此,我被告知队列对于传递消息非常有用,并且对其进行快速阅读可以告诉我它适合我的需求。 However, I am finding it a bit difficult to implement a basic setup and test my understanding -- 1 producer (that generates a message packet) and 1 consumer (the worker thread that dequeues the task and processes it). 但是,我发现实现一个基本的设置并测试我的理解有点困难-1个生产者(生成一个消息包)和1个消费者(使任务出队并对其进行处理的工作线程)。

I have searched and read many posts here and elsewhere...and I understand all the queue methods. 我已经在这里和其他地方搜索并阅读了许多帖子...而且我了解所有队列方法。 What I still do not understand is how to associate or bind a queue to 2 given nodes. 我仍然不了解的是如何将队列关联或绑定到2个给定节点。

I want node-1 and node-2 to send messages to node-3. 我希望节点1和节点2将消息发送到节点3。 For this basic scenario, I have to somehow create one (or 2) queues and "associate" it with node-1 and node-2 which uses it to place messages into and node-3. 对于这种基本情况,我必须以某种方式创建一个(或2个)队列,并将其与节点1和节点2“关联”,然后使用该节点将消息放入节点3和节点3中。 And, node-3 must also be "listening"/"associated" with this queue to "get" or dequeue a task. 并且,节点3还必须与此队列“侦听” /“关联”以“获取”或出队任务。

If node-1 and node-2 are to be 'producers', I should have them as 2 separate threads and node-3 being the 3rd thread. 如果将node-1和node-2设为“生产者”,则应将它们作为2个单独的线程,而node-3作为第三个线程。 Then, I must create one queue Q. Then, I should have node-1 and node-2 create messages, 'put' them into the queue. 然后,我必须创建一个队列Q。然后,我应该让node-1和node-2创建消息,然后将它们“放入”队列。 Node-3 will have to be 'somehow' notified /waken-up to 'get' these messages from Q and process it. 必须以某种方式通知/唤醒Node-3,才能从Q中“获取”这些消息并进行处理。

I have seen 我见过

http://docs.python.org/library/queue.html#module-Queue http://docs.python.org/library/queue.html#module-Queue

Here is the (untested) code I have so far: 这是我到目前为止拥有的(未经测试的)代码:

================================================================= ================================================== ===============

import threading
import queue

q = Queue.Queue(2)   # create a queue of size 2.

# worker is node-3 which received msgs from 1 and 2 and fuses them.
def worker():
    while True:
        msg = q.get()
        fuse_msgs(msg)
        q.task_done()

# generate 3 worker threads. Node-3 could be both a producer and consumer. Each
# thread represents each node that will be a potential producer/consumer or both.
# need something like t1 - thread-1 for node-1 ...

for i in range(3):
     t = Thread(target=worker)
     t.setDaemon(True)
     t.start()

# How can I make node-1 and node-2 to put items into a specified queue???



for msg in source():
    q.put(item)

q.join()  

========================================= ========================================

Am I going in the right direction? 我朝着正确的方向前进吗? Please let me know where I am wrong and what I am misunderstanding... 请让我知道我在哪里错和我误会了什么...

Any help is appreciated. 任何帮助表示赞赏。 I have hundreds of nodes and links. 我有数百个节点和链接。 If I get this fundamentals straight, I will be able to move on smoothly... 如果我掌握了这些基本知识,我将能够顺利进行...

Thanks, BRSrini. 谢谢,BRSrini。

I'm not commenting on the python code in particular, but as far as your queues are designed, it seems you just need one queue in the node 1,2,3 scenario you were describing. 我没有特别评论python代码,但是就您设计的队列而言,在您描述的节点1,2,3场景中似乎只需要一个队列。 Basically, you have one queue, where you have node 1 and node 2 putting messages to, and node 3 reading from. 基本上,您有一个队列,在该队列中,节点1和节点2将消息放入其中,节点3进行读取。

You should be able to tell node-3 to do a "blocking" get on the queue, so it will just wait until it sees a message for it to process, and leave nodes 1 and 2 to produce their output as fast as possible. 您应该能够告诉节点3在队列上进行“阻塞”操作,因此它将等待直到看到要处理的消息,并离开节点1和2以尽可能快地产生其输出。

Depending on the processing speed of each node, and the traffic patterns you expect, you will probably want a queue deeper than 2 messages, so that the producing nodes don't have to wait for the queue to be drained. 根据每个节点的处理速度以及您期望的流量模式,您可能希望队列的长度大于2条消息,以便生产节点不必等待队列耗尽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM