1

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.

j0k
22.8k28 gold badges81 silver badges90 bronze badges
asked Feb 11, 2011 at 21:23
1
  • 3
    Are the subprocesses python? If so you should consider the multiprocessing Queue construct. Commented Feb 11, 2011 at 21:27

3 Answers 3

3

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).

answered Feb 11, 2011 at 21:35
Sign up to request clarification or add additional context in comments.

3 Comments

This is a more complicated solution compare to p.poll and os.wait is very much platform specific, so you'd have to handle wait differently for different OS.
It's not significantly more code than the loop, and it's the correct approach.
Thanks for the help. I am running in solaris 10 ... I will try this approach as this looks the best possible approach
2

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

answered Feb 11, 2011 at 21:32

3 Comments

This is a busywait (it does not yield the CPU), which should be avoided if at all possible.
@Zack For 4 parallel processes, I doubt you'll see any real performance difference compared to os.wait. Since he already has code with p.wait(), it's simpler to replace it with p.poll()
It's not about performance of this program, it's about wasting CPU cycles that other programs could have used (or could have been spent in deep idle).
1

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.

answered Jul 28, 2011 at 14:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.