如何制作一个 Python 并发 Future 以在 Futures 列表完成时发出信号?
How to make a Python concurrent Future that signals when a list of Futures has completed?
我有一个函数可以将多个任务提交给 ThreadPoolExecutor
和 return 每次提交创建的 Futures 列表:
def submit_tasks() -> [Future]:
futures = []
for i in range(10):
future = executor.submit(Task())
futures.append(future)
return futures
def submit() -> Future:
futures = submit_tasks()
# I would like this function to return a single Future that clients can use to check
# whether all futures in the list have completed. How to do that?
我在 Python 3.8
我想要这个函数 return 一个单一的未来,客户可以用它来检查列表中的所有未来是否已经完成。怎么做?
使用concurrent.futures.wait
:https://docs.python.org/3/library/concurrent.futures.html#module-functions
from concurrent.futures import Future, wait
...
def submit() -> Future:
return wait(submit_tasks())
我有一个函数可以将多个任务提交给 ThreadPoolExecutor
和 return 每次提交创建的 Futures 列表:
def submit_tasks() -> [Future]:
futures = []
for i in range(10):
future = executor.submit(Task())
futures.append(future)
return futures
def submit() -> Future:
futures = submit_tasks()
# I would like this function to return a single Future that clients can use to check
# whether all futures in the list have completed. How to do that?
我在 Python 3.8
我想要这个函数 return 一个单一的未来,客户可以用它来检查列表中的所有未来是否已经完成。怎么做?
使用concurrent.futures.wait
:https://docs.python.org/3/library/concurrent.futures.html#module-functions
from concurrent.futures import Future, wait
...
def submit() -> Future:
return wait(submit_tasks())