这个怎么解决 File "C:\Users\10720\.conda\envs\pytorch\Lib\multiprocessing\spawn.py", line 122, in spawn_main exitcode = _main(fd, parent_sentinel)
时间: 2023-11-09 14:06:38 浏览: 263
pytorch3d-0.7.1-py37-cu116-pyt1121.tar.bz2
这个错误是由于在Windows系统上使用multiprocessing时,需要将进程启动方法放在if __name__ == '__main__':语句块中。这是因为Windows系统上的进程启动方式与Unix系统不同。因此,您需要将muchjob函数定义放在if __name__ == '__main__':语句块中,以确保在Windows系统上正确启动进程。您可以按照以下方式修改代码:
```
import time
import multiprocessing
def muchjob(x):
time.sleep(2)
return x**2
if __name__ == '__main__':
tic = time.time()
ans = [muchjob(i) for i in range(4)]
print('ans', ans)
toc = time.time()
print('used {:.10}s'.format(toc - tic))
print('*' * 20)
tic = time.time()
pool = multiprocessing.Pool(processes=3)
res = []
for i in range(4):
res.append(pool.apply_async(muchjob, (i,)))
pool.close()
pool.join()
ans = [r.get() for r in res]
print('ans', ans)
toc = time.time()
print('used {:.10}s'.format(toc-tic))
```
阅读全文