Quick Tips:How to Speed up Django applications

Esther Vaati
3 min readSep 28, 2022

A short guide to improving Performance in Django applications

Photo by Faisal on Unsplash

Avoid Using Django Model Serializer

The Django rest framework allows developers to ship applications fast. Django Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes. The Modelserializer, however, comes at a cost in terms of poor performance from endpoints that use the serializer. This poor performances common, especially when working with large datasets. It’s advisable to write your serializers, set read-only to non-writable fields, or cache the model instances.

Use generators

Don’t attempt to write your iterations; use the Python generator() function to make your iterations faster. For example, using generators is the best solution if you need to generate a large amount of values.

Asynchronous Requests

Synchronous programming is where tasks are in sequence. All tasks follow a particular order; this means that each task is done from start to finish before the next task starts.

A disadvantage of synchronous programming is that if a specific task takes considerably longer to execute, the preceding tasks must wait for the task to complete; this causes delays.

Asynchronous programming, however, means that tasks can coincide without waiting for other jobs to finish execution.

Asynchronous communication is primarily used in chat applications as well as applications that show real-time data.

Asynchronous programming saves the day when fetching data from an API. Fetching data from an API or making an http request generally takes longer than expected; rather than waiting for the http request to complete, you can apply an async function on the http call.

Async calls free ups programs to continue executing in other areas.

Another advantage of Asynchronous programming is that if you are making multiple requests, and one of the requests suddenly stops, this doesn’t affect others since the program is allowed to move to the next task.

Suppose you have an application that requests several times to an external api server; with synchronous programming, the application will have to wait until the proceeding api completes the HTTP request and returns the response, making your application slow.

For example, suppose we want to request an external API such as the weather Api; let’s compare the time it takes when using synchronous and asynchronous calls.

The synchronous function will look like this

The function above queries the Weather API and returns the current weather for some of the most popular cities in the world.

The current weather and the total time takes to query the API for each city will be:

18.0 0.7423.0 1.1528.0 1.5641.0 1.9629.0 2.3740.0 2.78

Let’s rewrite the function above with the help of httpx. Httpx is a python client that supports synchronous and asynchronous HTTP requests.

First install the client via pip

pip install httpx

Next write the async function which looks like this:

The current weather and the total time taken to query weather for each city will be:

16.0 0.4723.0 0.7028.0 0.9041.0 1.1129.0 1.3140.0 1.52

As you can see, above it takes about half the time as the synchronous function, (i.e. 1.52 vs 2.78 )which is a great performance improvement.

--

--