请教一下大佬多进程的超时问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
fmdxx1991

请教一下大佬多进程的超时问题

  •  
  •   fmdxx1991 Jun 10, 2021 2033 views
    This topic created in 1781 days ago, the information mentioned may be changed or developed.
    import time import random from multiprocessing import Pool def fun(name): print(f'{name} 运行') time.sleep(random.randrange(5,15)) print(f'{name} 运行结束') if __name__ == '__main__': pool = Pool(processes=2) names = ['anne','alice','biantai','haha'] for name in names: pool.apply_async(fun,(name,)) pool.close() pool.join() print('主线程') 

    我想要如果进程时间超过 10s 就结束掉,打印超时提示,然后让新进程执行,超时的进程放后面再执行,最后让所有的进程都在 10s 内成功运行完成,有什么优雅的方法吗?

    Supplement 1    Jun 17, 2021
    import multiprocessing from multiprocessing.dummy import Pool as ThreadPool from functools import partial import time import random def worker(name): begin = time.time() print(f'{name} 运行') time.sleep(random.randrange(5, 15)) end = time.time() print(f'{name} 运行结束,运行了{end - begin}秒') return end - begin def collectMyResult(result): pass # print("Got result {}".format(result)) def abortable_worker(func, *args, **kwargs): timeout = kwargs.get('timeout', None) name = kwargs.get("name",None) p = ThreadPool(1) res = p.apply_async(func, args=args) try: out = res.get(timeout) # Wait timeout seconds for func to complete. return out except multiprocessing.TimeoutError: print(f"{name}运行超时,已终止") raise if __name__ == "__main__": pool = multiprocessing.Pool(maxtasksperchild=1,processes=2) names = ["赵","钱","孙","李"] # list of arguments for name in names: abortable_func = partial(abortable_worker, worker, timeout=10,name=name) pool.apply_async(abortable_func, args=nae, callback=collectMyResult) pool.close() pool.join() 

    解决了超时就终止的问题了

    2 replies    2021-06-11 09:48:29 +08:00
    zhengxiaowai
        1
    zhengxiaowai  
       Jun 11, 2021
    https://docs.python.org/zh-cn/3/library/multiprocessing.html#using-a-pool-of-workers

    有 timeout 参数,会抛出异常,另外 "最后让所有的进程都在 10s 内成功运行完成" 你得有足够的进程数量
    fmdxx1991
        2
    fmdxx1991  
    OP
       Jun 11, 2021
    @zhengxiaowai 这个用 get 尝试过,发现同时运行 2 个子进程的时候,用 get(timeout)会堵塞主进程,直到超时才会执行下一步,这个就有问题了

    ``` python
    for name in names:
    p = pool.apply_async(fun,(name,))
    p.get(timeout=10)
    ```


    这样写的话,就是每次运行一个进程,然后等待超时,那就没必要用多进程了

    放外面的话, 也有很大的问题....
    About     Help     Advertise     Blog     API     FAQ     Solana     891 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 22:06 PVG 06:06 LAX 15:06 JFK 18:06
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86