One manufacturer of several consumers

I want to have one manufacturer, a multi-tenant architecture in Python when doing multi-threaded programming. I want to have this operation:

  • Manufacturer produces data
  • Consumers 1..N (N predefined) wait for the data to be received (blocked), and then process this data in different ways.

Therefore, I need all consumers to receive the same data from the manufacturer.

When I used Queue to accomplish this, I realized that everyone except the first consumer would starve with my implementation.

One of the possible solutions is to have a unique queue for each of the consumer flows in which the same data is transferred to several queues by the manufacturer. Is there a better way to do this?

from threading import Thread
import time
import random
from Queue import Queue

my_queue = Queue(0)

def Producer():
    global my_queue
    my_list = []
    for each in range (50):
        my_list.append(each)
    my_queue.put(my_list)

def Consumer1():
    print "Consumer1"
    global my_queue
    print my_queue.get()
    my_queue.task_done()

def Consumer2():
    print "Consumer2"
    global my_queue
    print my_queue.get()
    my_queue.task_done()


P = Thread(name = "Producer", target = Producer)

C1 = Thread(name = "Consumer1", target = Consumer1)

C2 = Thread(name = "Consumer2", target = Consumer2)


P.start()

C1.start()

C2.start()

C2 , C1 , P1. , C1 C2 , P1.

/​​!

+4
2

:

my_queue.put(my_list)

, my_list , :

def Producer():
    global my_queue
    my_list = []
    for each in range (50):
        my_list.append(each)
    my_queue.put(my_list)
    my_queue.put(my_list)

, .

: , , .

, , .

, , .

+1

?

, " ". , , .

+1

All Articles