5

I want to create many processes, each process runs 5 seconds later than a previous process, namely, the time interval between each process starts is 5 seconds, so that: run process 1 wait 5 seconds run process 2 wait 5 seconds run process 3 wait 5 seconds .....

like:

        for i in range(10):
            p = multiprocessing.Process(target=func)
            p.start()
            sleep(5)
        #after all child process exit
        do_something()

but I want to call do_something() after all the process exit I don't know how to do the synchronization here

with a python pool libary, I can have

    pool = multiprocessing.Pool(processes=4)
    for i in xrange(500):
            pool.apply_async(func, i)
    pool.close()
    pool.join()
    do_something()

but in this way, 4 processes will run simultaneously, I can't decide the time interval between processes, is it possible to create a process pool and then fetch each process, something like

pool = multiprocessing.Pool(processes=4)
for i in xrange(500):
    process = pool.fetch_one()
    process(func, i)
    time.sleep(5)
pool.close()
pool.join()
do_something()

are there such a library or source code snippets which satisfy my needs? thanks

4

1 回答 1

0

只是将建议放在一起,您可以执行以下操作:

plist = []
for i in range(10):
    p = multiprocessing.Process(target=func)
    p.start()
    plist.append(p)
    sleep(5)
for p in plist:
    p.join()
do_something()

你可以给一个 timeout 参数join()来处理卡住的进程;在这种情况下,您必须继续遍历列表,删除终止的进程,直到列表为空。

于 2013-05-06T14:31:27.480 回答