Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 异步-读取sql和异步IO?_Python_Pandas_Python Asyncio_Python 3.7 - Fatal编程技术网

Python 异步-读取sql和异步IO?

Python 异步-读取sql和异步IO?,python,pandas,python-asyncio,python-3.7,Python,Pandas,Python Asyncio,Python 3.7,有人能告诉我如何解决以下问题的正确方向吗。我正在尝试使用pandas.read\u sql和asyncio来提出一个解决方案。我想将表记录从一个数据库迁移到另一个数据库 我想做以下工作: table 1 . . . table n 我有以下功能: def extract(table): try: df = pd.DataFrame() df = pd.concat( [chunk for chunk in

有人能告诉我如何解决以下问题的正确方向吗。我正在尝试使用pandas.read\u sql和asyncio来提出一个解决方案。我想将表记录从一个数据库迁移到另一个数据库

我想做以下工作:

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\u sql是否发布了。异步是一个硬要求吗?你考虑过线程或多处理吗?是的,但也许我能对使用线程或多处理有一些想法。但是我听说使用这些方法可能会出现很多问题。即使异步IO是一个硬要求,基于异步IO的解决方案仍然会使用后台线程并行运行
DataFrame.read\u sql
。记住这一点,最好使用
concurrent.futures
,它为并行化代码提供了极好的工具。有没有办法在python代码中检查或发布它?例如:在获取时:释放GIL以使其他函数并行运行?另一个问题:您是否可以在ThreadPoolExecutor上立即管理结果,或者您是否需要等到所有提取(tab1…tabn)完成?@Maki您无法在Python中释放GIL,但C扩展可以在安全时释放GIL。(Panda的作者就是这样的。)如果你需要在结果到达时对其进行管理,请查看executor的方法。它返回一个可通过多种方式管理的函数,包括注册一个回调,在结果准备好时执行。@Maki还看到了一个迭代器,它接受一组未来,并在它们完成时生成它们。