-
Describe the bugI have a route operation that is running some sync code, therefore, as the docs says, I'm defining it as Googling it showed that this occurs when people mistakenly bring up two event loops and not reusing the same one, I wonder how could it happened in my case if each request is running in a different thread with a different event loop To ReproduceSteps to reproduce the behavior with a minimum self-contained file. see https://github.com/Hedingber/fastapi-bug Expected behaviorAll requests should return 200 status code Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
|
Beta Was this translation helpful? Give feedback.
-
|
@phy25 Thanks a lot for the fast response ! After reading #1066, #825 and this post looks like the right way to go is to change the path operation to be async ( Indeed my use of Anyways I suggest to add to the async related docs explanation on what's the way to go when you have both sync and async code in your path operation |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @phy25 ! 🙇 Thanks for reporting back and closing the issue @Hedingber 👍 |
Beta Was this translation helpful? Give feedback.
@phy25 Thanks a lot for the fast response !
tl;dr changed to
run_in_threadpoolinsideasync def- worked !After reading #1066, #825 and this post looks like the right way to go is to change the path operation to be async (
async def) and userun_in_threadpoolfor the sync code.I've verified it to be working both for the minified example, and for my production system.
Indeed my use of
asyncio.runstarts a separate loop, TBH my first try was to doasyncio.get_running_loop()but I got an exception claiming there's no running loop, so I thought that since I'm in a different thread I need to start a new loop.I can't say I fully understand what's going on under the hood here that causes this …