45 lines
988 B
Python
45 lines
988 B
Python
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
|
|
import multiprocessing
|
|
import queue
|
|
|
|
|
|
running = multiprocessing.Value('i', 1)
|
|
request = multiprocessing.JoinableQueue()
|
|
response = multiprocessing.Queue(1)
|
|
|
|
work=[]
|
|
|
|
def worker(request, response):
|
|
#try:
|
|
param = request.get(timeout=0.1)
|
|
if param.empty()
|
|
# Imagine heavy computation here.
|
|
#except queue.Empty:
|
|
# To check running flag.
|
|
response.put("work_done")
|
|
else:
|
|
result = param ** 2
|
|
response.put(result)
|
|
|
|
|
|
def main():
|
|
for n in range(10):
|
|
work.append(n)
|
|
process = multiprocessing.Process(target=worker, args=(request, response))
|
|
process.start()
|
|
for i in work:
|
|
print("Queueing {}".format(i))
|
|
request.put(i)
|
|
result = response.get()
|
|
#if result != "work_done":
|
|
while result != "work_done":
|
|
print('Result', result)
|
|
process.join()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|