2

I was working on Flask microservice and I needed to rewrite it with FastAPI but I had a problem that I had not with Flask.

The problem is that I send a request which is not reaching the server indefinitely. To unstuck the request for example with Postman or Hoppscotch I cancel the request and I resend the same one and it works perfectly. I also tried with curl and I cannot reproduce the problem, the request works at the first time.

I am using uvicorn and I tried with other ASGI servers and I reproduce the problem with Hypercorn. However, I tried to reproduce it with Daphne and it works.

I have no error messages because the request is just stuck and the server never catch it.

I cannot provide a minimal reproducible example as I wasn't able to create one. In fact I initialized some FastAPI projects and none of them had this problem. The project where I have this bug has a lot of background code and using multiprocessing and threading, maybe that is the cause of the problem ?

I tried many things to understand what can cause the problem. Do you have any ideas of what can produce what I have described above ?

brilleta
  • 21
  • 2
  • I experienced a similar issue when creating processes via `background_tasks`. More info: https://github.com/tiangolo/fastapi/issues/1313 – Teejay Bruno Mar 15 '21 at 21:34

1 Answers1

1

Asyncio is mainly thought as a single threaded program that can switch tasks because of idle moments, e.g. waiting for I/O.

On medium there's a nice article that explains the differences between asyncio and multithreading and how to achieve the latter on the former.

https://medium.com/analytics-vidhya/asyncio-threading-and-multiprocessing-in-python-4f5ff6ca75e8

That being said, with no source code, it's difficult to say where the problem could lie. If all the other projects work, while your doesn't, it's either your configuration/setup or some part of code you've added.

PS Sometimes I had a similar problem on windows while never on linux. It happened when there was an error on startup, which did not depend on my code. Though I'm not experiencing it anymore...maybe with the newer releases it got fixed. This may be the root cause of your problem, but I can't tell with no code.

lsabi
  • 3,641
  • 1
  • 14
  • 26
  • Thank you for your answer. For the moment, I don't use async def syntax because in the FastAPI documentation they say that "if you do not know, use normal def." – brilleta Mar 16 '21 at 09:00