new test for mp

This commit is contained in:
2018-05-17 21:44:45 +02:00
parent 32828d3a65
commit f78ddbc005
8 changed files with 324 additions and 25 deletions
+70
View File
@@ -0,0 +1,70 @@
import threading
import multiprocessing
import time
import sys
def network_worker(tq, wq):
while tq.empty() is not True:
ticker = tq.get(0.1)
print(threading.currentThread().getName(),"received ",ticker)
wq.put(ticker-1000)
#simulate work
time.sleep(0.01)
tq.task_done()
for i in range(8):
wq.put(None)
def worker(wq, i):
while True:
task = wq.get(2)
with i.get_lock():
if task is None:
break
i.value += 1
print(multiprocessing.current_process().name, task, wq.qsize(), i.value)
#simulate work
time.sleep(0.001)
wq.task_done()
print(task)
wq.task_done()
print("Closing: ", multiprocessing.current_process().name)
ticker_queue = multiprocessing.JoinableQueue()
work_queue = multiprocessing.JoinableQueue()
iterations = multiprocessing.Value('i', 0)
tickers = range(1000)
processes = []
threads = []
for i in tickers:
ticker_queue.put(i)
for i in range(20):
t = threading.Thread(target=network_worker, args = (ticker_queue, work_queue))
t.deamon = True
print("Starting: ",t.name)
t.start()
threads.append(t)
for i in range(8):
p = multiprocessing.Process(target = worker, args = (work_queue, iterations, ))
p.deamon = True
print("Starting: ",p.name)
p.start()
processes.append(p)
print("Closing down threads")
for t in threads:
print("Closing: ",t.name)
t.join()
print("Closing down workers")
for p in processes:
p.join()
print("Total number of iterations:", iterations.value)