From a7b64caa0e22a974d352b091866d346da90c0ab5 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Wed, 20 Dec 2017 22:26:50 +0100 Subject: python/mp.py --- python/mp.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 python/mp.py diff --git a/python/mp.py b/python/mp.py new file mode 100644 index 0000000..93fa864 --- /dev/null +++ b/python/mp.py @@ -0,0 +1,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 + -- cgit v1.2.3