0

Following the explanations given here FastAPI - How to read an json file while using UploadFile and there FastAPI runs api-calls in serial instead of parallel fashion I switched to threading instead of async.

I removed the awaits in my program and now get an error when trying to read the uploaded file:

RuntimeWarning: coroutine 'UploadFile.read' was never awaited
  result = context.run(func, *args)
from fastapi import FastAPI, UploadFile
from fastapi.responses import JSONResponse

app = FastAPI()

def validate_configuration(config_file: UploadFile = File(...)):
    try:
        content = config_file.read()  # used await before and now causes an attribute error
        print(content)
        configurations = yaml.safe_load(content.decode())
        return JSONResponse(content={"message": "Done"}
    except Exception as exc:
        return JSONResponse(content={"error": str(exc)}, status_code=400)

But of course I cannot use await anymore, because I am no longer using async so what is the intended way to resolve this?

Natan
  • 728
  • 1
  • 7
  • 23
  • Does `config_file.file.read()` do what you want? The exposed direct methods are the async helpers, so if you don't need async support, perform any operation directly on the included file-like object. https://fastapi.tiangolo.com/tutorial/request-files/#uploadfile – MatsLindh Aug 17 '23 at 12:04

0 Answers0