Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requests post not working with long processing time. #6575

Closed
luisbnzsa opened this issue Nov 16, 2023 · 1 comment
Closed

Requests post not working with long processing time. #6575

luisbnzsa opened this issue Nov 16, 2023 · 1 comment
Labels
actions/autoclose-qa Used for automation to auto-close an issue Question/Not a bug

Comments

@luisbnzsa
Copy link

Hello.

I am creating server API using FASTAPI/Uvicorn.

I am processing files with big AI models that are slow to process. When the file is lower than 10 Mb, everything works fine. But when longer, it implies that it will take more than 3 minutes. By doing the request post, on large files, the post does not get the response. The server continues as the process and shows 200 OK. So on the server side, everything seems fine but the request keeps running and does not get the ok code neither the data back. If I don´t add a timeout it will keep forever. If I add the timeout, it gives the error because it did not get the response.
I am running the server like this
uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload --timeout-keep-alive 600 --ws-ping-timeout 500
Adding a timeout keep alive of 600 seconds to make sure it can handle big files.

Now when I do the request, it won´t work. Then I tested this to check when it loses connection

@app.post("/pru/") async def pru(): print("Comenzamos") time.sleep(275) return "Devolucion " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
After making a post request to /pru/ using a sleep timer, I found out that 275 seconds is the time threshold when requests loses connection with the server and does not get anything back. Anything lower works fine.

Now, if I change the post to get, and I do it on Google Chrome browser, it works fine, even with 500 seconds.

Does anyone knows why the requests loses connection with the server or why it does not receives the message back from the server?

I investigated more, and some people recommended using TCPKeepAliveAdapter and stream = True. but that works with GET but not with POST. I have it like this.

keep_alive = TCPKeepAliveAdapter(idle=5, count=60, interval=10) s = requests.Session() s.mount("http://20.30.30.30:8080/aupro/tran-d/", keep_alive) r = s.post(url="http://20.30.30.30:8080/aupro/tran-d/", files=archivo, stream=True,timeout=400)

Any advise? I don´t know what else to try.

Thanks in advance.

@luisbnzsa luisbnzsa added actions/autoclose-qa Used for automation to auto-close an issue Question/Not a bug labels Nov 16, 2023
@luisbnzsa
Copy link
Author

At the end the solution I found, if anyone needs it, was not on the client side but in the server side. Something underneath is closing the connection after 275 seconds, so the solution was to create function in a threding.Thread, while the process is alive. It sends text messages every 30 seconds. In that way, both sides know that the connection is alive. I tested with a 5 minutes process. I used StreamingResponse to make it happen.

que = queue.Queue()
def director():
    # Esta funcion se utiliza para poder enviar mensajes periodicamente cada 30 segundos
    # es necesaria para mantener la conexión activa con el cliente en archivos que requeren 
    # más de 270 segundos para procesar
    resultado = ""
    t = Thread(target=lambda q, arg : q.put(trandia(arg)), args=(que, resultado,))
    t.start()
    while t.is_alive():
        t.join(30)
        yield "#processing...#"
    
    yield que.get()
    
return StreamingResponse(director(), media_type='text/event-stream')

@sigmavirus24 sigmavirus24 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actions/autoclose-qa Used for automation to auto-close an issue Question/Not a bug
Projects
None yet
Development

No branches or pull requests

3 participants
@sigmavirus24 @luisbnzsa and others