Update async features examples for Python 3.14 - #798
Open
lpozo wants to merge 1 commit into
Open
Conversation
Refresh the code for the "Getting Started With Async Features in Python" tutorial update. - Replace the `done`-flag loops with `while tasks:`, and iterate over a copy so removing an exhausted generator can't skip a task - Switch `asyncio.gather()` to `async with asyncio.TaskGroup()` in examples 4 and 6 (Python 3.11+) - Guard each HTTP request in example 6 with `asyncio.timeout(10)` - Rename the `queue` parameter to `work_queue` so it stops shadowing the `queue` module - Drop the stray `from builtins import range` in example 2 - Move the URL lists to https and replace the dead entries (twitter.com now redirects to x.com, yahoo.com returns 404) - Refresh requirements.txt to current releases and drop the transitive pins All six examples verified on Python 3.14.6. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Refreshes the sample code for understanding-asynchronous-programming, which backs Getting Started With Async Features in Python. Paired with the light update on that tutorial (CMS clone: post 2283).
The repo copy had drifted further than the article itself — it still pinned
aiohttp==3.5.4andrequests==2.21.0from 2019.Changes
while tasks:instead of thedoneflag (examples 2, 3, 5). This came from a reader suggestion, and it also fixes a latent bug: the old loop removed items fromtaskswhile iterating over it, which can skip a task. The loop now iterates overtasks.copy().asyncio.gather()→async with asyncio.TaskGroup()(examples 4 and 6). Task groups are the recommended default since Python 3.11 and cancel remaining tasks when one fails.asyncio.timeout(10)around the HTTP request in example 6, so an unresponsive server can't stall a task indefinitely.queueparameter towork_queuein examples 2 and 3, where it was shadowing thequeuemodule.from builtins import rangein example 2 (an IDE artifact — it's a no-op on Python 3).https://, with the dead entries replaced.twitter.comnow redirects tox.com, andyahoo.comreturns 404 over https, so it was dropped.requirements.txtrefreshed to current releases, keeping only the three direct dependencies rather than the full frozen transitive set.Verification
All six examples were run on Python 3.14.6:
example_1.pyexample_2.pyexample_3.pyexample_4.pyexample_5.pyexample_6.pyExamples 5 and 6 hit the live network, so their timings vary between runs. The async version stays roughly twice as fast as the sync one, which is the point the tutorial makes.
The code here is semantically identical to the code blocks in the updated article, with this repo's PEP 8 blank-line formatting preserved.
🤖 Generated with Claude Code