You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use Dask to process background jobs for my API, written in FastAPI. My current approach is:
Instantiating a local cluster on app startup.
fromcontextlibimportasynccontextmanagerfromdask.distributedimportLocalCluster@asynccontextmanagerasyncdeflifespan(app: FastAPI):
# Load the dask clusterapp.state.dask_cluster=LocalCluster(processes=False, asynchronous=True)
yield# Clean up the ML models and release the resourcesclose_call=app.state.dask_cluster.close()
ifclose_callisnotNone:
awaitclose_callapp=FastAPI(lifespan=lifespan)
Creating a client and using it to submit jobs each time a route is called. I wait for the jobs to finish and return a response to the client.
fromfastapiimportRequestfromdask.distributedimportClient, as_completed@router.post("/job",response_model=schemas.JobStartedConfirmation,)asyncdefstart_job(request: Request):
asyncwithClient(self.request.app.state.dask_cluster) asclient:
futures= []
future_job=client.compute(start_new_job())
futures.append(future_job)
# Do more stuff, wait the futures, and return a response to the client.
Obviously waiting for the jobs to complete before returning means it's not a background job yet. I want to make sure the API responds immediately and Dask stays running in the background. I am wondering what the best approach / pattern is for this:
Should I also start a Client on startup and share it between different calls on the route?
Has what I'm trying to do already been done before? Where can I find documentation for this?
If anyone is willing to share their own solutions for something similar, I would be very grateful. I can document this and contribute to the project if it's welcome.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello there,
I'm trying to use Dask to process background jobs for my API, written in FastAPI. My current approach is:
Obviously waiting for the jobs to complete before returning means it's not a background job yet. I want to make sure the API responds immediately and Dask stays running in the background. I am wondering what the best approach / pattern is for this:
If anyone is willing to share their own solutions for something similar, I would be very grateful. I can document this and contribute to the project if it's welcome.
Beta Was this translation helpful? Give feedback.
All reactions