How can I return a 202 Accepted response for long running REST call? #8323
-
|
Hope this is the right place for a question. I have a REST API post request that does a lot of computation, Is it possible to modify the response status code, for example, similar to this approach in Sanic? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
|
@danieljfarrell from fastapi import FastAPI
from pydantic import BaseModel
from uuid import uuid4
from collections import deque
from starlette.status import HTTP_201_CREATED, HTTP_202_ACCEPTED
# see https://github.com/encode/starlette/blob/master/starlette/status.py
queues = dict(
pending=deque(),
working=deque(),
finished=deque()
)
def get_job(id: str, queue: str = None, order=['finished', 'working', 'pending']):
_id = str(id)
# if queue:
# order = [queue]
for queue in order:
for job in queues[queue]:
# print(job['id'])
if str(job['id']) == _id:
return job, queue
return 'not found', None #this will fail horribly
app = FastAPI()
class BaseJob(BaseModel):
data: bytes = None
class JobStatus(BaseModel):
status: str
id: str
data: bytes = None
result: int = -1
@app.get("/jobs/{id}", response_model=JobStatus, status_code=HTTP_202_ACCEPTED)
async def read_job(id: str):
job, status = get_job(id)
d = dict(
id=job['id'],
status=status,
data=job['data'],
result=job.get('result', -1)
)
return d
@app.post("/jobs/", response_model=JobStatus, status_code=HTTP_201_CREATED)
async def create_job(*, job: BaseJob):
_job = dict(
id=str(uuid4()),
status='pending',
data=job.data
)
queues['pending'].append(_job)
return _job |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @rcox771 for the thorough response! Sorry for the delay @danieljfarrell , I had planned to create a tutorial in the docs for these cases (it will come in the next days nevertheless). First, to customize the status code, in general, you can do as @rcox771 says. In the path operation decorator with the Copying from @rcox771's example: ...
@app.get("/jobs/{id}", response_model=JobStatus, status_code=HTTP_202_ACCEPTED)
async def read_job(id: str):
# your code here
passThe But when you don't remember exactly which status code is for what (as frequently happens to me), you can import the variables from About background jobs, as FastAPI is fully based on Starlette and extends it, you can use Starlette's integrated background tasks. It is not in FastAPI's docs yet, but will come soon. For these Starlette's background tasks to work in FastAPI you need to return a In more complex scenarios, when you need distributed task workers, possibly in several servers, with different dependencies (for example, one worker might need I hope to add that to the tutorials too, but meanwhile, you can see how to set it all up with the full-stack project generator, it includes Celery: https://github.com/tiangolo/full-stack-fastapi-couchbase |
Beta Was this translation helpful? Give feedback.
-
|
Thanks so much! This project is so cool and the documentation is amazing. |
Beta Was this translation helpful? Give feedback.
-
|
That's great to hear! Thanks @danieljfarrell ! The status code docs are live now: https://fastapi.tiangolo.com/tutorial/response-status-code/ I still owe you the docs for background tasks. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks! What’s the history of this project? It seems to have come from nowhere to awesome in a few weeks from viewing the commit history. |
Beta Was this translation helpful? Give feedback.
-
|
@danieljfarrell here's a bit of history, now in the docs: https://fastapi.tiangolo.com/history-design-future/ And I quoted you 😁 |
Beta Was this translation helpful? Give feedback.
-
|
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
Beta Was this translation helpful? Give feedback.


@danieljfarrell here's a bit of history, now in the docs: https://fastapi.tiangolo.com/history-design-future/
And I quoted you 😁