1

I am using FastAPI to make predictions using a ML model. When I give a task_id and input, the app should add it to the background task and return the response accordingly. However, I am getting Error 500 when I try to do it.

After adding task_id_globally, it started throwing errors before it worked fine.

Error

  File ".\app\main.py", line 36, in post
    return {'result': response_name[task_id_global]}
TypeError: list indices must be integers or slices, not NoneType

Code

task_id_global = None
@app.get('/predict')
async def predict(task_id:int, background_tasks: BackgroundTasks,solute,solvent):
    task_id_global = task_id
    if task_id == 0:
        background_tasks.add_task(predictions,solute,solvent)
        return {'success'}
    elif task_id == 1:
        background_tasks.add_task(predictions_two,solute)
        return {'success'}
    else:
        return "Give proper task_id"
    
response_name = [response, attach_drug_name()]

@app.get('/predict_solubility')
async def post():
    return {'result': response_name[task_id_global]}
Chris
  • 18,724
  • 6
  • 46
  • 80
  • What is the actual error that results in the 500 response from FastAPI? The console will have the actual error? Returning a set (the `{'success'}` value) from your API probably doesn't make sense either (since it can't be represented in JSON). – MatsLindh Mar 07 '22 at 14:50
  • @MatsLindh I have added the error. It seems like the task_id value I have given is not assigned to `task_id_global`. How to rectify this –  Mar 07 '22 at 15:15
  • You can't assign a value to a variable outside your function; and in general, using global values in your application leads to further problems rather quickly. To make assignment to a variable outside of a function work, you can use a dict (since you can refer to the dict and update it), but generally you might want to move this to a more suitable queue than trying to keep it inside FastAPI. – MatsLindh Mar 07 '22 at 21:24

1 Answers1

1

You have set task_id_global to None, and thus, when calling /predict_solubility endpoint, it is trying to retrieve an element from the list using response_name[None]; hence, the error. So you should set task_id_global to 0, which should point to some default value in your response_name list - even if /predict endpoint has not yet been called - or perform a check inside the second endpoint to see whether task_id_global is not None and then decide whether to proceed retrieving an item from the list. Next, inside /predict endpoint declare task_id_global as global before using it (using the global keyword), as, in the way it is currently declared, it is interpreted as a local variable, and hence, the global one never gets affected by any changes occur to task_id_global inside the endpoint (have a look here).

task_id_global = None

@app.get('/predict')
async def predict(task_id:int,solute,solvent):
    global task_id_global
    task_id_global = task_id
    ...

Also, depending on your task (e.g., if you have multiple requests concurrently accessing that global variable), you might want to consider other options, such as Redis for instance. Have a look at this answer.

Chris
  • 18,724
  • 6
  • 46
  • 80