简体   繁体   English

为什么Python多处理守护进程不能打印到标准输出?

[英]Why is a Python multiprocessing daemon process not printing to standard output?

I have been experimenting with multiprocessing, and running into a mindblock with daemons. 我一直在尝试多处理,并使用守护进程运行到一个mindblock。

I have one daemon and one non-daemon process, the daemon emitting output every one second indefinitely, while the non-daemon prints output immediately upon start, sleeps for 3 seconds, then prints again and returns. 我有一个守护进程和一个非守护进程,守护进程每隔一秒无限期地发出输出,而非守护进程在启动时立即打印输出,休眠3秒,然后再次打印并返回。

The problem is, the expected output from the daemon process doesn't show up at all. 问题是,守护进程的预期输出根本没有显示出来。

Reviewing past SO questions on daemons, the common issues appear to be either the other processes ending before the daemon, or the stdout requiring flushing to show output. 回顾过去关于守护进程的SO问题,常见问题似乎是在守护进程之前结束的其他进程,或者需要刷新以显示输出的stdout。 Both have (I think ) been addressed, however I continue to only see printed output from non-daemonic processes. 两者都(我认为 )已得到解决,但我仍然只看到来自非守护进程的打印输出。

The 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 Output: 预期产出:

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 actually gets produced: 实际生产的内容:

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

Any advice on why this is happening would be truly appreciated. 任何关于为什么会发生这种情况的建议都会得到真正的体会

You can get a clearer picture of the order of events by turning on logging by placing 您可以通过放置打开日志来更清楚地了解事件的顺序

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

after the other import statements. 在其他进口声明之后。 Then your program will yield 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 starts shutting down first, then it terminates Process-1, the daemon process. 因此,主要开始先关闭,然后终止进程-1,即守护进程。 That's why you do not see any more beeps while Process-2 continues. 这就是为什么在Process-2继续时你不会再看到哔哔声的原因。

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

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