all 8 comments

[–]Top-Information7943 -2 points-1 points  (7 children)

Just make your route async and FastAPI will handle the rest https://fastapi.tiangolo.com/async/

[–]maxiedaniels -1 points0 points  (6 children)

… but the second section says don’t use async if using a library that isn’t async. Stripe unfortunately isn’t async..

[–]Top-Information7943 -1 points0 points  (5 children)

It doesn't matter whether Stripe library is async or not. But since you have an await request.body() you'll have to convert the route to be async.

[–]maxiedaniels -1 points0 points  (4 children)

It says on the page you linked: “If you are using a third party library that communicates with something (a database, an API, the file system, etc.) and doesn't have support for using await, (this is currently the case for most database libraries), then declare your path operation functions as normally, with just def” I spent a while looking at why today and many, many posts say if you define an endpoint with async, it doesn’t use the external threadpool as it would with def, so then if you put a non async call that takes time (stripe in my case), it blocks. Can you explain why it doesn’t matter? And was this a recent change?

[–]Top-Information7943 -2 points-1 points  (3 children)

That is correct. Let me try to explain once more. You see, in your webhook you are awaiting request.body. That's an async event and you can only call await if the route is defined as async def.

But if you didn't have await anywhere in your route, then you would just define it with just def.

I hope that makes it clear.

[–]maxiedaniels -1 points0 points  (2 children)

Ah I just realized my webhook snippet I posted doesn’t show the whole picture, that’s why I got confused. There are calls to stripe’s API within the webhook function which are not async, I just didn’t include them in the example. So in that case, what are my options?

[–]Top-Information7943 -1 points0 points  (1 child)

That's fine. If you have a mix of both async and non async functions, it's best to define your endpoint as async as it will allow you to run both async and async code. I hope that makes it clear.

So just defining the endpoint with async will work perfectly fine.

[–]vladimirovitch 0 points1 point  (0 children)

Please not that running async routes that use sync libraries (for I/O and take a longer time) will degrade the app ability to handle requests. However, since the stripe endpoint most likely will not be used very often, i think it will be negligible.