question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLAlchemy using the same session across threads in the same async function

See original GitHub issue

Describe your question The question basically boils down to whether the following is safe, assuming there is a new SQLAlchemy session s being created every time this function is invoked:

async def func(s: Session):
  loop = asyncio.get_running_loop()
  await loop.run_in_executor(None, some_func, s)
  await loop.run_in_executor(None, some_other_func, s)
  ...

I’m thinking that it is safe because even though the session is used in (potentially) two different threads in the two run_in_executor calls, those two calls are not concurrent, since the second call won’t be executed until the first one completes. This means the session is not accessed concurrently. Am I right in thinking that?

I asked the same question on Stackoverflow here: https://stackoverflow.com/questions/65650198/sqlalchemy-using-the-same-session-across-threads-in-the-same-async-function. But I feel like I may be able to get a faster response here.

Thanks so much!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
zzzeekcommented, Jan 11, 2021

hey there -

yes your thinking is correct, the Session will be used in threads but as long as it isn’t used concurrently in more than one thread (or greenlet, or whatever), it should be fine.

if you are using the SQLite backend, there are some extra things to take into consideration, you probably have to set the “check_same_thread” flag to False, there’s some background on this at https://docs.sqlalchemy.org/en/13/dialects/sqlite.html#threading-pooling-behavior .

0reactions
ljiatucommented, Jan 11, 2021

Ok, thx for the explanation. I don’t think we are going to use the same async session or connection on different event loops once we adopt 1.4. From what I understand the web framework we are using won’t create two event loops.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLAlchemy using the same session across threads ... - GitHub
I'm thinking that it is safe because even though the session is used in (potentially) two different threads in the two run_in_executor calls ......
Read more >
python - SQLAlchemy using the same session across threads ...
I'm thinking that it is safe because even though the session is used in (potentially) two different threads in the two run_in_executor calls, ......
Read more >
SQLAlchemy connection pool within multiple threads and ...
Using multiple threads ​​ An object or a function is said to be thread-safe if concurrent access or modification by different threads is...
Read more >
Contextual/Thread-local Sessions — SQLAlchemy 1.3 ...
A Registry that can store one or multiple instances of a single class on the basis of a “scope” function.
Read more >
Python, SQLAlchemy, and Concurrency - Better Programming
Discusses coroutines, threads, parallelism, concurrency, asyncio, ... The takeaway here is that any function we denote with the async ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found