How to use Background Tasks? #4107
-
|
Sorry, I’m just an AI engineer who has recently started trying to set up a server, so maybe I’ve asked a silly question. Please bear with me. I have an API that initiates a PyTorch training process based on the data sent from the frontend. Later, I realized that this locks up the backend, making it impossible for other APIs to be accessed. I looked into the official documentation, and it seems that using BackgroundTasks might be the correct solution? Like this? Is this the correct way to tackle long cpu-lock tasks? @post("/upload")
async def upload_file(
data: UploadFile = Body(media_type=RequestEncodingType.MULTI_PART, default=None)
) -> Response[dict[str, str]]:
return Response({
"status": "success",
}, background=BackgroundTask(train, max_epochs=10000, data_path=cleaned_file_path))I’d also like to ask for another suggestion. On my server, I first preprocess the data and then run the training. From this perspective, the preprocessing part should also be implemented as a Background Task. I can split it into two APIs, where the frontend controls when to preprocess and when to train—I understand that concept. Alternatively, I could use a single API with two background tasks, passing them as a list. (If my understanding of Background is correct) That seems feasible to me as well. My question is: Can I directly call one API from another API? Like this: @post("/a")
async def a()
@post("/b")
async def b()
# call a hereIs above code a silly choice? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
No, it is not. I recommend reading this article from our docs: https://docs.litestar.dev/latest/topics/sync-vs-async The background tasks still run in the same process as your application, so for CPU bound tasks, you're not gaining anything. For long running / compute heavy tasks, you want to offload the process to a task queue, like celery. |
Beta Was this translation helpful? Give feedback.
No, it is not. I recommend reading this article from our docs: https://docs.litestar.dev/latest/topics/sync-vs-async
The background tasks still run in the same process as your application, so for CPU bound tasks, you're not gaining anything.
For long running / compute heavy tasks, you want to offload the process to a task queue, like celery.