Reputation: 191
I'm trying to write a process in Python which runs indefinitely and spawns exactly "n" number of processes in background which run some python code. I do not care for the output/result of these background processes, but would like to know when any of them terminate so that I always have "n" processes running. Any ideas how I can go about this or some reference material that could help?
Thanks & Regards, Azeem
Upvotes: 1
Views: 127
Reputation: 691
I would use a multiprocessing.Pool for this type of work. You can create a pool with a given number of worker processes and assign python functions to them to be run.
from multiprocessing import Pool
import time
def f(x):
print(x)
time.sleep(1)
print("finished", x)
pool = Pool(processes=4)
for x in xrange(10):
pool.apply_async(f, [x])
pool.close()
pool.join()
Returns the following:
0
1
2
3
('finished', 0)
('finished', 1)
('finished', 2)
('finished', 3)
4
5
6
7
('finished', 5)
('finished', 7)
('finished', 4)
('finished', 6)
8
9
('finished', 8)
('finished', 9)
Upvotes: 3