如何使用SQLAlchemy引擎同时执行多条postgreSQL语句?

How to use SQLAlchemy engine to execute multiple postgreSQL statements at once?

我正在尝试生成要执行的 SQL 语句列表,但我认为当前代码会逐一提交它们:

    for batch in batches: # batch is a list of dictionary objects, batches is a list of lists
                
        for obj in batch: # for dictionary object in list
            
            with engine.connect() as connection:
                overwrite_failure = context['retry_failure'] == True and obj['status'] in ['success', 'critical failure']
    
                if overwrite_failure:
                    # load_deletes returns a string query which is escaped and formatted by .execute()
                    query_one, query_two = load_deletes(obj, table_name, columns, unique_cols, should_overwrite, identifier)
                    
                    connection.execute(query_one, obj)
                    connection.execute(query_two, obj)
                    
                else:
                  # load_updates returns a string query which is escaped and formatted by .execute()
                    query = load_updates(obj, table_name, columns, unique_cols, should_overwrite)
                    connection.execute(query, obj)

在循环外初始化数据库连接。还要确保 auto_commit 设置已关闭。