【发布时间】:2018-04-29 01:14:01
【问题描述】:
Jupyter 笔记本
我基本上在使用多处理模块,我还在学习多处理的功能。我正在使用 Dusty Phillips 的书,这段代码属于它。
import multiprocessing
import random
from multiprocessing.pool import Pool
def prime_factor(value):
factors = []
for divisor in range(2, value-1):
quotient, remainder = divmod(value, divisor)
if not remainder:
factors.extend(prime_factor(divisor))
factors.extend(prime_factor(quotient))
break
else:
factors = [value]
return factors
if __name__ == '__main__':
pool = Pool()
to_factor = [ random.randint(100000, 50000000) for i in range(20)]
results = pool.map(prime_factor, to_factor)
for value, factors in zip(to_factor, results):
print("The factors of {} are {}".format(value, factors))
在 Windows PowerShell(不是 jupyter notebook)上,我看到以下内容
Process SpawnPoolWorker-5:
Process SpawnPoolWorker-1:
AttributeError: Can't get attribute 'prime_factor' on <module '__main__' (built-in)>
我不知道为什么单元格永远不会结束运行?
【问题讨论】:
标签: python-3.x debugging multiprocessing jupyter