我在Python 3.7(在Windows 10中)中使用多处理和venv时遇到一些问题。我在testing.py
中有以下脚本:
from multiprocessing import Pool
def square(number):
return number * number
def main():
print("Starting")
p = Pool(1)
l = [1,2,3]
for res in p.imap(square, l):
print("Res:", res)
print("Done")
if __name__ == '__main__':
main()
然后使用以下命令执行它
python testing.py
我得到以下输出(按预期):
Starting
Res: 1
Res: 4
Res: 9
Done
但是,如果我创建并激活venv
,然后使用来运行该
python -m venv venv
venc\Script\activate.bat
python testing.py
我唯一的输出是
Starting
,脚本永远不会结束,而只会一直等待。如果我停用venv并再次运行它,它将再次起作用。
venv\Script\deactivate.bin
python testing.py
如果我在venv
中运行脚本时卡住了脚本,请按Ctrl + C,我将得到以下信息:
KeyboardInterrupt
Process SpawnPoolWorker-34:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\Program Files\Python37\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "C:\Program Files\Python37\lib\multiprocessing\pool.py", line 110, in worker
task = get()
File "C:\Program Files\Python37\lib\multiprocessing\queues.py", line 351, in get
with self._rlock:
File "C:\Program Files\Python37\lib\multiprocessing\synchronize.py", line 95, in __enter__
return self._semlock.__enter__()
KeyboardInterrupt
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python37\lib\multiprocessing\__init__.py", line 16, in <module>
from . import context
File "C:\Program Files\Python37\lib\multiprocessing\context.py", line 6, in <module>
from . import reduction
File "C:\Program Files\Python37\lib\multiprocessing\reduction.py", line 16, in <module>
import socket
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 906, in _find_spec
File "<frozen importlib._bootstrap_external>", line 1280, in find_spec
File "<frozen importlib._bootstrap_external>", line 1252, in _get_spec
File "<frozen importlib._bootstrap_external>", line 1364, in find_spec
File "<frozen importlib._bootstrap_external>", line 81, in _path_stat
第一次,我得到SpawnPoolWorker-1
,第二次得到SpawnPoolWorker-2
,但是它一直在增加,所以感觉就像游泳池在不断地催生工人,但是我不知道为什么,为什么它只会在venv
中使用。