请问 Python 中的 ThreadPoolExecutor 嵌套使用时外层如何能不等待内层结果并直接返回 - V2EX
推荐学习书目
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
Hiyokunotori

请问 Python 中的 ThreadPoolExecutor 嵌套使用时外层如何能不等待内层结果并直接返回

  •  
  •   Hiyokunotori Jul 22, 2020 2331 views
    This topic created in 2120 days ago, the information mentioned may be changed or developed.

    先上代码

    from concurrent.futures import ThreadPoolExecutor import time from loguru import logger def func1(x): time.sleep(2) return x def func2(): my = [] exe = ThreadPoolExecutor() for r in exe.map(func1,range(10)): my.append(r) exe.shutdown() logger.info(my) def func3(): exe = ThreadPoolExecutor(1) exe.submit(func2) # 这里我想先返回 1,然后异步执行函数 haha2,但是我如果设置了 wait 为 False 的话 haha2 就不会执行, # 不设置的话得等到 haha2 执行完毕才能返回 1,我该怎么做 exe.shutdown(wait=False) return 1 if __name__ == '__main__': res = func3() print(res) 

    我现在的需求是有三个函数,函数 2 中会使用 ThreadPoolExecutor 去执行函数 1(为了提升效率),函数 3 中会执行函数 2 并返回一个值,但是我想函数 3 先返回值就给一个 ThreadPoolExecutor(1)并且设置 wait 为 False,但是这样函数 2 并没有执行,请问我该如何实现我得目的。。。还是说 ThreadPoolExecutor 不适用这种场景,那么我应该怎么办呢

    2 replies    2020-07-22 16:28:09 +08:00
    xiaolinjia
        1
    xiaolinjia  
       Jul 22, 2020
    def func3():
    import threading
    t = threading.Thread(target=func2)
    t.start()
    return 1
    ToughGuy
        2
    ToughGuy  
       Jul 22, 2020
    直接把 futures return 回去

    def func2():
    ----....
    ----returun exe.map(func1,range(10))
    About     Help     Advertise     Blog     API     FAQ     Solana     3360 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 31ms UTC 13:45 PVG 21:45 LAX 06:45 JFK 09:45
    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