我开始学习python中的多处理,但已经到了代码挂起的地步。它只需使用多线程计算1000000个阶乘。在
import multiprocessing
def part(n):
ret = 1
n_max = n + 9999
while n <= n_max:
ret *= n
n += 1
print "Part "+ str(n-1) + " complete"
return ret
def buildlist(n_max):
n = 1
L = []
while n <= n_max:
L.append(n)
n += 10000
return L
final = 1
ne = 0
if __name__ == '__main__':
pool = multiprocessing.Pool()
results = [pool.apply_async(part, (x,)) for x in buildlist(1000000)]
for r in results:
x = r.get()
final *= x
ne+= 1
print ne
print final
我已经包含了一些print函数来尝试诊断代码挂起的位置,它将按预期打印part函数中包含的字符串100次。“print ne”也能工作100次。在
问题是final无法打印,代码也无法完成。在
我如何解决这个问题?在
编辑:还有,既然这次投票被否决了,有人能解释一下我做错了什么/为什么我被否决了?在
程序运行良好-直到
print final
。然后它花了大量的时间来打印这个数字,这是非常巨大的。。。在相关问题 更多 >
编程相关推荐