多处理代码在 while 循环中不起作用

问题描述 投票:0回答:1

周日快乐。

我有这个代码,我想使用多处理模块运行。但它不仅仅因为某种原因而起作用。

with ProcessPoolExecutor() as executor:
    while True:
        if LOCAL_RUN:
            print("ALERT: Doing a local run of the automation with limited capabilities.")

        list_of_clients = db_manager.get_clients()
        random.shuffle(list_of_clients)

        list_of_not_attempted_clients_domains = db_manager.tags()
        group_of_clients_handlers = {}

        # no matches
        if not list_of_not_attempted_clients_domains:
            sleep = 60 * 10
            pretty_print(f'No matches found. Sleeping for {sleep}s')
            time.sleep(sleep)
            continue

        for client in list_of_clients:
            client_id = client[0]
            client_name = client[1]
            group_of_clients_handlers[client_id] = [ClientsHandler(db_manager), client_name]

        #  MULTI-PROCESSING CODE
        try:
            print('running function...')
            executor.map(
                partial(run, group_of_clients_handlers=group_of_clients_handlers),
                list_of_not_attempted_clients_domains
            )
        except Exception as err:
            print(err)

尽管我尝试对此进行调试,但我不知道为什么这不起作用,尽管我觉得这与进程需要时间启动或调度任务等有关,但我不确定。

while 循环继续运行,我看到所有像

running function...
这样的打印语句,但 run 函数永远不会执行。 run 函数是一个非常大的函数,具有嵌套的大函数。

except 块也不会打印出任何错误。很想听听您的想法...

python multithreading oop parallel-processing multiprocessing
1个回答
0
投票

ProcessPoolExecutor.map
创建一个迭代器,必须消耗该迭代器才能获取异常,否则异常将被丢弃。

from concurrent.futures import ProcessPoolExecutor

def raising_func(val):
  raise ValueError(val)

with ProcessPoolExecutor(4) as pool:
  pool.map(raising_func, [1,2,3,4,5])

with ProcessPoolExecutor(4) as pool:
  list(pool.map(raising_func, [1,2,3,4,5]))  # < ---- exception is thrown from here
© www.soinside.com 2019 - 2024. All rights reserved.