Python Forum

Full Version: Subprocess.send_signal, wait until completion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a subprocess running named proc and need to send it a USR1 signal. To do that, I'm using proc.send_signal(signal.SIGUSR1). The problem is that it takes some time for the process to do what it does after receiving the signal and I need to wait until it finishes that before continuing the execution of my code. I am currently adding a time.sleep(0.5) line after sending the signal (it usually takes about half a second to finish), but it varies a little with each execution and therefore it does not always works as needed.

Is there a way to wait until the process finishes doing what it does after sending a signal to it?

Thank you very much in advance and please forgive my English.

Best regards,
Plinio
You can use process.wait(

wait()

Or check_output or check_call which all wait for the return code depending on what you want to do and the version of python.

time.sleep(0.5)
proc.send_signal(signal.SIGUSR1)
process.wait()
Thank you maffaz for your reply,

Regretfully I cannot use proc.wait(), because I'm not terminating the process. proc.wait() waits until the process finishes, but in this case I am sending a signal USR1 to "pause" the task of the subprocess, not to finish it. Therefore, wait() will wait forever...

I need a way to wait until the process completes the "pausing", I mean, wait until it finishes doing what the signal USR1 made it to do.

I hope I explained it more clearly this time.

Best,
Plinio
Have a look at this example:

https://python-forum.io/Thread-Popen-How...5#pid50825

Let me know if it helps. you will have to modify a little..

If it doesn't please post the code and we can have a proper look..

just the process part :)
(Jun-28-2018, 09:53 PM)maffaz Wrote: [ -> ]Have a look at this example:

https://python-forum.io/Thread-Popen-How...5#pid50825

Let me know if it helps. you will have to modify a little..

If it doesn't please post the code and we can have a proper look..

just the process part :)

Something like this..

pause_complete = 'finished'
proc = subprocess.Popen(command + args, 
                                  stdout=subprocess.PIPE, stdin=subprocess.PIPE, 
                                  stderr=subprocess.PIPE, universal_newlines=True)

for line in proc.stdout:
    print(line)
    if pause_complete in line.split():
        print('Pausing Finished')
        ... do something
Thank you very much maffaz! That's an idea worth exploring!
As soon as I get home I'll try it and let you know the outcome.

Best,
Plinio