【发布时间】:2015-05-28 16:58:03
【问题描述】:
我对 Python 多处理相当陌生,我遇到了一个教程,因此尝试检查它的多处理。这里的进程没有被终止。他们永远在奔跑。怎么了。?我读到当可选参数为真时,进程不会终止。因此我在那里放了空的声明。但该过程仍然没有终止。请指教。谢谢。我使用的机器有 Python 2.7.6 这是我的代码
from multiprocessing import Process
from Queue import Empty,Queue
import math
def isprime(n):
if not isinstance(n,int):
raise TypeError("argument is not of int type")
if n<2:
return False
if n==2:
return True
max= int(math.ceil(math.sqrt(n)))
i=2
while i<=max:
if n%i==0:
return False
return True
def sum_primes(n):
return sum([x for x in xrange(2,n) if isprime(x)])
def do_work(q):
while True:
try:
x=q.get(block=False)
print sum_primes(x)
except Empty:
break
if __name__=="__main__":
work_queue=Queue()
for i in range (100000,5000000,100000):
work_queue.put(i)
processes=[Process(target=do_work, args=(work_queue,)) for i in range(8)]
for p in processes:
p.start()
for p in processes:
p.join()
【问题讨论】:
-
尝试通过在调用 producer.start() 之前添加 producer.daemon = True 使您的进程成为守护进程,并查看脚本是否会退出
标签: python multiprocessing terminate