子进程调用停止异步执行 Python 父进程
Subprocess call stopping asynchronously-executed Python parent process
以下 shell 会话演示了我所看到的行为:
[user@compname python-test]$ cat test.py
#!/usr/bin/env python
import subprocess
from time import sleep
proc = subprocess.Popen("ffmpeg", stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
print "Starting process: " + str(proc)
status = proc.poll()
while status is None:
print "Process still running."
sleep(0.01)
status = proc.poll()
print "Status: " + str(status)
[user@compname python-test]$ python test.py
Starting process: <subprocess.Popen object at 0x7f2893f27150>
Process still running.
Process still running.
Status: 1
[user@compname python-test]$ python test.py &
[4] 6976
[user@compname python-test]$ Starting process: <subprocess.Popen object at 0x7ffa0ea82150>
Process still running.
Process still running.
[4]+ Stopped python test.py
[user@compname python-test]$ ps
PID TTY TIME CMD
4684 pts/101 00:00:00 python
4685 pts/101 00:00:00 ffmpeg
7183 pts/101 00:00:00 ps
14385 pts/101 00:00:00 bash
可以看到,当简单测试Python程序正常运行时,就成功完成了。当它是 运行 异步时(使用 &
),Python 进程在子进程调用完成后立即停止(并且 poll()
将 return 非-None
值).
- 使用
Popen.wait()
时会发生相同的行为
- 该行为是
ffmpeg
独有的。
- Python 进程和
ffmpeg
都停止了,如对 ps
的调用所示。
谁能帮我解决这个问题?我在 subprocess
模块、bash 的 &
运算符或 ffmpeg
的文档中没有看到任何可以解释这一点的内容。
Python版本是2.6.6,bash是GNU bash版本4.1.2(1)-release(x86_64-redhat-linux-gnu),ffmpeg是3.0版本.1-静态。
感谢您的帮助!
当shell=False 时会出现同样的情况吗?使用 shell=False.
时,您必须指向可执行文件的直接路径
我发现 ffmpeg
在 运行 异步时会阻塞自己。一个邮件列表显示使用 nohup
可以避免这个问题。有兴趣的请参阅 here。
以下 shell 会话演示了我所看到的行为:
[user@compname python-test]$ cat test.py
#!/usr/bin/env python
import subprocess
from time import sleep
proc = subprocess.Popen("ffmpeg", stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
print "Starting process: " + str(proc)
status = proc.poll()
while status is None:
print "Process still running."
sleep(0.01)
status = proc.poll()
print "Status: " + str(status)
[user@compname python-test]$ python test.py
Starting process: <subprocess.Popen object at 0x7f2893f27150>
Process still running.
Process still running.
Status: 1
[user@compname python-test]$ python test.py &
[4] 6976
[user@compname python-test]$ Starting process: <subprocess.Popen object at 0x7ffa0ea82150>
Process still running.
Process still running.
[4]+ Stopped python test.py
[user@compname python-test]$ ps
PID TTY TIME CMD
4684 pts/101 00:00:00 python
4685 pts/101 00:00:00 ffmpeg
7183 pts/101 00:00:00 ps
14385 pts/101 00:00:00 bash
可以看到,当简单测试Python程序正常运行时,就成功完成了。当它是 运行 异步时(使用 &
),Python 进程在子进程调用完成后立即停止(并且 poll()
将 return 非-None
值).
- 使用
Popen.wait()
时会发生相同的行为
- 该行为是
ffmpeg
独有的。 - Python 进程和
ffmpeg
都停止了,如对ps
的调用所示。
谁能帮我解决这个问题?我在 subprocess
模块、bash 的 &
运算符或 ffmpeg
的文档中没有看到任何可以解释这一点的内容。
Python版本是2.6.6,bash是GNU bash版本4.1.2(1)-release(x86_64-redhat-linux-gnu),ffmpeg是3.0版本.1-静态。
感谢您的帮助!
当shell=False 时会出现同样的情况吗?使用 shell=False.
时,您必须指向可执行文件的直接路径我发现 ffmpeg
在 运行 异步时会阻塞自己。一个邮件列表显示使用 nohup
可以避免这个问题。有兴趣的请参阅 here。