Start two processes and wait for the second process to finish to finish the first one

0

Dear users of the community after starting two processes simultaneously, how can I make Python 3 wait for the second process to finish and then finish the first one, I tried with time.sleep () But this only gives me a certain time, the idea is that the code is able to determine when the second process is complete to finish the first Eg: if I execute Word and then Paint, that Word closes once Paint (as a second process) is open Thank you in advance for your help.

    
asked by Franco M 17.05.2018 в 17:20
source

1 answer

0

Depends on how you throw the subprocesses.

For the examples you give (Word and Paint), I assume you work in Windows. There are different ways to launch threads from python, but not all of them allow you to communicate with the subprocess released for tasks such as waiting for it to finish, getting its exit status or forcing its completion.

The most flexible way is subprocess.Popen .

The following example opens two applications (Notepad and Paint), after which you wait for Paint to finish and when this happens, forces the completion of Notepad.

from subprocess import Popen

notepad = Popen("notepad")
paint = Popen("mspaint")

paint.wait()
notepad.kill()

Investigate the Popen manual if you want to pass arguments to the programs or send them signals or get their exit status code.

    
answered by 17.05.2018 в 17:49