An Introduction To Concurrency in Python
Posted By : Anoop Sharma | 09-Mar-2021

In this article, we will talk about concurrency with regards to Python programming, the different structures it comes in and we will accelerate a straightforward program to see the exhibition gains in real world.
Concurrency
At the point when at least two occasions are simultaneous it implies that they are occurring simultaneously. In actuality, simultaneousness is basic since a ton of things occur simultaneously constantly. In figuring, things are somewhat extraordinary with regards to simultaneousness.
In figuring, simultaneousness is the execution of bits of work or errands by a PC simultaneously. Regularly, a PC executes a bit of work as others sit tight, when it is finished, the assets are liberated and the following bit of work starts execution. This isn't the situation when simultaneousness is actualized as the bits of work to be executed don't generally need to trust that others will be finished. They are executed simultaneously.
Concurrency and Parallelism
The term Parallelism refers to process to make programs faster by executing multiple operations at the same time.
Whereas Concurrency refers to doing multiple computations one at a time. That is one after another. Parallelism may need hardware support for achieving the target whereas Concurrency does not need any extra hardware support to achieve its target.
Thread vs Process vs Task
A process is the set of instruction as code which operates on related data in the current instance of time.
A thread is a scheduling technique, That's how CPU works . A process needs at least one thread for the CPU to execute task.
A task is a set of instructions that are loaded via program in memory.
Multithreading vs Multiprocessing vs Asyncio
Multithreading
- Thread: shared memory (easier communication between threads).
- Task switching handled by OS
Multiprocessing
- Process: separate and larger memory foodprint
- Spawning is slower than threading process in Multiprocessing.
Asyncio
- Faster than threads.
- Use of callback is replaced by await.
- Harder to implement asyncio in code but easier to get it right if used correctly.
Synchronous Script
With these details, let's create our first script that will help us to download bunch of images to a downloads folder:
import os
from urllib import request
from imgurpython import ImgurClient
import timeit
client_secret = os.getenv("CLIENT_SECRET")
client_id = os.getenv("CLIENT_ID")
client = ImgurClient(client_id, client_secret)
def download_image_data(link):
file_name = link.split('/')[3].split('.')[0]
file_format = link.split('/')[3].split('.')[1]
request.urlretrieve(link, "downloads/{}.{}".format(file_name, file_format))
print("{}.{} downloaded data".format(file_name, file_format))
def main():
images = client.get_album_images('Sports')
for image in images:
download_image(image.link)
if __name__ == "__main__":
print("Time taken via synchronously: {}".format(timeit.Timer(main).timeit(number=1)))
Optimizing via Multithreading
Let's try Multithreading and see how it performs for concurrent operations:
import threading
from concurrent.futures import ThreadPoolExecutor
def download_album_data(album_id):
images = client.get_album_images(album_id)
with ThreadPoolExecutor(max_workers=5) as executor:
executor.map(download_image, images)
def main():
download_album_data('Sports')
if __name__ == "__main__":
print("Time taken via multithreading: {}".format(timeit.Timer(main).timeit(number=1)))
Optimizing via Multiprocessing
Now we will see how Multiprocessing with more than a few CPUs performs for a similar content:
import multiprocessing
def main():
images = client.get_album_images('Sports')
data = multiprocessing.Pool(multiprocessing.cpu_count())
result = data.map(download_image_data, [image.link for image in images])
if __name__ == "__main__":
print("Time taken via multiprocessing: {}".format(timeit.Timer(main).timeit(number=1)))
Optimizing via AsyncIO
Applying the same script using Asyncio now:
import asyncio
import aiohttp
#Method implementing async
async def download_image_data(link, session):
file_name = link.split('/')[3].split('.')[0]
file_format = link.split('/')[3].split('.')[1]
async with session.get(link) as response:
with open("downloads/{}.{}".format(file_name, file_format), 'wb') as fd:
async for data in response.content.iter_chunked(1024):
fd.write(data)
print("{}.{} downloaded content".format(file_name, file_format))
async def main():
images = client.get_album_images('Sports')
async with aiohttp.ClientSession() as session:
data = [download_image_data(image.link, session) for image in images]
return await asyncio.gather(*data)
if __name__ == "__main__":
start_time = timeit.default_timer()
loop = asyncio.get_event_loop()
results = loop.run_until_complete(main())
time_taken = timeit.default_timer() - start_time
print("Time taken via AsyncIO: {}".format(time_taken))
Performance Comparison

Conclusion
In this post, we have secured simultaneousness and how it thinks about to parallelism. We have additionally investigated the different strategies that we can use to execute simultaneousness in our Python code, including multithreading and multiprocessing, and furthermore examined their disparities.
Are You planning to get your mobile application built with Python? Our Python app developers use this language for creating video streaming apps, blockchain apps, business apps, and more. For more queries on app development using Python, get in touch with our experts today!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anoop Sharma
Anoop is a Python developer, who has worked on Python Framework Django and is keen to increase his skillset in the field. He has a zest for learning and is capable of handling challenges. He is a team player and has good enthusiasm.