前景提要
先上代码 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 不适用这种场景,那么我应该怎么办呢