Skip to content

Instantly share code, notes, and snippets.

@ixuuux
Created September 24, 2020 03:21
Show Gist options
  • Save ixuuux/9bfc789c70e375ac385b86604c93ee06 to your computer and use it in GitHub Desktop.
Save ixuuux/9bfc789c70e375ac385b86604c93ee06 to your computer and use it in GitHub Desktop.
concurrent.futures.thread.ThreadPoolExecutor线程池添加回调
# -*- 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