我一直在尝试多处理,并使用守护进程运行到一个mindblock.
我有一个守护进程和一个非守护进程,守护进程每隔一秒无限期地发出输出,而非守护进程在启动时立即打印输出,休眠3秒,然后再次打印并返回.
问题是,守护进程的预期输出根本没有显示出来.
回顾过去关于守护进程的SO问题,常见问题似乎是在守护进程之前结束的其他进程,或者需要刷新以显示输出的stdout.两者都(我认为)已得到解决,但我仍然只看到来自非守护进程的打印输出.
代码:
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() …Run Code Online (Sandbox Code Playgroud)