简体   繁体   中英

Stopping the processes spawned using pool.apply_async() before their completion

Suppose we have some processes spawned using pool.apply_async(). How can one stop all other processes when either one of them returns a value? Also, Is this the right way to get running time of an algorithm? Here's the sample code :-

import timeit
import multiprocessing as mp

data = range(1,200000)

def func(search):
    for val in data:
        if val >= search:
            # Doing something such that other processes stop ????
            return val*val 

if __name__ == "__main__":
    cpu_count = mp.cpu_count()
    pool = mp.Pool(processes = cpu_count)

    output = []

    start = timeit.default_timer()

    results = []
    while cpu_count >= 1:
        results.append(pool.apply_async(func, (150000,)))
        cpu_count = cpu_count - 1

    output = [p.get() for p in results]
    stop = timeit.default_timer()
    print output

    pool.close()
    pool.join()

    print "Running Time : " + str(stop - start) + " seconds"

I've never done this, but python docs seems to give an idea about how this should be done.

Refer: https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process.terminate

In your snippet, I would do this:

while cpu_count >= 1:
        if len(results)>0:
            pool.terminate()
            pool.close()
            break
        results.append(pool.apply_async(func, (150000,)))
        cpu_count = cpu_count - 1

Also your timing method seems okay. I would use time.time() at start and stop and then show the subtraction because I'm used to that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM