-
Couldn't load subscription status.
- Fork 4.3k
Added [Term Entry] Python keywords: await #7736 #7762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+81
−0
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a0b4172
Added [Term Entry] Python keywords: await #7736
Anshusinghh ab80a45
minor fixes with the content
mamtawardhani 26357ac
Update await.md
mamtawardhani 361ff0a
Merge branch 'main' into docs/update
mamtawardhani 5beff0f
Merge branch 'main' into docs/update
Radhika-okhade d870127
Update content/python/concepts/keywords/terms/await/await.md
Radhika-okhade 0b42d4c
Update content/python/concepts/keywords/terms/await/await.md
Radhika-okhade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| --- | ||
| Title: 'await' | ||
| Description: 'Suspends the execution of an asynchronous function until the awaited coroutine, task, or awaitable object finishes.' | ||
| Subjects: | ||
| - 'Computer Science' | ||
| - 'Web Development' | ||
| Tags: | ||
| - 'Async Await' | ||
| - 'Functions' | ||
| - 'Python' | ||
| CatalogContent: | ||
| - 'learn-python-3' | ||
| - 'paths/computer-science' | ||
| --- | ||
|
|
||
| The **`await`** keyword suspends the execution of an asynchronous function until the awaited [coroutine](https://docs.python.org/3/library/asyncio-task.html#id2), task, or awaitable object completes. It allows asynchronous code to look and behave like synchronous code. | ||
Radhika-okhade marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Syntax | ||
|
|
||
| ```pseudo | ||
| await expression | ||
| ``` | ||
|
|
||
| > **Note:** The `await` keyword can only be used inside an `async` function. Using it outside an async function will raise a `SyntaxError`. | ||
|
|
||
| ## Example 1 | ||
|
|
||
| This example pauses execution while waiting for simulated data to be fetched: | ||
|
|
||
| ```py | ||
| import asyncio | ||
|
|
||
| async def fetch_data(): | ||
| print("Fetching data...") | ||
| await asyncio.sleep(1) | ||
| print("Data received!") | ||
| return {"data": 42} | ||
|
|
||
| async def main(): | ||
| result = await fetch_data() | ||
| print("Result:", result) | ||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| Here is the output: | ||
|
|
||
| ```shell | ||
| Fetching data... | ||
| Data received! | ||
| Result: {'data': 42} | ||
| ``` | ||
|
|
||
| In this example: | ||
|
|
||
| - `async` defines an asynchronous function that can contain `await`. | ||
| - `asyncio` provides tools for running asynchronous code, such as event loops, coroutines, and tasks. | ||
|
|
||
| ## Example 2 | ||
|
|
||
| This example waits for one second before returning and printing a message: | ||
|
|
||
| ```py | ||
| import asyncio | ||
|
|
||
| async def greet(): | ||
| await asyncio.sleep(1) | ||
| return "Hello from await!" | ||
|
|
||
| async def main(): | ||
| message = await greet() | ||
| print(message) | ||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| The output of this code is: | ||
|
|
||
| ```shell | ||
| Hello from await! | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.