Single sqlalchemy session in tests and asyncio tasks in FastApi. This session is in 'prepared' state; no further SQL can be emitted within this transaction #11270
Replies: 2 comments
-
|
I think you need to create less complicated code example to get the answer.. Try to get rid of unnecessary things and give us code we can run or at least understand easily. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Closing this. Feel free to reopen it whenever you are ready to provide less complicated code example (MRE) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I have my fastapi app with sqlalchemy and tests for him. I can't attach minimal example code for reproduce it, but i understand problem. I'm attaching some code to help explain the problem.
I run all tests local sometime and I have error. And all my tests passed in CI always.
I use one session for test and for my app(This is convenient to keep the base clean). I have simple tests with send kafka message and check db. I use LifespanManager in my TestAsyncClient for run code after startup event. After that I create asyncio task and consume kafka messages and write to db. Since I run a asyncio task, I have a problem when a single session is used in different tasks.
I can add sleep before the request to the database in tests and this will solve the problem(this way there will never be a conflict within the same session). But it seems that the problem is fundamentally something else.
Maybe someone had similar problems and knows some kind of workaround?
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.104.1
Pydantic Version
2.6.1
Python Version
3.10.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions