简体   繁体   中英

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. On my system and Python (2.X) daemon is False by default. But according to the 3.X documentation

If provided, the keyword-only daemon argument sets the process daemon flag to True or False. If None (the default), this flag will be inherited from the creating process.

Meaning that there is a possibility in your Python Shell on Windows to run processes as daemon without explicit specification.

To change this either set the flag to 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 ).

Or use join :

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

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

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

print "done"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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