简体   繁体   English

在sqlalchemy中并行运行查询

[英]Running queries in parallel in sqlalchemy

I have a table (say table_A) in a db A, there are two other tables in db's B and C. I am running query like : 我在数据库A中有一个表(例如table_A),在数据库B和C中还有另外两个表。我正在运行类似的查询:

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

I am running it in a loop, adding the rows in table_B and updating time1,time2 in each iteration. 我在循环中运行它,在table_B中添加行,并在每次迭代中更新time1,time2。

Now, I want to run another query in parallel on the same (or different table) for different time intervals and insert in another table_C (loop). 现在,我想在相同(或不同的表)上以不同的时间间隔并行运行另一个查询,并插入另一个table_C(循环)中。 The important point is to run them parallely. 重要的是并行运行它们。

How should I go about it in sqlalchemy? 我应该如何在sqlalchemy中进行处理? Should I use celery for this or will that be an overkill? 我应该用芹菜做菜吗?

Thanks in advance! 提前致谢!

You can use threading for paralel processing. 您可以将线程用于并行处理。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM