首页 > 解决方案 > 如何处理 SQLAlchemy 断开连接?

问题描述

我正在使用 sqlalchemy 建立与 Presto 的连接,然后使用 dask 并行读取查询。问题是由于TimeoutError. 将 SQLAlchemy 引擎传递给 Pandas 时实现重试逻辑的最佳方法是什么?

代码示例:

import dask.dataframe as dd
from dask import delayed

def parallelize_query(date):
    engine = create_engine(f'presto://{user}:{password}@{host}:{port}/{catalog}/{schema}',
                                   connect_args={'protocol': 'https',
                                                 'requests_kwargs': {'verify': key}})

    q = query.format(date)
    data = pd.read_sql(q, conn)
    engine.dispose()
    return data

ddf = dd.from_delayed([delayed(parallelize_query)(d) for d in recent_dates])

错误:

ConnectionError: HTTPSConnectionPool(host='<host>', port=<port>): Max retries exceeded with url: /v1/statement/executing/20200427_231652_00087_rfnzc/yb90eab8fdeee4de5656afabc011dd8724e77bdcb/13761 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x913f95e90>: Failed to establish a new connection: [Errno 60] Operation timed out'))

标签: pythonpandassqlalchemydaskpresto

解决方案


推荐阅读