【发布时间】:2021-04-26 06:38:23
【问题描述】:
我正在尝试在 MacOS 上学习线程和多处理。我无法启动这些进程,python 给出了以下错误消息。
错误
运行时错误: 已尝试在 当前进程已完成其引导阶段。
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.
我的代码: parallel_processing.py
import multiprocessing
import time
start= time.perf_counter()
def do_something():
print('sleeping 1 sec....')
time.sleep(1)
return('done sleeping...')
# do_something()
p1 = multiprocessing.Process(target = do_something)
p2 = multiprocessing.Process(target = do_something)
p1.start()
p2.start()
finish= time.perf_counter()
print(f'finished in {finish-start} seconds')
【问题讨论】:
标签: macos python-multiprocessing python-3.8