Skip to content

FastAPI return empty string in time consuming task #3878

@rxy1212

Description

@rxy1212

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import os
import subprocess
from multiprocessing import Pool

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel


class Param(BaseModel):
    shell_path: str

# time consuming task !!!
def run(param: Param):
    subprocess.run(f'sh {param.shell_path}', shell=True)
    return 0


app = FastAPI()
pool = Pool(8)


@app.post('/faiss/action')
def yjy(param: Param):
    if not os.path.exists(param.shell_path):
        return {'code': 1, 'message': f'{param.shell_path} file not found!'}
    try:
        res = pool.map(run, [param])
    except Exception as e:
        return {'code': 2, 'message': f'shell execute error! {e}'}
    code = res[0]
    return {'code': code, 'message': 'success.'}


if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=16636)

Description

I try to build a time consuming task server with fastapi, But I found a strange question.
When I request my api /faiss/action with requests, fastapi return a empty string to requests after about 5 to 10mins.
At the begenning, it works right, requests block my client code (that's what i want), but after 5 to 10mins, fastapi just return a empty string to my client. That leads to a early end of my client, at the same time my time consuming task still run in server.
It's there a timeout mechanism in fastapi or How can I resolve this question?

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.68.1

Python Version

3.6.8

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions