将多个函数传递给回调函数

时间:2017-05-23 14:34:30

标签: python concurrent.futures

鉴于:

with concurrent.futures.ProcessPoolExecutor(max_workers=(2*multiprocessing.cpu_count()+1)) as executor:
        for netelement in DOC['code']['info']['dev']:
            job = executor.submit(bgp_communities.do_lookup, netelement)
            job.add_done_callback(functools.partial(bgp_communities.do_data_wrangling, DOC))

是否可以将第二个函数(如bgp_communities.do_data_wrangling)作为参数传递给future的回调?

1 个答案:

答案 0 :(得分:0)

你的问题不是很清楚。但是,如果您希望在完成作业时调用多个函数,只需再次调用job.add_done_callback

with concurrent.futures.ProcessPoolExecutor(max_workers=(2*multiprocessing.cpu_count()+1)) as executor:
    for netelement in DOC['code']['info']['dev']:
        job = executor.submit(bgp_communities.do_lookup, netelement)
        job.add_done_callback(functools.partial(bgp_communities.do_data_wrangling, DOC))
        job.add_done_callback(...)

请参阅docs

  

add_done_callback(FN)

     

将可调用的fn附加到将来。当未来被取消或完成运行时,将以未来作为唯一参数调用fn。

     

添加的callables按照添加的顺序调用,并且始终在属于添加它们的进程的线程中调用。如果callable引发Exception子类,则会记录并忽略它。如果callable引发BaseException子类,则行为未定义。

     

如果未来已经完成或取消,将立即调用fn。

相关问题