Python subprocess
跳转到导航
跳转到搜索
注意事项
check_output 不会在子进程退出时返回
和 ssh 相似,check_output 会等待管道关闭,即使子进程已经退出(但是有其它进程仍在使用此管道),它也不会返回。可以使用 .wait() 方法来等待子进程退出而非管道关闭。
使用 bash 时的信号处理
当使用 shell=True,shell 为 bash 时,SIGINT、SIGQUIT 信号可能被 bash 忽略,然后被 Python 进程继承。使用以下代码恢复 KeyboardInterrupted 异常:
import signal
signal.signal(signal.SIGINT, signal.default_int_handler)