Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Asynchronous Functions and Coroutines in Python


Asynchronous functions and coroutines in Python allow you to write non-blocking code that can perform multiple tasks concurrently. This is particularly useful for I/O-bound operations such as file reading, database access, or network communication. Python's asyncio module provides the tools to define and manage asynchronous tasks.

What Are Asynchronous Functions?

An asynchronous function in Python is defined using the async def keyword. These functions are also known as coroutines. Unlike regular functions, coroutines can pause their execution using the await keyword and resume later, allowing other tasks to run during the pause.

Defining and Using Asynchronous Functions

Here is a basic example of an asynchronous function:

    import asyncio

    async def greet():
        print("Hello, World!")
        await asyncio.sleep(1)
        print("Goodbye, World!")

    # Running the coroutine
    asyncio.run(greet())
        

Output:

    Hello, World!
    Goodbye, World!
        

Using await Inside Coroutines

The await keyword is used to pause the execution of a coroutine until the awaited task is completed. This is commonly used with other asynchronous calls.

    import asyncio

    async def fetch_data():
        print("Fetching data...")
        await asyncio.sleep(2)
        print("Data fetched!")

    async def process_data():
        print("Processing data...")
        await asyncio.sleep(1)
        print("Data processed!")

    async def main():
        await fetch_data()
        await process_data()

    asyncio.run(main())
        

Output:

    Fetching data...
    Data fetched!
    Processing data...
    Data processed!
        

Running Multiple Coroutines Concurrently

You can execute multiple coroutines at the same time using asyncio.gather.

    import asyncio

    async def task_one():
        print("Task one starting")
        await asyncio.sleep(2)
        print("Task one completed")

    async def task_two():
        print("Task two starting")
        await asyncio.sleep(1)
        print("Task two completed")

    async def main():
        await asyncio.gather(task_one(), task_two())

    asyncio.run(main())
        

Output:

    Task one starting
    Task two starting
    Task two completed
    Task one completed
        

Creating Tasks with asyncio.create_task

The asyncio.create_task function allows you to schedule a coroutine to run as a task. This is useful for managing multiple coroutines independently.

    import asyncio

    async def print_numbers():
        for i in range(3):
            print(f"Number: {i}")
            await asyncio.sleep(1)

    async def main():
        task = asyncio.create_task(print_numbers())
        print("Task created!")
        await task
        print("Task completed")

    asyncio.run(main())
        

Output:

    Task created!
    Number: 0
    Number: 1
    Number: 2
    Task completed
        

Using Async Generators

Async generators are a special kind of coroutine that can yield values asynchronously. They are defined using async def and the yield keyword.

    import asyncio

    async def async_generator():
        for i in range(3):
            await asyncio.sleep(1)
            yield i

    async def main():
        async for value in async_generator():
            print(f"Generated: {value}")

    asyncio.run(main())
        

Output:

    Generated: 0
    Generated: 1
    Generated: 2
        

Advantages of Asynchronous Functions

Limitations

Conclusion

Asynchronous functions and coroutines in Python offer an efficient way to handle multiple tasks concurrently. By leveraging the asyncio module and its tools like await, asyncio.gather, and asyncio.create_task, you can write scalable and responsive programs for various use cases.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java