简体   繁体   English

Python进程挂起,直到子进程使用Tee方案终止

[英]Python Process hangs until child process terminate using tee scheme

I have a following problem related to process synchronization. 我有以下与进程同步有关的问题。

There is a python script startup.py, an executable maestro, and an executable tee. 有一个python脚本startup.py,一个可执行的大师和一个可执行的tee。 My python script startup.py starts maestro program and pipes the stderr/stdout of maestro into the log file using tee as well write into the console. 我的python脚本startup.py启动maestro程序,并使用tee将maestro的stderr / stdout通过管道传输到日志文件中,并写入控制台。

I achieved this using following code: 我使用以下代码实现了这一点:

cmd = "maestro"
mae_err_log = "output.txt"
maestro = subprocess.Popen(cmd, stderr = subprocess.STDOUT, stdout=subprocess.PIPE)
tee = subprocess.Popen(['tee', mae_err_log], stdin = maestro.stdout)
maestro.stdout.close()
tee.communicate()
maestro_status = maestro.returncode
sys.exit(maestro_status)

My maestro program is a gui program when I exit from the maestro program, it internally call posix system("maestro_cleanup &") api and immediately exit. 我退出maestro程序时,我的maestro程序是gui程序,它在内部调用posix system(“ maestro_cleanup&”)api并立即退出。 I noticed that my maestro program does not release the console until maestro_cleanup program terminate although I am running maestro_cleanup in the background. 我注意到在maestro_cleanup程序终止之前,我的maestro程序不会释放控制台,尽管我在后台运行maestro_cleanup。 I need to run maestro_cleanup in the background as it takes time and I do not want to hold the console for the period until maestro_cleanup finishes. 我需要在后台运行maestro_cleanup,因为这需要时间,并且在maestro_cleanup完成之前,我不希望在一段时间内保留控制台。 Using above code, maestro program does not terminate until maestro_cleanup finishes. 使用上述代码,maestro程序直到maestro_cleanup完成后才终止。

I see "ps -elf" shows following: 我看到“ ps -elf”显示如下:

0 S j 16876  6678  0  75 0 - 65307 wait 18:56 pts/53 00:00:00 python startup.py
0 Z j 17230 16876  4  76 0 - 0     exit 18:56 pts/53 00:00:04 [maestro] <defunct>
0 S j 17231 16876  0  77 0 - 948 pipe_w 18:56 pts/53 00:00:00 /usr/bin/tee output.txt
0 S j 17424     1  0  77   0 -   948 -  18:57 pts/53   00:00:00 maestro_cleanup

You can see the maestro_cleanup parent process is session process id instead maestro because I started maestro_cleanup using system api in background. 您可以看到maestro_cleanup父进程是会话进程ID,而不是maestro,因为我在后台使用系统api启动了maestro_cleanup。

Any idea why maestro does not terminate until maestro_cleanup finishes? 知道为什么maestro直到maestro_cleanup完成才终止吗?

Any help would be appreciated. 任何帮助,将不胜感激。

-Vipin -维品

Well the subprocess.Popen documentation does say tee.communicate() will "Wait for process to terminate"; 好的subprocess.Popen 文档确实说tee.communicate()将“等待进程终止”; also your invocation is the same as this guy who does say his child process runs in the background, so that seems ok. 您的调用也与此人相同,后者确实说他的子进程在后台运行,所以看起来还可以。 So this blocking-before-exit sounds like a limitation of subprocess. 因此,这种出口前阻塞听起来像是子流程的限制。

Try the links in 'Python spawn off a child subprocess, detach, and exit' which references the ActiveState recipe: 'Creating a daemon the Python way': The Python way to detach a process from the controlling terminal and run it in the background as a daemon. 尝试参考“ ActiveState配方”的“ Python产生一个子子进程,分离并退出”中的链接:“以Python方式创建一个守护程序”:从控制终端分离进程并在后台运行该进程的Python方式一个守护程序。

I'd say use close_fd=True . 我会说使用close_fd=True It should do the trick. 它应该可以解决问题。

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

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