在sqlalchemy中并行运行查询

2024-06-28 20:35:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在数据库a中有一个表(比如表_a),在数据库B和C中还有两个表。我运行的查询如下:

session.query(Table_A).filter(Table_A.date >= time1, InventoryLog.audit_date < time2)

我在一个循环中运行它,在表\u B中添加行,并在每次迭代中更新time1、time2。在

现在,我想在同一个(或不同的表)上以不同的时间间隔并行运行另一个查询,并在另一个表(循环)中插入。重要的一点是并行运行它们。在

我该如何在sqlalchemy中进行呢?我应该用芹菜来做这个,还是那会是一个过度的杀戮?在

提前谢谢!在


Tags: 数据库date间隔sqlalchemysession时间tableaudit
1条回答
网友
1楼 · 发布于 2024-06-28 20:35:06

可以使用线程进行并行处理。在

from threading import Thread

def fn1(arg1):
    while True:
        session.query(Table_A).filter(Table_A.date >= time1, InventoryLog.audit_date < time2)
        # do other stuff


def fn2(arg1):
    while True:
        session.query(Table_B).filter(Table_B.date >= time1, InventoryLog.audit_date < time2)
        # do other stuff


def fn3(arg1):
    while True:
        session.query(Table_C).filter(Table_C.date >= time1, InventoryLog.audit_date < time2)
        # do other stuff


t1 = Thread(target=fn1, args=(1,))
t2 = Thread(target=fn2, args=(1,))
t3 = Thread(target=fn3, args=(1,))

t1.start()
t2.start()
t3.start()

相关问题 更多 >