How to create an endpoint with fastapi, I have experience creating endpoints with the post method...but lets say that I have a long process. 20+ minutes, so I cannot maintain the connection live for too much this cause Cloudflare limitations, so I was thinking in create an endpoint with the post method, but also another endpoint with the get method so I can consult the system if the process has finished...lets say every minute and when the process is finished maybe download the result from the cloud...or well I don't have completely clear this part maybe you can clarify this part for me...but what do you think is plausible?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
from fastapi import FastAPI, BackgroundTasks
from celery import Celery
app = FastAPI()
# Celery configuration
celery = Celery("tasks", broker="amqp://guest:guest@localhost//")
@celery.task
def long_running_task(task_id):
# Simulate a 20-minute task
import time
time.sleep(1200) # 20 minutes
# Store the result in cloud storage (e.g., AWS S3)
# For demonstration purposes, we'll just store it in a file
with open(f"result_{task_id}.txt", "w") as f:
f.write("Task completed!")
@app.post("/initiate-task/")
async def initiate_task(background_tasks: BackgroundTasks):
task_id = "some_task_id"
background_tasks.add_task(long_running_task, task_id)
return {"task_id": task_id, "status": "initiated"}
@app.get("/task-status/{task_id}")
async def check_task_status(task_id: str):
# For demonstration purposes, we'll just check if the result file exists
import os
result_file = f"result_{task_id}.txt"
if os.path.exists(result_file):
return {"task_id": task_id, "status": "completed"}
else:
return {"task_id": task_id, "status": "running"}
@app.get("/download-result/{task_id}")
async def download_result(task_id: str):
# For demonstration purposes, we'll just return the result file
import os
result_file = f"result_{task_id}.txt"
if os.path.exists(result_file):
with open(result_file, "rb") as f:
return {"file": f.read()}
else:
return {"error": "Result not found"}
Run the application
bash
uvicorn main:app --host 0.0.0.0 --port 8000
Celery worker
Run a Celery worker in a separate terminal:
bash
celery -A main.celery worker --loglevel=info
Example usage
Initiate a task: curl -X POST http://localhost:8000/initiate-task/
Check task status: curl -X GET http://localhost:8000/task-status/<task_id>
Download result: curl -X GET http://localhost:8000/download-result/<task_id>
This approach allows you to handle long-running tasks without blocking the HTTP connection. The client can poll the task status endpoint to check if the task is completed, and then download the result when it's available.
Cloud Storage Integration
To integrate with cloud storage, you can modify the long_running_task function to upload the result to the cloud storage instead of storing it in a file. For example, with AWS S3:
python
import boto3
s3 = boto3.client("s3")
@celery.task
def long_running_task(task_id):
# Simulate a 20-minute task
import time
time.sleep(1200) # 20 minutes
# Store the result in AWS S3
s3.put_object(Body="Task completed!", Bucket="my-bucket", Key=f"result_{task_id}.txt")
Then, modify the download_result endpoint to return the result from the cloud storage: