3

I have a FastAPI app that runs on Gunicorn Webserver and the gateway interface is ASGI.

I try to simulate a response that takes a long time in my FastAPI App, and I expect that when I disconnect from the request connection currently under processing (e.g. close the tab), ASGI should abort or terminate the job in Gunicorn worker, while I found out that my FastAPI complete the processing anyway. See the logs below,

api          | TRACE:    HTTP connection made
api          | TRACE:    ASGI [4] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.0', 'server': None, 'client': None, 'scheme': 'http', 'root_path': '', 'headers': '<...>', 'method': 'GET', 'path': '/long-time', 'raw_path': b'/long-time', 'query_string': b''}
api-proxy    | 172.23.0.1 - - [12/Dec/2022:07:31:46 +0000] "GET /long-time HTTP/1.1" 499 "e9224322f5f65f94a54e4a6ca812ae72" generate 0 bytes in 3.051 "http://localhost:8000/docs" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" "-
api          | TRACE:    HTTP connection lost
api          | [2022-12-12 15:31:53,295] [INFO] [/router.py:810]: Ready to return
api          | TRACE:    ASGI [4] Receive {'type': 'http.disconnect'}
api          | TRACE:    ASGI [4] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
api          | TRACE:    ASGI [4] Send {'type': 'http.response.body', 'body': '<4 bytes>'}
api          | TRACE:    ASGI [4] Completed
async def my_func_1():
    """
    my func 1
    """
    await asyncio.sleep(10)

    return "zzzzzzzz"


@api_router.get("long-time")
async def root():
    """
    my home route
    """
    a = await asyncio.gather(my_func_1())

    fastapi_logger.info("Ready to return")
    
    return a

So I wonder if there's any configurable/programmable way to terminate the request processing, or send the response immediately rather than wait till the API endpoint function exits? Thanks.

Dayo Choul
  • 861
  • 2
  • 9
  • 23

0 Answers0