Hello, I am trying to use server-side cursors wit...
# ask-community
c
Hello, I am trying to use server-side cursors with prefect-sqlalchemy and am unable to get it to work. Code fragment:
Copy code
query = f"SELECT * FROM `{table}`"
    print(f"Starting DB fetch")
    with conn_obj.get_engine().connect() as connection:
        print(f"Got DB connection")
        with connection.execution_options(yield_per=yield_per).execute(query) as result:
            print("Ran DB Query")
            print(f"sending block partition")
            time.sleep(1)
            for block in result:
                p_in.send(block)
...
fails with error
Copy code
AttributeError: __enter__
This type of error suggests that context manager is not supported - but it it is if SQLAlchemy is version 2. But my
pip freeze
reports
prefect-sqlalchemy==0.3.2
Can someone please assist? 1. What version of SQLAlchemy is prefect-sqlalchemy built over? 2. How do I do server-side cursors with Prefect SQLAlchemy?
1
n
hi @Cormac - i think the issue is just that its an async context manager instead of a sync one
c
@Nate thanks - very helpful! how do I force sync behaviour?
n
hmm I'm not sure that's not something I would recommend. the context manager as it exists in
prefect-sqlalchemy
is implemented to be async if you want/need sync behavior, I might recommend: • using
sqlalchemy
directly • contributing a sync interface to
prefect-sqlalchemy
c
@Nate: let me ask a different way: how do I force a server-side cursor, which I require in order to support large table ELT...?
@Nate I can confirm I am not using an Async engine. so its not going to be the async context manager case....
n
the point I'm trying to make is that its async regardless see this union type? the context manager is async regardless of the engine
what you might be able to do is use
@sync_compatible
to write an async function that you can use as if it were a sync function like this one
a
@sync_compatible
decorated function will behave like an async function in an async context (like an
async
fn definition, or anywhere with a running event loop) but will behave sync otherwise (no need to await)
c
@Nate ok - thanks for the extra pointers. I'll have a look at what I can do.
n
no worries! let me know if you have trouble
1