星期天快乐.

我想使用多处理模块运行这个代码.但它的作用不仅仅是出于某种原因.

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)

尽管我try 调试它,但我不知道为什么它不起作用,尽管我觉得这与进程需要时间启动或安排任务等有关,但我不确定.

while循环只是继续运行,我看到所有的print声明都像running function...一样,但run函数永远不会执行.run函数是一个非常大的函数,具有嵌套的大函数.

except块也不会打印出任何错误.很想听听你的 idea .

推荐答案

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

Python相关问答推荐

阅读Polars Python中管道的函数定义

在应用循环中间保存pandas DataFrame

如何在Python中使用io.BytesIO写入现有缓冲区?

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

如何根据日期和时间将状态更新为已过期或活动?

Pydantic 2.7.0模型接受字符串日期时间或无

带条件计算最小值

从numpy数组和参数创建收件箱

使用setuptools pyproject.toml和自定义目录树构建PyPi包

NumPy中条件嵌套for循环的向量化

Python+线程\TrocessPoolExecutor

当点击tkinter菜单而不是菜单选项时,如何执行命令?

Pandas GroupBy可以分成两个盒子吗?

为什么\b在这个正则表达式中不解释为反斜杠

基于行条件计算(pandas)

在Python中计算连续天数

为什么常规操作不以其就地对应操作为基础?

从列表中获取n个元素,其中list [i][0]== value''

导入错误:无法导入名称';操作';

PYTHON、VLC、RTSP.屏幕截图不起作用