From 522859566948d56e1530f64c215fe46135f46c1e Mon Sep 17 00:00:00 2001 From: Sophie Beech <219414866+Fae-coded@users.noreply.github.com> Date: Tue, 21 Oct 2025 18:16:43 +0100 Subject: [PATCH 1/4] Create async keyword python entry --- .../concepts/keywords/terms/async/async.md | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 content/python/concepts/keywords/terms/async/async.md diff --git a/content/python/concepts/keywords/terms/async/async.md b/content/python/concepts/keywords/terms/async/async.md new file mode 100644 index 00000000000..efa56c31e0f --- /dev/null +++ b/content/python/concepts/keywords/terms/async/async.md @@ -0,0 +1,80 @@ +--- +Title: 'async' +Description: 'Allows users to define an asynchronous function in Python.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Async Await' + - 'Functions' + - 'Python' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The **`async`** keyword is used to define an asynchronous function in Python. Asynchronous functions allow multiple tasks to run simultaneously, unlike synchronous code, which completes each task in sequence before beginning the next task. Async allows for better resource utilisation by handling multiple operations concurrently and enables non-blocking execution. + +## Syntax + +```pseudo +import asyncio + +async def function_name(parameters): + # Function body + await #awaitable code +``` +In the syntax: + +- `import asyncio`: Imports the Python library asyncio that contains the async and await keywords. +- `async`: The async keyword is declared before def to define an asynchronous function. +- `await`: The await keyword must be included in the function body, followed by the function body code to be awaited. + +> **Note:** If you declare async and do not include the await keyword within the function body a warning will occur and the code will not execute fully. + +## Example + +In this example, the async function `hello()` prints the first half of 'Hello world!', waits 3 seconds, and then prints the second half: + +```py +import asyncio + +async def hello(): + print("Hello") + await asyncio.sleep(3) # Pauses for 3 seconds + print("world!") +``` + +Here is the output: + +```shell +Hello +World! +``` + +## Codebyte Example + +In this example, the brew_tea and make_toast async functions are called at the same time. make_toast completes first in 3 seconds and waits for brew_tea to complete in an extra 2 seconds. Once brew_tea has completed, both return messages are printed together after a total of 5 seconds. + +```codebyte/python +import asyncio + +async def brew_tea(): + print("Start brewing tea") + await asyncio.sleep(5) # Pauses for 5 seconds + return "The tea is ready!" + +async def make_toast(): + print("Start making toast") + await asyncio.sleep(3) # Pauses for 3 seconds + return "The toast is ready!" + +async def make_breakfast(): + batch = asyncio.gather(brew_tea(), make_toast()) + tea_complete, toast_complete = await batch + print(tea_complete) + print(toast_complete) + +result = asyncio.run(make_breakfast()) +print(result) +``` \ No newline at end of file From ea99564f8a89fa6dc1dfd91a9ed6c606585fe019 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 28 Oct 2025 11:18:36 +0530 Subject: [PATCH 2/4] Update async.md for clarity and consistency Revised explanations of the async keyword, syntax, and examples for clarity and accuracy. Improved formatting and consistency in code examples. --- .../concepts/keywords/terms/async/async.md | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/content/python/concepts/keywords/terms/async/async.md b/content/python/concepts/keywords/terms/async/async.md index efa56c31e0f..b0147826fdb 100644 --- a/content/python/concepts/keywords/terms/async/async.md +++ b/content/python/concepts/keywords/terms/async/async.md @@ -13,26 +13,35 @@ CatalogContent: - 'paths/computer-science' --- -The **`async`** keyword is used to define an asynchronous function in Python. Asynchronous functions allow multiple tasks to run simultaneously, unlike synchronous code, which completes each task in sequence before beginning the next task. Async allows for better resource utilisation by handling multiple operations concurrently and enables non-blocking execution. +The **`async`** keyword defines an asynchronous function in Python. Asynchronous functions let a program handle multiple tasks concurrently without waiting for each to finish, improving efficiency and enabling non-blocking execution. ## Syntax ```pseudo import asyncio - + async def function_name(parameters): # Function body await #awaitable code ``` + In the syntax: -- `import asyncio`: Imports the Python library asyncio that contains the async and await keywords. -- `async`: The async keyword is declared before def to define an asynchronous function. -- `await`: The await keyword must be included in the function body, followed by the function body code to be awaited. +- `import asyncio`: Imports Python’s built-in library for running and managing asynchronous tasks. +- `async`: The `async` keyword is declared before [`def`](https://www.codecademy.com/resources/docs/python/keywords/def) to define an asynchronous function (coroutine). +- `await`: Used inside an async function to pause execution until the awaited coroutine or awaitable object completes. + +**Parameters:** + +Same as a regular function, any number of positional or keyword arguments. -> **Note:** If you declare async and do not include the await keyword within the function body a warning will occur and the code will not execute fully. +**Return value:** -## Example +Returns a coroutine object, which must be awaited (using `await`) to get the actual result. + +> **Note:** If an `async` function doesn’t contain any `await` statements, it will still be valid but won’t perform any asynchronous operations. + +## Example 1 In this example, the async function `hello()` prints the first half of 'Hello world!', waits 3 seconds, and then prints the second half: @@ -40,34 +49,34 @@ In this example, the async function `hello()` prints the first half of 'Hello wo import asyncio async def hello(): - print("Hello") - await asyncio.sleep(3) # Pauses for 3 seconds - print("world!") + print("Hello") + await asyncio.sleep(3) # Pauses for 3 seconds + print("world!") ``` -Here is the output: +Here is the output of this code: ```shell Hello -World! +world! ``` -## Codebyte Example +## Example 2 -In this example, the brew_tea and make_toast async functions are called at the same time. make_toast completes first in 3 seconds and waits for brew_tea to complete in an extra 2 seconds. Once brew_tea has completed, both return messages are printed together after a total of 5 seconds. +In this example, the `brew_tea` and `make_toast` async functions start running at the same time. `make_toast` finishes first in 3 seconds, while `brew_tea` takes 5 seconds to complete. The program waits for both tasks to finish, then prints their results together after a total of 5 seconds: -```codebyte/python +```py import asyncio async def brew_tea(): - print("Start brewing tea") - await asyncio.sleep(5) # Pauses for 5 seconds - return "The tea is ready!" - + print("Start brewing tea") + await asyncio.sleep(5) # Pauses for 5 seconds + return "The tea is ready!" + async def make_toast(): - print("Start making toast") - await asyncio.sleep(3) # Pauses for 3 seconds - return "The toast is ready!" + print("Start making toast") + await asyncio.sleep(3) # Pauses for 3 seconds + return "The toast is ready!" async def make_breakfast(): batch = asyncio.gather(brew_tea(), make_toast()) @@ -77,4 +86,14 @@ async def make_breakfast(): result = asyncio.run(make_breakfast()) print(result) -``` \ No newline at end of file +``` + +The output of this code is: + +```shell +Start brewing tea +Start making toast +The tea is ready! +The toast is ready! +None +``` From 5716c7437141734ed1da2a8c800de80f2f366407 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 29 Oct 2025 19:33:44 +0530 Subject: [PATCH 3/4] Format and lint fix --- content/python/concepts/keywords/terms/async/async.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/python/concepts/keywords/terms/async/async.md b/content/python/concepts/keywords/terms/async/async.md index b0147826fdb..bd602fb42bf 100644 --- a/content/python/concepts/keywords/terms/async/async.md +++ b/content/python/concepts/keywords/terms/async/async.md @@ -1,14 +1,14 @@ --- Title: 'async' -Description: 'Allows users to define an asynchronous function in Python.' -Subjects: +Description: 'Allows users to define an asynchronous function in Python.' +Subjects: - 'Computer Science' - 'Data Science' -Tags: +Tags: - 'Async Await' - 'Functions' - 'Python' -CatalogContent: +CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- From 7319a25ec326f32bc3334ba2ad48aa09abde9d42 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 29 Oct 2025 19:35:14 +0530 Subject: [PATCH 4/4] minor fix --- content/python/concepts/keywords/terms/async/async.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/keywords/terms/async/async.md b/content/python/concepts/keywords/terms/async/async.md index bd602fb42bf..7ce3a5c0115 100644 --- a/content/python/concepts/keywords/terms/async/async.md +++ b/content/python/concepts/keywords/terms/async/async.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`async`** keyword defines an asynchronous function in Python. Asynchronous functions let a program handle multiple tasks concurrently without waiting for each to finish, improving efficiency and enabling non-blocking execution. +The **`async`** keyword defines an [asynchronous function](https://www.codecademy.com/article/wix-what-is-async-and-await) in Python. Asynchronous functions let a program handle multiple tasks concurrently without waiting for each to finish, improving efficiency and enabling non-blocking execution. ## Syntax