multiprocessing.Process() 刚刚停止工作
multiprocessing.Process() just stopped working
我在 Spyder 上的 Python 中自学多进程处理,并且正在处理一些相对简单的示例,但它突然停止工作了。回到一些以前有效的更简单的例子,它们现在似乎也不起作用了。我想不出我能做些什么来让他们停止工作。下面是我的代码:
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)
没有错误消息。
您需要阅读 error
并按照说明进行操作!
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()
我在 Spyder 上的 Python 中自学多进程处理,并且正在处理一些相对简单的示例,但它突然停止工作了。回到一些以前有效的更简单的例子,它们现在似乎也不起作用了。我想不出我能做些什么来让他们停止工作。下面是我的代码:
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)
没有错误消息。
您需要阅读 error
并按照说明进行操作!
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()