Render NumPy array in FastAPI

Option 1 – Return image as bytes The below examples show how to convert an image loaded from disk, or an in-memory image (numpy array), into bytes (using either PIL or OpenCV libraries) and return them using a custom Response. For the purposes of this demo, the below code is used to create the in-memory … Read more

Sharing python objects across multiple workers

It is not possible to share a python object between different processes straightforwardly. The facilities included in the multiprocessing module (like managers or shared memory) are not suitable for sharing resources between workers, since they require a master process creating the resources and do not have the durability property. Also server processes can be run … Read more

How can I send an HTTP request from my FastAPI app to another site (API)?

requests is a synchronous library. You need to use an asyncio-based library to make requests asynchronously. httpx httpx is typically used in FastAPI applications to request external services. It provides synchronous and asynchronous clients which can be used in def and async def path operations appropriately. It is also recommended for asynchronous tests of application. … Read more

FastAPI runs api-calls in serial instead of parallel fashion

As per FastAPI’s documentation: When you declare a path operation function with normal def instead of async def, it is run in an external threadpool that is then awaited, instead of being called directly (as it would block the server). Thus, def (sync) routes run in a separate thread from a threadpool, or, in other … Read more

How to add both file and JSON body in a FastAPI POST request?

As per FastAPI documentation, You can declare multiple Form parameters in a path operation, but you can’t also declare Body fields that you expect to receive as JSON, as the request will have the body encoded using application/x-www-form-urlencoded instead of application/json (when the form includes files, it is encoded as multipart/form-data). This is not a … Read more