DEV Community

SK RAJIBUL
SK RAJIBUL

Posted on

Choosing the best asynchronous library in Python

Choosing the best asynchronous library in Python depends on various factors such as your use case, familiarity with threading concepts, and performance requirements. Let's explore the options:

  1. Threading Library:
  • Best for: More control over each thread, functions that don't return a value.

  • Use it when: You need fine-grained control over threading operations.


  import threading

import requests


def fetch_url(url):

   response = requests.get(url)

   print(f"Fetched data from {url}: {response.status_code}")


urls = ["https://lnkd.in/g5g8CQpJ", "https://lnkd.in/gb5ETARx"]


threads = []

for url in urls:

   thread = threading.Thread(target=fetch_url, args=(url,))

   thread.start()

   threads.append(thread)


for thread in threads:

   thread.join()


Enter fullscreen mode Exit fullscreen mode
  1. Concurrent Futures Library:
  • Best for: Functions that return a value, simpler usage compared to threading.

  • Use it when: You don't need low-level control over threading operations.


from concurrent.futures import ThreadPoolExecutor

import requests


def fetch_url(url):

   response = requests.get(url)

   print(f"Fetched data from {url}: {response.status_code}")


urls = ["https://lnkd.in/g5g8CQpJ", "https://lnkd.in/gb5ETARx"]


with ThreadPoolExecutor() as executor:

   executor.map(fetch_url, urls)


Enter fullscreen mode Exit fullscreen mode
  1. Asyncio Library:
  • Best for: Both returning values and high-level/low-level asynchronous operations.

  • Use it when: You need complex asynchronous operations and want to leverage coroutines.


 import asyncio

import aiohttp


async def fetch_url(url):

   async with aiohttp.ClientSession() as session:

       async with session.get(url) as response:

           data = await response.text()

           print(f"Fetched data from {url}: {response.status}")


async def main():

   urls = ["https://lnkd.in/g5g8CQpJ", "https://lnkd.in/gb5ETARx"]

   await asyncio.gather(*[fetch_url(url) for url in urls])


asyncio.run(main())


Enter fullscreen mode Exit fullscreen mode

In this example:

The threading library creates a separate thread for each URL request.

Concurrent Futures library utilizes a thread pool to concurrently fetch data from multiple URLs.

Asyncio library employs coroutines to asynchronously fetch data from each URL.

When it comes to speed, asyncio tends to be about 3.5x faster than threading because it efficiently manages threads and scales better.

Choose the library that best fits your needs and level of expertise! 🚀

Top comments (0)