0

If I launch several of subprocesses in python (using Popen, for instance), how can I place the main thread into sleep mode until at least of them finished? I also need to put some timeout, if none of them finishes after 1min I want to record some log.

If I use p[i].wait(timeout) doesn't work because it will block until p[i] finish, hence if any other subprocess finishes (p[j] - for j!=i) my main thread wouldn't be notified.

What is the proper way to accomplish this?

rph
  • 901
  • 1
  • 10
  • 26
  • one way is [`signal.set_wakeup_fd()` on `SIGCHLD` on POSIX](http://stackoverflow.com/q/30087506/4279) that can awake a `select()`-based loop when a child process exits (on Windows, you would probably use `WaitForMultipleObjects()` and/or Job object). A more portable (less efficient) way is to use a [Condition object](https://docs.python.org/3/library/threading.html#condition-objects) (the main thread calls `cv.wait()` and child threads call `cv.notify()` when a subprocess exits.) – jfs Jul 01 '16 at 15:34
  • Note: [you don't need threads to run multiple processes](http://stackoverflow.com/q/14533458/4279) – jfs Jul 01 '16 at 15:39

0 Answers0