Created
September 24, 2020 03:21
-
-
Save ixuuux/9bfc789c70e375ac385b86604c93ee06 to your computer and use it in GitHub Desktop.
concurrent.futures.thread.ThreadPoolExecutor线程池添加回调
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import time | |
from concurrent.futures import Future | |
from concurrent.futures.thread import ThreadPoolExecutor | |
thread_pool = ThreadPoolExecutor(max_workers=5) | |
def task(a: int, b: int) -> int: | |
time.sleep(1) | |
return a * b | |
def result(future: Future): | |
res = future.result() | |
# todo 拿到结果做其它工作 | |
print(res) | |
if __name__ == '__main__': | |
thread_pool.submit(task, 30, 30).add_done_callback(result) | |
# 会打印 600 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment