简体   繁体   English

为什么在使用多处理模块时我的程序无法正常工作

[英]why my program doesn't work when I using multiprocessing module

I'm tring to learn the multiprocessing module and I found some example code from the internet. 我正努力学习多处理模块,并从互联网上找到了一些示例代码。 the code is same but the result is different . 代码相同,但结果不同。 please help why my subprogram doesn't work ? 请帮助为什么我的子程序不起作用?

The only way I can reproduce your problem is if I set the processes to be daemonic: 重现问题的唯一方法是将进程设置为守护进程:

p1 = Process(target=piao, args=('a',))
p2 = Process(target=piao, args=('b',))
p3 = Process(target=piao, args=('c',))

p1.daemon = True
p2.daemon = True
p3.daemon = True

p1.start()
p2.start()
p3.start()

A daemon thread will continue to run without blocking the main program from exiting. daemon线程将继续运行,而不会阻止主程序退出。 On my system and Python (2.X) daemon is False by default. 在我的系统和Python(2.X) daemon上,默认情况下为False But according to the 3.X documentation 但是根据3.X文档

If provided, the keyword-only daemon argument sets the process daemon flag to True or False. 如果提供,则仅关键字守护程序参数将进程守护程序标志设置为True或False。 If None (the default), this flag will be inherited from the creating process. 如果为None(默认),则此标志将从创建过程中继承。

Meaning that there is a possibility in your Python Shell on Windows to run processes as daemon without explicit specification. 这意味着Windows上的Python Shell中有可能在没有显式指定的情况下将进程作为daemon运行。

To change this either set the flag to false: 要更改此设置,请将标志设置为false:

p1.daemon = False
p2.daemon = False
p3.daemon = False

which has to be done before calling start but in case of Python 3.6 can be done in the command where you invoke the Process object (see this ). 这必须在调用start之前完成,但是对于Python 3.6,可以在调用Process对象的命令中完成(请参阅参考资料 )。

Or use join : 或使用join

p1.daemon = True
p2.daemon = True
p3.daemon = True

p1.start()
p2.start()
p3.start()

p1.join()
p2.join()
p3.join()

print "done"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM