【问题标题】:Python Process hangs until child process terminate using tee schemePython 进程挂起,直到子进程使用 tee 方案终止
【发布时间】:2011-08-10 08:28:43
【问题描述】:

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

有一个 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 不会终止吗?

任何帮助将不胜感激。

-维平

【问题讨论】:

    标签: python popen tee


    【解决方案1】:

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

    尝试'Python spawn off a child subprocess, detach, and exit' 中的链接,其中引用了 ActiveState 配方:“以 Python 方式创建守护程序”:将进程从控制终端分离并在后台作为守护程序运行的 Python 方式。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-08
        • 2017-02-28
        • 2013-08-18
        • 2016-12-02
        • 2012-09-07
        • 2015-07-07
        • 2021-08-12
        相关资源
        最近更新 更多