简体   繁体   中英

Non-blocking subprocess and catch the output of each subprocess

so I got this tricky situation which I need to execute few subprocess and be able to get each subprocess output. This is what I'm using atm:

output = Popen(cmd, stdout=PIPE, stderr=STDOUT)
output = output.communicate()[0]

Which works really well and also getting me errors, if there are any. Now I need to execute like 10 of those and be able to get the output of each one of them. I was wondering if there is a smooth way of doing that WITHOUT using multithreading.

subprocess.Popen starts the child process asynchronously, only communicate makes the operation synchronous.

So you just have to first launch all your children processes. Then depending on how you want to process their output, you can:

  • if you need to process output in a ordered manner, that is display output from cmd1, then cmd2, ... just use communicate or wait for each child command
  • if you need to process output as soon as it is available and you use a Linux or Unix-like platform, you could use the select module.
  • if you need to process output as soon as each command ends and want to support Windows platforms, you could just loop with subprocess.poll on child processes to know which have finished.
  • if you need more, you are likely to have to use threading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM