Async-Pandas Read_SQL和Ayncio? [英] ASYNC - Pandas read_sql and asyncio?

查看:27
本文介绍了Async-Pandas Read_SQL和Ayncio?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我指个方向,告诉我如何解决下面这个问题。我正在尝试使用Pandas、Read_SQL和Ayncio提出一个解决方案。我要将表记录从一个数据库迁移到另一个数据库。

我想做以下事情:

table 1
.
.
.
table n

我有这个功能:

def extract(table):
    try:
        df = pd.DataFrame()
        df = pd.concat(
              [chunk for chunk in
                  pd.read_sql(sql,
                              con=CONNECTION,
                              chunksize=10**5)]
                    )
    except Exception as e:
        raise e
    else:
        return df

我希望并行运行这些操作,而不是逐个运行。

extract(table1)
extract(table2)
.
.
extract(tablen)

推荐答案

Asyncio是关于将非阻塞代码组织到回调和协程中。并行运行CPU密集型代码是线程的一个用例:

from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as executor:
    frames = list(executor.map(extract, all_tables))

这是否真的会比顺序代码运行得更快取决于pd.read_sql是否释放GIL

这篇关于Async-Pandas Read_SQL和Ayncio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆