栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python多处理帮助有条件退出

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python多处理帮助有条件退出

也许您正在寻找这样的东西?请记住,我正在为Python 3编写。上面的打印语句是Python 2,在这种情况下,应注意的是使用xrange而不是range。

from argparse import ArgumentParserfrom random import randomfrom subprocess import Popenfrom sys import exitfrom time import sleepdef add_something(i):    # Sleep to simulate the long calculation    sleep(random() * 30)    return i + 1def run_my_process():    # Start up all of the processes, pass i as command line argument    # since you have your function in the same file, we'll have to handle that    # inside 'main' below    processes = []    for i in range(100):        processes.append(Popen(['python', 'thisfile.py', str(i)]))    # Wait for your desired process result    # Might want to add a short sleep to the loop    done = False    while not done:       for proc in processes: returnpre = proc.poll() if returnpre == 90:     done = True     break    # Kill any process that are still running    for proc in processes:        if proc.returnpre is None: # Might run into a race condition here, # so might want to wrap with try block proc.kill()if __name__ == '__main__':    # Look for optional i argument here    parser = ArgumentParser()    parser.add_argument('i', type=int, nargs='?')    i = parser.parse_args().i    # If there isn't an i, then run the whole thing    if i is None:        run_my_process()    else:        # Otherwise, run your expensive calculation and return the result        returnpre = add_something(i)        print(returnpre)        exit(returnpre)

编辑:

这是一个使用多处理模块而不是子进程的更干净的版本:

from random import randomfrom multiprocessing import Processfrom sys import exitfrom time import sleepdef add_something(i):    # Sleep to simulate the long calculation    sleep(random() * 30)    exitpre = i + 1    print(exitpre)    exit(exitpre)def run_my_process():    # Start up all of the processes    processes = []    for i in range(100):        proc = Process(target=add_something, args=[i])        processes.append(proc)        proc.start()    # Wait for the desired process result    done = False    while not done:        for proc in processes: if proc.exitpre == 90:     done = True     break    # Kill any processes that are still running    for proc in processes:        if proc.is_alive(): proc.terminate()if __name__ == '__main__':    run_my_process()

编辑2:

这是最后一个版本,我认为它比其他两个版本要好得多:

from random import randomfrom multiprocessing import Poolfrom time import sleepdef add_something(i):    # Sleep to simulate the long calculation    sleep(random() * 30)    return i + 1def run_my_process():    # Create a process pool    pool = Pool(100)    # Callback function that checks results and kills the pool    def check_result(result):        print(result)        if result == 90: pool.terminate()    # Start up all of the processes    for i in range(100):        pool.apply_async(add_something, args=[i], callback=check_result)    pool.close()    pool.join()if __name__ == '__main__':    run_my_process()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626627.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号