I have deployed a machine learning model using Fast API in Heroku and I have implemented the background worker and async but when I am doing inference I am still getting 503 error. I know the server response time in Heroku is 30 seconds.
But as I am using a background worker I should be able to run in the background, instead, I am getting errors. I am using Heroku free plan to deploy
app = FastAPI()
response = {}
@app.get('/predict')
async def predictions(solute, solvent):
mol = Chem.MolFromSmiles(solute)
response["predictions"] = delta_g.item()
return {'result': response}
async def predict(background_tasks: BackgroundTasks,solute,solvent):
background_tasks.add_task(predictions,solute,solvent)
from predict_json import json_data_func
@app.get('/predict_two')
async def predictions_two(solute):
for i in data:
delta_g, interaction_map = model([get_graph_from_smile(Chem.MolToSmiles(Chem.AddHs(Chem.MolFromSmiles(solute)))).to(device), get_graph_from_smile(Chem.MolToSmiles(Chem.AddHs(Chem.MolFromSmiles(i)))).to(device)])
response_two[i] = delta_g.item()
return {'result': response_two}
async def predict_two(background_tasks: BackgroundTasks,solute):
background_tasks.add_task(predictions_two,solute)
Procfile:
web: gunicorn -w 2 -k uvicorn.workers.UvicornWorker main:app
So what should I have to do to run my code in the background without having a server response time-out error?
You can add timeout parameter (with -t or --timeout) to the gunicorn as stated in the gunicorn documentation.
Example usage:
gunicorn -w 2 -k uvicorn.workers.UvicornWorker -t 90 main:app