title: Unable to make backround tasks run using fastapi - 504: Gateway Timeout Error
body: <p>I am using fastapi to do predictions on a ml model. Because a single prediction is taking more than 2 minutes I have made the prediction to run as a background task using fastapi background tasks. Now when i deployed to digital ocean i was unable to run background worker. I am getting <code>504: Gateway Timeout Error</code>.</p>
  <p>startup command</p>
  <pre class="lang-py prettyprint-override"><code>gunicorn --worker-tmp-dir /dev/shm --config gunicorn.config.py main:app
  </code></pre>
  <p><code>gunicorn.config.py</code></p>
  <pre class="lang-py prettyprint-override"><code>bind = &quot;0.0.0.0:8080&quot;
  workers = 4
  worker_class = &quot;uvicorn.workers.UvicornWorker&quot;
  </code></pre>
  <p><code>main.py</code></p>
  <pre class="lang-py prettyprint-override"><code>response = {}
  async def predictions(solute, solvent):
      mol = Chem.MolFromSmiles(solute)
      solute_graph = get_graph_from_smile(solute)
      mol = Chem.MolFromSmiles(solvent)
      solvent_graph = get_graph_from_smile(solvent)
      delta_g, interaction_map =  model([solute_graph.to(device), solvent_graph.to(device)])
      interaction_map_one = torch.trunc(interaction_map)
      response[&quot;interaction_map&quot;] = (interaction_map_one.detach().numpy()).tolist()
      response[&quot;predictions&quot;] = delta_g.item()
  
  @app.post('/predict_solubility')
  async def post():
      return {'result': response}
  
  @app.post('/predict')
  async def predict(background_tasks: BackgroundTasks,solute,solvent):
      background_tasks.add_task(predictions,solute,solvent)
      return {'success'}
  
  response_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()
      
  @app.post('/predict_solubility_json')
  async def post():
      return {'result': response_two}
  
  @app.get('/predict_two')
  async def predict_two(background_tasks: BackgroundTasks,solute):
      background_tasks.add_task(predictions_two,solute)
      return {'success'}
  
  if __name__ == &quot;__main__&quot;:
      uvicorn.run(app,host=&quot;0.0.0.0&quot;, port=8000, reload=True)
  </code></pre>
  <p>I have added <code>worker</code> in digital ocean from components even though i am getting still server timeout. I am not sure whether i have not implemented background tasks using fastapi for is their any problem with my startup command</p>