Python Forum
Subprocess.send_signal, wait until completion
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subprocess.send_signal, wait until completion
#1
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
Reply
#2
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()
Reply
#3
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
Reply
#4
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 :)
Reply
#5
(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
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wait til a date and time KatManDEW 2 1,390 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  wait for the first of these events Skaperen 4 1,872 Mar-07-2022, 08:46 PM
Last Post: Gribouillis
  how to do a two-way wait Skaperen 2 1,229 Mar-04-2022, 02:31 AM
Last Post: Skaperen
  Run an app in a standalone terminal and wait until it's closed glestwid 2 2,447 Aug-30-2020, 08:14 AM
Last Post: glestwid
  python os.popen is not working for wait method elenaflorence87 0 1,969 Jul-22-2020, 12:56 PM
Last Post: elenaflorence87
  Wait for command within a process bRitch022 1 3,181 Jul-15-2020, 07:03 PM
Last Post: bRitch022
  Multiprocessing Module Running An Infinite Child Process Even After Completion hsikora 0 3,836 Dec-19-2018, 08:01 AM
Last Post: hsikora
  How do you wait for a daemon to reach a certain state with subprocess.popen()? phalse 3 7,772 Jul-08-2017, 12:32 PM
Last Post: DeaD_EyE
  just a trial. Please wait 5 minutes before deleting this thread sylas 2 3,357 Jun-06-2017, 03:34 PM
Last Post: snippsat
  Code completion for namedtupe-based object in PyCharm volcano63 4 5,908 May-01-2017, 09:18 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020