summaryrefslogtreecommitdiff
path: root/python/mp.py
blob: 93fa864b5d88fbe4e7ff7f4de64bb2b78330b1e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import threading as mp
from Queue import Queue
from time import sleep
from random import randint

workers = []
queues = []
master_queue = Queue()

def f(i, master, queue):
    while True:
        inp = master_queue.get(True)
        print("got input %s"%inp)
        sleep(randint(1,10))
        queue.put(">>>> %d %s\n"%(i, inp))

for i in range(10):
    queue = Queue(1)
    workers.append(mp.Thread(target=f, args=(i,master_queue,queue)))
    queues.append(queue)
    workers[i].start()

for i in range(10000):
    master_queue.put("msg-"+str(randint(1,1000)))

while True:
    for i in range(10):
        if not queues[i].empty():
            data = queues[i].get()
            print data