1

我有以下与进程同步相关的问题。

有一个 python 脚本 startup.py、一个可执行的 maestro 和一个可执行的 tee。我的 python 脚本 startup.py 启动 maestro 程序并使用 tee 将 maestro 的 stderr/stdout 通过管道传输到日志文件中,并写入控制台。

我使用以下代码实现了这一点:

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)

我的 maestro 程序是一个 gui 程序,当我退出 maestro 程序时,它在内部调用 posix system("maestro_cleanup &") api 并立即退出。我注意到我的 maestro 程序在 maestro_cleanup 程序终止之前不会释放控制台,尽管我在后台运行 maestro_cleanup。我需要在后台运行 maestro_cleanup,因为这需要时间,而且我不想在 maestro_cleanup 完成之前持有控制台。使用上面的代码,maestro 程序在 maestro_cleanup 完成之前不会终止。

我看到“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

您可以看到 maestro_cleanup 父进程是会话进程 ID 而不是 maestro,因为我在后台使用系统 api 启动了 maestro_cleanup。

知道为什么在 maestro_cleanup 完成之前 maestro 不会终止吗?

任何帮助,将不胜感激。

-维平

4

2 回答 2

1

那么subprocess.Popen文档确实说 tee.communicate() 将“等待进程终止”;您的调用也与这个确实说他的子进程在后台运行的人相同,所以这似乎没问题。所以这个退出前阻塞听起来像是子进程的限制。

尝试引用 ActiveState 配方的“Python 生成子子进程、分离和退出”中的链接:“以 Python 方式创建守护进程”:从控制终端分离进程并在后台运行的 Python 方法一个守护进程。

于 2011-07-04T20:34:56.043 回答
0

我会说使用close_fd=True. 它应该可以解决问题。

于 2013-12-01T18:37:56.587 回答