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
+30 -19
View File
@@ -3,29 +3,38 @@ import multiprocessing
import time
import sys
def network_worker(tq, wq):
def network_worker(tq, wq, eq):
ticker_queue = tq
work_queue = wq
while ticker_queue.empty() is not True:
ticker = tq.get()
print(threading.currentThread().getName(),"recieved ",ticker)
ticker = tq.get(0.1)
print(threading.currentThread().getName(),"received ",ticker)
work_queue.put(ticker-1000)
time.sleep(0.02)
#ticker_queue.task_done()
work_queue.put(None)
time.sleep(0.01)
ticker_queue.task_done()
exit_queue.put(1)
#work_queue.close()
return
def worker(wq):
def worker(wq, i):
work_queue = wq
while work_queue.get() is not None:
task = work_queue.get()
print(multiprocessing.current_process(),"recieved",task)
#work_queue.task_done()
#while work_queue.get(0.1) is not None:
while work_queue.qsize():
print(work_queue.qsize())
#if work_queue.empty() == False:
with i.get_lock():
i.value += 1
task = work_queue.get(10)
print(multiprocessing.current_process(),"received",task)
#simulate work
time.sleep(0.001)
work_queue.task_done()
return
ticker_queue = multiprocessing.JoinableQueue()
work_queue = multiprocessing.JoinableQueue()
exit_queue = multiprocessing.JoinableQueue()
iterations = multiprocessing.Value('i', 0)
tickers = range(1000)
processes = []
@@ -33,24 +42,26 @@ 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 = threading.Thread(target=network_worker, args = (ticker_queue, work_queue, exit_queue))
t.deamon = True
print("Starting: ",t.name)
t.start()
for i in range(4):
p = multiprocessing.Process(target = worker, args = (work_queue, ))
#p.deamon = True
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)
if work_queue.empty() == True:
ticker_queue.join()
print(exit_queue,empty())
if exit_queue.empty() == False:
work_queue.join()
for p in processes:
p.terminate()
p.join()
print("Resulting work:", work_queue)
print("Total number of iterations:", iterations.value)