WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Advanced · question 43 of 100

What is the difference between the async/await and the threading module for concurrency in Python? When would you use each?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Both the async/await and the threading modules are used for achieving concurrency in Python, but they use different approaches to accomplish this.

The threading module provides a way to create and manage threads in Python. Each thread represents a separate flow of execution within a single process. Threads can be used to perform CPU-bound tasks in parallel, or to perform I/O-bound tasks while other threads are blocked waiting for I/O operations to complete.

On the other hand, the async/await syntax is used to create coroutines, which are similar to threads but have some important differences. Coroutines are functions that can be paused and resumed at specific points, allowing other coroutines to execute in the meantime. They are typically used for performing I/O-bound tasks, where waiting for I/O operations to complete can cause significant delays.

The async/await approach has some advantages over the threading approach. For example:

Memory usage: Coroutines require less memory than threads because they do not create a separate stack for each execution context.

Context switching: Switching between coroutines is faster than switching between threads, because it involves only a few CPU instructions.

Scalability: Coroutines can handle a large number of connections simultaneously, making them well-suited for building highly scalable network servers.

However, the threading approach also has some advantages over the async/await approach. For example:

Simplicity: Threads are a simpler and more intuitive way to achieve concurrency, especially for CPU-bound tasks.

Compatibility: The threading module is compatible with a wider range of third-party libraries and frameworks than the async/await syntax.

In general, you would use the threading module when you need to perform CPU-bound tasks or when working with third-party libraries that do not support coroutines. You would use the async/await syntax when you need to perform I/O-bound tasks or when building highly scalable network servers.

Here is an example of using the threading module to perform CPU-bound tasks in parallel:

    import threading
    
    def count(n):
        for i in range(n):
            print(i)
    
    threads = []
    for i in range(5):
        thread = threading.Thread(target=count, args=(1000000,))
        thread.start()
        threads.append(thread)
    
    for thread in threads:
        thread.join()

In this example, we create 5 threads that each count to 1,000,000. By running these threads in parallel, we can significantly speed up the execution time of the program.

Here is an example of using the async/await syntax to perform I/O-bound tasks with coroutines:

    import asyncio
    import aiohttp
    
    async def fetch(session, url):
        async with session.get(url) as response:
            return await response.text()
    
    async def main():
        async with aiohttp.ClientSession() as session:
            tasks = [asyncio.create_task(fetch(session, 'https://www.google.com')) for _ in range(5)]
            responses = await asyncio.gather(*tasks)
            for response in responses:
                print(response[:100])
    
    asyncio.run(main())

In this example, we use the aiohttp library and the async/await syntax to fetch the HTML content of the Google homepage in parallel using 5 coroutines. By running these coroutines concurrently, we can significantly reduce the amount of time it takes to fetch the content.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics