我有以下与进程同步相关的问题。
有一个 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 不会终止吗?
任何帮助,将不胜感激。
-维平