Why doesn't the Python multiprocessing demonstration process print to standard output?

I experimented with multiprocessing and worked in mindblock with daemons.

I have one daemon and one non-demon process, each of which gives an output every second endlessly, while the non-demon prints the output immediately after starting, sleeping for 3 seconds, then prints and returns again.

The problem is that the expected result of the daemon process is not displayed at all.

Reviewing past SO issues on daemons, common problems seem to be either other processes ending before the daemon, or stdout requiring a reset to output output. Both (I think) were considered, however, I continue to see only printed materials from non-demonic processes.

Code:

from multiprocessing import Process, current_process import sys import time def worker(): """ Announce that the process has started, sleep 3 seconds then announce that the process is ending. """ name = current_process().name print name, 'starting...' sys.stdout.flush() time.sleep(3) print name, 'ending...' sys.stdout.flush() return def daemon(): """ Announce that the process has started, beep, then beep once every second """ name = current_process().name print name, 'starting...' print 'beep...' sys.stdout.flush() while True: time.sleep(1) print 'beep...' sys.stdout.flush() if __name__=='__main__': d = Process(target=daemon) d.daemon = True d.start() p = Process(target=worker) p.daemon = False p.start() 

Expected Result:

 Process-1 starting... # Order here may vary beep... Process-2 starting... beep... beep... Process-2 ending... #There may or may not be another beep here 

What really turns out:

 Process-2 starting... Process-2 ending... 

Any advice on why this is happening will be truly appreciated.

+6
source share
1 answer

You can get a clearer picture of the order of events by enabling logging by posting

 import multiprocessing as mp logger = mp.log_to_stderr(logging.INFO) 

after other import statements. Then your program will give something like:

 [INFO/Process-1] child process calling self.run() [INFO/MainProcess] process shutting down Process-1 starting... beep... [INFO/Process-2] child process calling self.run() [INFO/MainProcess] calling terminate() for daemon Process-1 Process-2 starting... [INFO/MainProcess] calling join() for process Process-2 Process-2 ending... [INFO/Process-2] process shutting down [INFO/Process-2] process exiting with exitcode 0 [INFO/MainProcess] calling join() for process Process-1 

Thus, the main start ends first and then Process-1, the daemon process, ends. This is why you do not see more beeps while process 2 continues.

+4
source

Source: https://habr.com/ru/post/980360/


All Articles