[go: up one dir, main page]

0% found this document useful (0 votes)
6 views3 pages

Await in Python - GeeksforGeeks

The 'await' keyword in Python is essential for asynchronous programming, allowing tasks to pause execution until another task is ready, enabling non-blocking I/O operations. Examples demonstrate how to run multiple coroutines concurrently and process tasks sequentially using the asyncio library. The document provides syntax, parameters, and practical examples to illustrate the use of 'await' in Python.

Uploaded by

linfengmian2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Await in Python - GeeksforGeeks

The 'await' keyword in Python is essential for asynchronous programming, allowing tasks to pause execution until another task is ready, enabling non-blocking I/O operations. Examples demonstrate how to run multiple coroutines concurrently and process tasks sequentially using the asyncio library. The document provides syntax, parameters, and practical examples to illustrate the use of 'await' in Python.

Uploaded by

linfengmian2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

await in python - GeeksforGeeks

www.geeksforgeeks.org/python/await-in-python/

February 25, 2025

await in python

Last Updated : 25 Feb, 2025

await keyword in Python is used to pause the execution of a task until the result
of another task or operation is ready. It's a key part of Python's asynchronous
programming, allowing for non-blocking, concurrent execution of I/O-bound
tasks.

Python
import asyncio

# asynchronous function
async def fun():
print("Hello")
await asyncio.sleep(1) # Simulate an asynchronous task
print("World")

asyncio.run(fun()) # calling fun()

Output
Hello
World

Explanation:

• await asyncio.sleep(1) pauses the execution for 1 second without


blocking other tasks, allowing the event loop to run other asynchronous
operations.
• After the pause, print("World") executes, demonstrating the non-
blocking behavior enabled by await.

Syntax
await <expression>

Parameters:

expression: An awaitable object, such as a coroutine, an asynchronous


function, or any object that supports asynchronous operations (e.g.,
asyncio.sleep(), a Future object).

Returns:

It returns the result of the awaited task or coroutine once it has finished
executing.

await Examples
Example 1: Mutiple coroutines with await

This example demonstrates how to run two tasks concurrently using Python's
asyncio library.

Python
import asyncio

async def task_1():


print("Task 1 started")
await asyncio.sleep(2) # Simulate a 2-second task
print("Task 1 completed")

async def task_2():


print("Task 2 started")
await asyncio.sleep(1) # Simulate a 1-second task
print("Task 2 completed")

async def main():


await asyncio.gather(task_1(), task_2()) # Run both tasks concurrently

asyncio.run(main())

Output
Task 1 started
Task 2 started
Task 1 completed
Task 2 completed
Explanation: task_1() and task_2() are asynchronous functions that print
messages and pause for 2 and 1 second, respectively. The main() coroutine
runs both tasks concurrently with asyncio.gather(). asyncio.run(main()) starts
the event loop, running and waiting for both tasks to complete.

Example 2: await with custom async function

This example demonstrates how to process tasks sequentially using Python's


asyncio library.

Python
import asyncio

async def custom_async_task(task_num):


print(f"Task {task_num} started")
await asyncio.sleep(3) # Simulate a 3-second task
print(f"Task {task_num} completed")

async def main():


await custom_async_task(1)
await custom_async_task(2)

asyncio.run(main())

Output
Task 1 started
Task 1 completed
Task 2 started
Task 2 completed

Explanation : custom_async_task() simulates a 3-second task, printing its


start and completion messages. The main() runs two tasks sequentially, waiting
for one to finish before starting the next. asyncio.run(main()) executes both
tasks, totaling around 6 seconds of runtime.

You might also like