[英]Python multiprocessing is not calling function
I am trying to understand how multiprocessing works in Python.我试图了解多处理在 Python 中的工作原理。 Here is a simple code which is not calling the function as I expected it would.
这是一个简单的代码,它没有像我预期的那样调用 function。
import time
import multiprocessing
def do_something():
print('Sleep')
time.sleep(1)
print('Wake up')
start = time.perf_counter()
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)')
In Jupyter Notebook , after executing I am getting following output:在Jupyter Notebook中,执行后我开始关注 output:
Finished in 0.2 second(s)
I though it would be like something like this:我虽然会是这样的:
Sleep
Sleep
Wake up
Wake up
Finished in 0.2 second(s)
What am I missing?我错过了什么?
You should check "Programming guidelines" ( https://docs.python.org/3/library/multiprocessing.html#multiprocessing-programming ) to figure out why you need the:您应该检查“编程指南”( https://docs.python.org/3/library/multiprocessing.html#multiprocessing-programming )以找出您需要以下内容的原因:
if __name__ == '__main__' :
guard in your scripts that use multiprocessing.在使用多处理的脚本中保护。 Since you don't have that in your notebooks, it wont work properly.
由于您的笔记本中没有它,因此它无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.