在Python多进程、多线程编程中,如果使用future和add_done_callback, 经常会遇到如何区分异步任务的问题。这里推荐两种方式。
方式一:给future新增属性
def call_back(future):
print(future.task)
with ProcessPool(...) as pool:
for task in get_tasks():
future = pool.schedule(...)
# 添加属性
future.task = task
# 或者setattr
# setattr(future, 'task', task)
future.add_done_callback(call_back)
方式二:使用偏函数(partial函数)
from functools import partial
def call_back(task, future):
# 这里可以获得task对象
pass
with ProcessPool(...) as pool:
for task in get_tasks():
future = pool.schedule(...)
done_callback = partial(task_done, task)
future.add_done_callback(call_back)