Multiprocessing in FastAPI
Last Updated :
24 Apr, 2025
In this article, we are going to see what is multiprocessing, and how multiprocessing can utilize the system resources efficiently and can reduce the time taken to complete a big task. We will see the "concurrent.futures" module that is used for multiprocessing in FastAPI and then see how we can use it to download a big zip file from the server efficiently.
What is Multiprocessing?
A Process is an independent program having its own memory space, code, data, and system resources. Multiple processes run on the system independently of each other. Two processes will not have access to a shared resource simultaneously. Multiprocessing is the running of multiple processes at the same time on a multi-core CPU. Multiprocessing helps in utilizing the CPU resources efficiently by achieving true data parallelism.
There are two types of time-bounding operations I/O bound and CPU bound. In I/O bound operation more time is consumed in input-output operation and in CPU bound task process consumes more CPU time. Multiprocessing is mostly used when CPU-bound tasks need to be completed because the task can be divided into multiple processes and those processes can run on multiple processors at the same time.
Downloading Multiple images using Multiprocessing
We are going to create an API that will download a zip file consisting of 10 high-resolution images from the server. The server first has to download the images, zip them, and then send them to the client. Now if the images are of high resolution it will take a lot of time if the server download the images synchronously. For downloading the image faster we can create 10 processes each responsible for downloading different images and writing the downloaded image in the zip file.

What is "concurrent.futures" Module ?
We are going to use the concurrent.futures module which is used to execute tasks in parallel. It provides two main classes ThreadPoolExecutor and ProcessPoolExecutor which can be used to submit the tasks that need to be run in parallel. The ProcessPoolExecutor will create a set of processes and tasks are assigned to one of the process that is available. For example we can create an instance of ProcessPoolExecutor and then pass the function that needs be executed in parallel by calling the submit method on the executor object. The executor will then assign this function to one of the processes in the process pool.
Setting up the Project
We are going to create a "/download" endpoint which will download a zip file consisting of 10 blurred images. At the server side we are going to download the images, zip it and send it back as response to the user. Create a folder named multiprocessing_gfg and create a file named "app.py" in it and copy paste the below code:
- The /download endpoint is defined as an asynchronous FastAPI route (async def uploadfile()).
- Inside this endpoint, a list called list is initialized to store the futures returned by the ProcessPoolExecutor. These futures represent the concurrent image processing tasks.
- The concurrent.futures.ProcessPoolExecutor is used to manage a pool of worker processes. Within a loop, it submits the process_image function as a task with the URL and an index (i) as arguments. This means that up to 10 image processing tasks can run concurrently, thanks to the ProcessPoolExecutor.
- The concurrent.futures.wait function is used to wait for all submitted tasks to complete before proceeding.
- After all image processing tasks are done, the add_images_to_zip function is called to add the processed images to a ZIP file.
- Finally, a FileResponse is returned with the ZIP file as an attachment.
Python3
import io
from fastapi import FastAPI
from PIL import Image, ImageFilter
from pathlib import Path
import concurrent.futures
import zipfile
from fastapi.responses import FileResponse
import requests
app = FastAPI()
def process_image(url, count):
try:
response = requests.get(url)
image = Image.open(io.BytesIO(response.content))
image = image.filter(ImageFilter.GaussianBlur(1))
temp_image_path =Path(f"temp_{count}.jpg")
image.save(temp_image_path)
except Exception as e:
print(e)
def add_images_to_zip(zipf):
try:
for i in range(10):
image_path = Path(f"temp_{i}.jpg")
zipf.write(image_path)
image_path.unlink()
except Exception as e:
print(e)
@app.get("/download")
async def uploadfile():
try:
url = "https://picsum.photos/200/300"
list = []
zip_file_path = Path("processed_images.zip")
with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
with concurrent.futures.ProcessPoolExecutor() as executor:
for i in range(10):
futures = executor.submit(process_image, url, i)
print(futures)
list.append(futures)
concurrent.futures.wait(list)
add_images_to_zip(zipf)
return FileResponse(zip_file_path, headers={"Content-Disposition": "attachment; filename=processed_images.zip"})
except Exception as e:
print(e)
return {"message": e}
Deployement of the Project
To run the fastAPI application created above we are going to use the uvicorn which is an ASGI server used to deploy the fastAPI web application. Run the below command on the terminal :
python -m uvicorn app::app --reload

In the above line we specified the file name app which is going to be used as our application. Then we specified the app instance we created of the FastAPI class. "--reload" tag is used to automatically reload the server whenever changes in the app.py files are detected.
Open web browser and copy paste the below URL:
http://127.0.0.1:8000/download
The above url will download the zip file on your system.
Output
Similar Reads
FastAPI - Introduction
Developers are continuously on the lookout for technologies that allow them to rapidly and efficiently construct sophisticated APIs and online applications. FastAPI, a relatively new addition to the Python web framework landscape, has quickly garnered traction due to its speed, simplicity, and devel
5 min read
Microservice in Python using FastAPI
Microservices architecture is the approach to software development where the large application is composed of smaller, independent services that communicate over well-defined APIs. Each service can be focused on a specific business function and it can be developed, deployed, and scaled independently
5 min read
FastAPI - Using MongoDB
Let's explore a new way to create APIs using FastAPI. It's fast, easy, and powerful. In this article, we'll also see how to use MongoDB to do things like adding, reading, updating, and deleting data in our API. MongoDB with FastAPIAfter creating an API, the next step is to have a database that can s
10 min read
Multiprocessing in Python and PyTorch
Multiprocessing is a technique in computer science by which a computer can perform multiple tasks or processes simultaneously using a multi-core CPU or multiple GPUs. It is a type of parallel processing in which a program is divided into smaller jobs that can be carried out simultaneously. The progr
12 min read
Multiprocessing with NumPy Arrays
Multiprocessing is a powerful tool that enables a computer to perform multiple tasks at the same time, improving overall performance and speed. In this article, we will see how we can use multiprocessing with NumPy arrays. NumPy is a library for the Python programming language that provides support
5 min read
FastAPI - Uvicorn
FastAPI is a modern, high-performance, and robust Python web framework used for building REST APIs. It seamlessly integrates with UVICORN, a lightweight ASGI server, which excels in handling multiple connections and events concurrently. The choice of UVICORN for FastAPI is driven by its exceptional
3 min read
Pipeline in Query Processing in DBMS
Database system processing in a satisfactory manner encompasses providing fast responses to data retrieval and manipulation tasks, with two of the keywords being performance and responsiveness. A concept that acts as the foundational element in improving batch processing performance is called "pipel
5 min read
Multiprocessing in Python | Set 1 (Introduction)
This article is a brief yet concise introduction to multiprocessing in Python programming language. What is multiprocessing? Multiprocessing refers to the ability of a system to support more than one processor at the same time. Applications in a multiprocessing system are broken to smaller routines
7 min read
FastAPI - Pydantic
In this article, we will discuss FastAPI and Pydantic by using an example. So, let's get started. FastAPI and Pydantic are two potent tools within the Python ecosystem, highly acclaimed for their roles in crafting resilient and efficient web APIs. This article will guide you through the process of e
7 min read
Introduction to FastAPI And Installation
Introduction to FastAPIFastAPI is a modern, fast (as the name suggests), and highly performant Python web framework used for building APIs. It is built on top of standard Python-type hints and is powered by asynchronous programming using Python's "asyncio". FastAPI is known for its speed, simplicity
4 min read