Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess.Popen() wait for completion [duplicate]

I am writing a small script to serially walk through a directory and run a command on the subdirectories therein.

I am running into a problem however with Popen() that it will walk through the directories and run the desired command without waiting for the previous one to finish. i.e.

for dir in dirs:     #run command on the directory here. 

it kicks off the command for each dir without caring about it afterwards. I want it to wait for the current one to finish, then kick off the next. The tool I am using on the directories is Log2timeline from SANS SIFT which takes quite a while and produces quite a bit of output. I don't care about the output, I just want the program to wait before kicking off the next.

What's the best way to accomplish this?

Thank you!

like image 611
DJMcCarthy12 Avatar asked Feb 02 '15 18:02

DJMcCarthy12


People also ask

Does Popen wait for process to finish?

Using Popen MethodThe Popen method does not wait to complete the process to return a value. This means that the information you get back will not be complete.

Is Popen wait blocking?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.

What is subprocess Popen in Python?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

What is the difference between subprocess run and Popen?

Popen . The main difference is that subprocess. run() executes a command and waits for it to finish, while with subprocess. Popen you can continue doing your stuff while the process finishes and then just repeatedly call Popen.


1 Answers

Use Popen.wait:

process = subprocess.Popen(["your_cmd"]...) process.wait() 

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

If you are using python >= 2.7 and you don't care about the output just use check_call.

You can also use call but that will not raise any error if you have a non-zero return code which may or may not be desirable

like image 70
Padraic Cunningham Avatar answered Sep 17 '22 13:09

Padraic Cunningham