我不相信这是this的重复,因为他的问题似乎是由使用multiprocessing.pool
引起的,而我没有这么做。在
该计划:
import multiprocessing
import time
def task_a(procrange,result):
"Naively identify prime numbers in an iterator of integers. Procrange may not contain negative numbers, 0, or 1. Result should be a multiprocessing.queue."
for i in procrange: #For every number in our given iterator...
for t in range (2,(i//2)+1): #Take every number up to half of it...
if (i % t == 0): #And see if that number goes evenly into it.
break #If it does, it ain't prime.
else:
#print(i)
result.put(i) #If the loop never broke, it's prime.
if __name__ == '__main__':
#We seem to get the best times with 4 processes, which makes some sense since my machine has 4 cores (apparently hyperthreading doesn't do shit)
#Time taken more or less halves for every process up to 4, then very slowly climbs back up again as overhead eclipses the benifit from concurrency
processcount=4
procs=[]
#Will search up to this number.
searchto=11000
step=searchto//processcount
results=multiprocessing.Queue(searchto)
for t in range(processcount):
procrange=range(step * t, step * (t+1) )
print("Process",t,"will search from",step*t,"to",step*(t+1))
procs.append(
multiprocessing.Process(target=task_a, name="Thread "+str(t),args=(procrange,results))
)
starttime=time.time()
for theproc in procs:
theproc.start()
print("Processing has begun.")
for theproc in procs:
theproc.join()
print(theproc.name,"has terminated and joined.")
print("Processing finished!")
timetook=time.time()-starttime
print("Compiling results...")
resultlist=[]
try:
while True:
resultlist.append(results.get(False))
except multiprocessing.queues.Empty:
pass
print(resultlist)
print("Took",timetook,"seconds to find",len(resultlist),"primes from 0 to",searchto,"with",processcount,"concurrent executions.")
。。。工作完美,结果是:
^{pr2}$然而,如果search_to
增加500。。。在
Processing has begun.
Thread 0 has terminated and joined.
Thread 1 has terminated and joined.
Thread 2 has terminated and joined.
。。。剩下的就是沉默。进程黑客显示Python线程每个消耗12%的CPU,一个接一个地消耗掉。。。而不是终止。它们一直挂着直到我手动终止它们。在
为什么?在
**显然,无论是上帝还是圭多都有一种残酷的幽默感。在
似乎
result.put(i)
中有一个问题,因为当我提交它时,脚本开始运行良好。所以我建议您不要使用来保存结果multiprocessing.Queue
。相反,您可以使用数据库:MySQL、MongoDB等。注意:您不能使用SQLite,因为在SQLite中,在任何时刻(从docs)只有一个进程可以对数据库进行更改。在相关问题 更多 >
编程相关推荐