Hi All,
I am trying to achieve threading inside each process of multiprocessing. I have 2 queues one for multiprocess (process) & another inside each process. when i execute it got hung after below output. My goal here is to go through p_source queue & for each process picks up all t_source queue (parallel) so i have used thread inside process.
Pls correct what i have missed here. Thanks !
#!/usr/bin/env python2.7
import multiprocessing
from threading import Thread
from Queue import Queue
def do_stuff(q):
print ("its do_stuff fn - {}".format(q.get()))
def start_my_work(i,q):
print ("Start work for {}".format(q))
t_source = ['My','Name','Is','John','I','Work','In','IT']
qu = Queue()
num_threads = 2
for i in range(num_threads):
worker = Thread(target=do_stuff, args=(qu,))
worker.setDaemon(True)
worker.start()
for item in t_source:
qu.put(item)
qu.join()
q = multiprocessing.Queue()
jobs = []
p_source = ['hi','there','how','are','you','doing']
for item in p_source:
q.put(item)
for i in range(3): # 3 multiprocess
p = multiprocessing.Process(target=start_my_work, args=(i,q))
jobs.append(p)
p.start()
for p in jobs: #join on all processes ...
p.join()
print "List processing complete."
Start work for <multiprocessing.queues.Queue object at 0x7f9fdfc1bc90>
its do_stuff fn - My
its do_stuff fn - Name
Start work for <multiprocessing.queues.Queue object at 0x7f9fdfc1bc90>
its do_stuff fn - My
Start work for <multiprocessing.queues.Queue object at 0x7f9fdfc1bc90>
its do_stuff fn - Name
its do_stuff fn - My
its do_stuff fn - Name