在一个Celery 任务中,我启动了一个subprocess
,需要(1)能够发送一个SIGINT
,同时(2)还可以访问子进程的stdout和stderr.我可以做其中的一个,但不能同时做两个.
当子进程中的命令以列表或带有bash前缀的字符串形式给出时,我可以发送SIGINT
:
proc = subprocess.Popen(,
[sys.executable, "-m", path.to.module, myarg1, myarg2, ...], # also works with f"/bin/bash -c {sys.executable} -m path.to.module {myarg1} {myarg2} ..."
stdin=sys.stdin, stdout=PIPE, stderr=PIPE, shell=False
)
据我所知,这两种 Select 最终都会启动bash,似乎只有运行中的bash才会对SIGINT
做出react .
相反,运行"python-m..."意味着我的程序不再对SIGINT
做出react ,但另一方面,它允许我开始查看stdout/stderr并在我的python程序中记录:
proc = subprocess.Popen(,
f"{sys.executable} -m path.to.module {myarg1} {myarg2} ..."
stdin=sys.stdin, stdout=PIPE, stderr=PIPE, shell=False
)
有了上面的内容,现在我不能再向我的程序发送SIGINT
,但日志(log)记录工作正常.
我怎样才能让这两件事同时工作呢?我try 过shell=True
次和各种stdin/out/err调整,但没有运气.
编辑:使用最上面的表单(命令作为列表),并在path.to.module
中的程序中添加signal.signal()
,我既可以接收SIGINT,也可以看到一些输出.