3

I'm building an app using asyncio. I will be using sqlalchemy as the orm. From what i understand scoped_session associates a session with a thread so they don't with each other's operations. Now since asyncio works on a single thread and I believe scoped_session will not work correctly which will cause problems. What would be the correct way to use sqlalchemy sessions with asyncio?

2 Answers 2

6

The answer depends on your code.

In general you can use sessionmaker for creating session factory and using it on your own.

If your code has implicit context you may use scoped_session with custom scopefunc as shown in example.

Implicit context may utilize asyncio.Task.current_task() call but you need to find a way for scoped session destruction also.

Sign up to request clarification or add additional context in comments.

3 Comments

What is the default behaviour for scoped_session destruction? Does it get destroyed when the thread is closed? Wouldn't it also in that case destroy when the task is finished?
scoped_session has no destructor, sorry. That's why you need signal subscription in the example above.
What kind of signal subscription do I need to provide in the scopefunc parameter of the scoped_session function, if I am using the aiohttp framework? @AndrewSvetlov
1

I don't think you can use sqlalchemy with asyncio. The SQL Alchemy API is all blocking and incompatible with asyncio. http://techspot.zzzeek.org/2015/02/15/asynchronous-python-and-databases/

You can use SQL Alchemy asynchronously with gevent though, as long as you can find a gevent-compatible driver for your database. And gevent greenlents map nicely to scoped_session.

EDIT: actually, it looks like you can use SQL Alchemy with asyncio as long as you can find an asyncio-compatible driver for your database, such as https://github.com/aio-libs/aiopg . As for scoped_session, use current_task and destroy it on your web framework's request end.

2 Comments

I'm not looking to make sqlalchemy async. What I want to do is keep the tasks from using the same session at the same time i.e. The tasks should not be sharing sessions.
Except you are assigning each tasks item to a new thread, using a normal sqlalchemy session i.e Session = sessionmaker(bind=some_engine) session = Session() might be all you need. You create it before the task ends and close it after the tasks is finished.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.