Updated ticker_new.py
This commit is contained in:
4
Multiprocessing test/mp_test_4.py
Normal file → Executable file
4
Multiprocessing test/mp_test_4.py
Normal file → Executable file
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python2
|
||||
|
||||
import sys #exit
|
||||
import gobject
|
||||
@@ -297,4 +297,4 @@ if __name__ == '__main__':
|
||||
|
||||
print "finished"
|
||||
sys.exit()
|
||||
|
||||
|
||||
|
||||
@@ -3,16 +3,12 @@ import multiprocessing
|
||||
import time
|
||||
import sys
|
||||
|
||||
def network_worker(tq, wq):
|
||||
def handler(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:
|
||||
@@ -21,13 +17,9 @@ def worker(wq, i):
|
||||
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()
|
||||
@@ -37,34 +29,37 @@ tickers = range(1000)
|
||||
processes = []
|
||||
threads = []
|
||||
|
||||
for i in tickers:
|
||||
ticker_queue.put(i)
|
||||
def looprun(n):
|
||||
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(20):
|
||||
t = threading.Thread(target = handler, args = (ticker_queue, work_queue))
|
||||
t.deamon = True
|
||||
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)
|
||||
for i in range(8):
|
||||
p = multiprocessing.Process(target = worker, args = (work_queue, iterations, ))
|
||||
p.deamon = True
|
||||
p.start()
|
||||
processes.append(p)
|
||||
|
||||
ticker_queue.join()
|
||||
for i in range(8):
|
||||
work_queue.put(None)
|
||||
|
||||
work_queue.join()
|
||||
|
||||
print("Closing down workers")
|
||||
for p in processes:
|
||||
p.join()
|
||||
print("Run:",n)
|
||||
print("Total number of iterations:", iterations.value)
|
||||
return
|
||||
|
||||
for n in range(1):
|
||||
looprun(n)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user