summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--python/mp.py31
1 files changed, 31 insertions, 0 deletions
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
+