将多个函数传递给调用

2024-05-23 22:27:12 发布

您现在位置:Python中文网/ 问答频道 /正文

鉴于:

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的回调?在


Tags: datadocwithjobdoconcurrentmaxbgp
1条回答
网友
1楼 · 发布于 2024-05-23 22:27:12

你的问题不太清楚。但如果您希望在任务完成时调用多个函数,只需对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)

Attaches the callable fn to the future. fn will be called, with the future as its only argument, when the future is cancelled or finishes running.

Added callables are called in the order that they were added and are always called in a thread belonging to the process that added them. If the callable raises an Exception subclass, it will be logged and ignored. If the callable raises a BaseException subclass, the behavior is undefined.

If the future has already completed or been cancelled, fn will be called immediately.

相关问题 更多 >