new test for mp
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user