Python multiprocessing.Process()刚刚停止工作
我在Spyder上自学Python中的多重存储,并在学习一些相对简单的示例时,它突然停止了工作。回到一些以前有效的简单示例,它们现在似乎不起作用。我想不出我能做些什么让他们停止工作。下面是我的代码:Python multiprocessing.Process()刚刚停止工作,python,python-3.x,multithreading,python-multiprocessing,Python,Python 3.x,Multithreading,Python Multiprocessing,我在Spyder上自学Python中的多重存储,并在学习一些相对简单的示例时,它突然停止了工作。回到一些以前有效的简单示例,它们现在似乎不起作用。我想不出我能做些什么让他们停止工作。下面是我的代码: import time import multiprocessing start = time.perf_counter() def do_something(): print('Sleeping 1 second...') time.sleep(1) print('Do
import time
import multiprocessing
start = time.perf_counter()
def do_something():
print('Sleeping 1 second...')
time.sleep(1)
print('Done Sleeping...')
p1 = multiprocessing.Process(target = do_something)
p2 = multiprocessing.Process(target = do_something)
p1.start()
p2.start()
p1.join()
p2.join()
finish = time.perf_counter()
print(f'Finished in {round(finish - start, 2)} second(s)')
它看起来就像中间部分:
p1 = multiprocessing.Process(target = do_something)
p2 = multiprocessing.Process(target = do_something)
p1.start()
p2.start()
p1.join()
p2.join()
不是吗
编辑
唯一的输出是
Finished in 0.64 second(s)
没有错误信息。您需要阅读
错误
并按照说明操作
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
请运行以下代码:
import multiprocessing
def worker(num):
"""Returns the string of interest"""
return "worker %d" % num
def main():
pool = multiprocessing.Pool(4)
results = pool.map(worker, range(10))
pool.close()
pool.join()
for result in results:
# prints the result string in the main process
print(result)
if __name__ == '__main__':
# Better protect your main function when you use multiprocessing
main()
停止工作意味着什么?给出一个错误?它只是像多处理代码不在那里一样运行输出“在0.64秒内完成”谢谢我从来没有收到任何错误它只是在多处理代码不在那里说“在0.64秒内完成”@Christopherl你是通过Windows运行它吗?如果有的话。那么哪个
空闲
。请确认是否以python script.py
的形式运行它是的,我正在windows上运行Spyder@ChristopherEll感谢αԋɱҽԃαМєιcαη,我想我仍然有问题,我已经尝试在程序中的不同点使用if name=='main',但仍然得到了几乎相同的结果。