AttributeError: 'NoneType' object has no attribute 'WriteFile'
win11 + python3.11
报错:
python
from multiprocessing import Pool,Process
def func(i):
print(i)
if __name__ == '__main__':
pool = Pool(10)
for i in range(10):
p = Process(target=func, args=(i,))
p.start()
"""
1
0
9
3
7
2
6
4
5
8
Exception ignored in: <function Pool.__del__ at 0x000001F92C4C0900>
Traceback (most recent call last):
File "C:\ProgramData\anaconda3\Lib\multiprocessing\pool.py", line 271, in __del__
File "C:\ProgramData\anaconda3\Lib\multiprocessing\queues.py", line 374, in put
File "C:\ProgramData\anaconda3\Lib\multiprocessing\connection.py", line 199, in send_bytes
File "C:\ProgramData\anaconda3\Lib\multiprocessing\connection.py", line 279, in _send_bytes
AttributeError: 'NoneType' object has no attribute 'WriteFile'
"""
这个问题是代码问题,不能进程池和进程同时混用.
解决方案要么选择使用进程池要么选择使用多进程.
python
from multiprocessing import Process
from concurrent.futures import ProcessPoolExecutor
def func(i):
print(i)
if __name__ == '__main__':
# 多进程实现
# for i in range(10):
# p = Process(target=func, args=(i,))
# p.start()
# 进程池实现进程的创建
pool = ProcessPoolExecutor(10)
for i in range(10):
pool.submit(func, i)
pool.shutdown(wait=True)