Parallel Asynchronous API Call in Python

Sankhadip Samanta
The Startup
Published in
2 min readAug 17, 2020

--

From The Programming Arsenal

A synchronous program is executed one step at a time. Even with conditional branching, loops and function calls, you can still think about the code in terms of taking one execution step at a time. When each step is complete, the program moves on to the next one.

An asynchronous program behaves differently. It still takes one execution step at a time. The difference is that the system may not wait for an execution step to be completed before moving on to the next one.

Although here we will implement asynchronous program that will execute 15 different instance parallelly rather than waiting for every instance to be completed.

Let’s jump to the implementation.

First, we will see how synchronous program reacts

Sync Program Output

Here we are calling a API 15 times one by one . One API call starts only after previous API call finishes. If you look at output, it takes 16.67 secs to complete 15 API calls. This is synchronous approach where next process starts when previous process ends. This type approach sometime costs heavy in real time project.

Now same we will implement same using async approach where we will make 15 API calls parallelly.

Async ProgramOutput

The max time taken to complete 2.45 secs.

The main advantage of using parallel execution is that code runs more efficiently and saves time.

This is it. If you are having any doubts, have a comment below

Check GitHub Repo for source code

Sankhadip Samanta

Full Stack Developer, Code Quotient | Tech Writer and Social Media Handler at BlogMarch

Find me on Linkedin 😃 and Github 😅

--

--