开发者

Multiprocessing in python - ensuring a minimum number of child processes are active

开发者 https://www.devze.com 2023-02-04 11:21 出处:网络
I want to ensure that a mininum number of child processes spawned are still active at any given time; the currently spawns as below:

I want to ensure that a mininum number of child processes spawned are still active at any given time; the currently spawns as below:

pool = Pool(processes=WORKERS)
pool.map(worker.run, range(WORKERS)) # pass proc开发者_运维百科ess a number as its ID

There are certain cases where the workers would return or crash and burn because of an error - i would like to spawn a new worker in this case. is this at all possible?

Thanks,


Here's a simple (dumb?) solution: periodically check all your workers, pop those that have returned and add new ones to the worker list (base code borrowed from Doug Hellman):

import multiprocessing, random, time

def worker(num):
    """thread worker function"""
    time.sleep(random.randint(1,10))
    print 'Worker:', num
    return

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,))
        jobs.append(p)
        p.start()

    while True:
        time.sleep(2)
        for n, p in enumerate(jobs):
            if not p.is_alive():
                jobs.pop(n)
                i += 1
                p = multiprocessing.Process(target=worker, args=(i,))
                jobs.append(p)
                p.start()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号