In a celery task, I am launching a subprocess
and need (1) to be able to send a SIGINT
while (2) also having access to the subprocess's stdout and stderr. I am able to do one or the other, but not both simultaneously.
I can send SIGINT
when the command in subprocess is given as a list, or as a string prepended with bash:
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
)
As far as I understand, both options are ultimately launching bash and it seems that only a running bash will react to SIGINT
.
Conversely, running "python -m ..." means my program no longer reacts to the SIGINT
, but on the other hand it allows me to start seeing the stdout/stderr and logging inside my python program:
proc = subprocess.Popen(,
f"{sys.executable} -m path.to.module {myarg1} {myarg2} ..."
stdin=sys.stdin, stdout=PIPE, stderr=PIPE, shell=False
)
With the above, now I'm no longer able to send SIGINT
to my program but the logging is working.
How can I get both things to work at the same time? I've played around with shell=True
and the various stdin/out/err tweaks, but no luck.
EDIT: With the top form (command as a list) and adding signal.signal()
to my program in path.to.module
I am able to both receive the SIGINT as well as see some output.