当我尝试运行来自documantaions的简单操作时,例如: https://docs.python.org/3.4/library/multiprocessing.html?highlight=process
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
它无限期地运行代码。我试图添加close和join,并试图获取过程的ID,但没有任何效果。好像它一遍又一遍地循环池,无法执行它。怎么了?
from datetime
import datetime from datetime
import date
import os
import time
from multiprocessing import Pool
def y(z):
print(f'Process {os.getpid()} working record {z}')
time.sleep(1)
q = z*2
print(f'Process {os.getpid()} done record {q}')
return(q)
with Pool(2) as p:
result = p.map(y, [1, 2, 3])
p.close()
p.join()
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))
######错误
Process SpawnPoolWorker-1:
Process SpawnPoolWorker-2:
Traceback (most recent call last):
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py", line 110, in worker
task = get()
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\queues.py", line 354, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'y' on <module '__main__' (built-in)>
Traceback (most recent call last):
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py", line 110, in worker
task = get()
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\queues.py", line 354, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'y' on <module '__main__' (built-in)>
Process SpawnPoolWorker-3:
Traceback (most recent call last):
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py", line 110, in worker
task = get()
File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\queues.py", line 354, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'y' on <module '__main__' (built-in)>
答案 0 :(得分:0)
在修复了第二个语法错误(似乎只是复制粘贴问题)后,您的两个代码示例都在我的机器上的python3中正常运行。 (请参阅下面的编辑代码。)也许您的计算机上配置错误,或者您没有使用正确的命令来运行它们?您能够成功运行其他python程序吗?
from datetime import datetime
from datetime import date
import os
import time
from multiprocessing import Pool
def y(z):
print(f'Process {os.getpid()} working record {z}')
time.sleep(1)
q = z*2
print(f'Process {os.getpid()} done record {q}')
return(q)
start_time = datetime.now()
with Pool(2) as p:
result = p.map(y, [1, 2, 3])
p.close()
p.join()
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))