Python一次将多个查询发送到PostgreSQL数据库

时间:2019-01-10 13:04:17

标签: python postgresql

我有3个表,我想一次发送3个查询。我该怎么办?目前,我使用Pool().imap()进行此操作,但我认为我将改用多线程。我对么?

这是我的代码:

from sqlalchemy import create_engine

def pull_data(timeframe, sql_to_format):
    return pd.read_sql(
        sql_to_format.format(tf=timeframe),
        create_engine("postgresql+psycopg2://...")
    )


if __name__ == '__main__':
    pool = Pool(processes=3)
    raw = pool.imap(partial(
        pull_data,
        sql_to_format="SELECT * FROM xyz.abc_{tf}"),
        ['1991', '1992', '1993'])
    pool.close()

编辑

好的,这就是我的解决方法:

class myThread (threading.Thread):
    def __init__(self, tf):
        threading.Thread.__init__(self)
        self.tf = tf

    def run(self):
        sql_to_format = f'SELECT * FROM xyz.abc_{tf}'
        print(func_to_execute_query(sql_to_format).shape)


if __name__ == '__main__':
    threads = [myThread(x) for x in [f'199{i}' for i in range(1,4)]]

    for th in threads:
        th.start()

    for th in threads:
        th.join()

还有更好的更快方法吗?

0 个答案:

没有答案