Nirmal Agarwal
Nirmal Agarwal

Reputation: 205

python subprocess

Suppose I have 4 resources which I want to utilize. I create 4 subprocesses and then wait for 1 of them to be free and as soon as one of them is free I utilize the freed resource again.

I want to know if there is any way we can find out which subprocess has finished working. As of now I am storing all the pid's of children in a list and using for loop to iterate.

for p in ps:
   p.wait()

But this blocks the application until the 1st process has finished. My objective is to wait until any of the subprocesses are finished. Please let me know any ideas.

Upvotes: 1

Views: 1182

Answers (3)

drone115b
drone115b

Reputation: 208

In python 3.2.x you can use concurrent futures. A very simple interface which implements the processing engine you described with either processes or threads. (Of course threads are subject to the GIL issue, but in 3.2.x the GIL penalty is apparently far less.) All you do is supply the function that will access the resource of interest.

Upvotes: 1

zwol
zwol

Reputation: 140445

Retrieve the process ID from the pid field of each subprocess object in your list, and create a dictionary mapping those process IDs back to the subprocess objects. Then call os.wait, which will wait for any of the subprocesses to exit, and use your dictionary to map the PID returned back to the appropriate subprocess. For finer control use os.waitpid or os.wait3 or os.wait4 (the Python documentation doesn't really explain how these work, but AFAIK they map directly to the waitpid and wait4 system calls, so you can use the documentation for those as a guideline).

Upvotes: 3

robert
robert

Reputation: 34398

Use p.poll(). It will tell you if the process has terminated.

Upvotes: 2

Related Questions