我正在尝试在Python中的多处理库中使用队列。执行下面的代码后(打印语句起作用),但是在调用Queue上的join之后,这些进程没有退出,并且仍然存在。我如何终止其余过程?
谢谢!
def MultiprocessTest(self): print "Starting multiprocess." print "Number of CPUs",multiprocessing.cpu_count() num_procs = 4 def do_work(message): print "work",message ,"completed" def worker(): while True: item = q.get() do_work(item) q.task_done() q = multiprocessing.JoinableQueue() for i in range(num_procs): p = multiprocessing.Process(target=worker) p.daemon = True p.start() source = ['hi','there','how','are','you','doing'] for item in source: q.put(item) print "q close" q.join() #q.close() print "Finished everything...." print "num active children:",multiprocessing.active_children()
尝试这个:
import multiprocessing num_procs = 4 def do_work(message): print "work",message ,"completed" def worker(): for item in iter( q.get, None ): do_work(item) q.task_done() q.task_done() q = multiprocessing.JoinableQueue() procs = [] for i in range(num_procs): procs.append( multiprocessing.Process(target=worker) ) procs[-1].daemon = True procs[-1].start() source = ['hi','there','how','are','you','doing'] for item in source: q.put(item) q.join() for p in procs: q.put( None ) q.join() for p in procs: p.join() print "Finished everything...." print "num active children:", multiprocessing.active_children()