Repository navigation
gh-145912: Add "CPU-bound" and "I/O-bound" to glossary, link existing mentions #162886
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -372,6 +372,18 @@ Glossary | |
| :keyword:`async with` keywords. These were introduced | ||
| by :pep:`492`. | ||
|
|
||
| CPU-bound | ||
| Describes a task that spends most of its time computing, which keeps a | ||
| CPU core busy. Running such tasks :term:`concurrently <concurrency>` on | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "running multiple such tasks" |
||
| one core does not finish the work sooner, because they take turns using | ||
| that core, but running them in :term:`parallel <parallelism>` on several | ||
| cores can. For example, if sorting a large list takes one second, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorting is maybe not the best example, parallel sorting being non-trivial. Summing a list of integers, maybe? |
||
| sorting three of them takes about three seconds on one core and can take | ||
| as little as one second on three cores. With the | ||
| :term:`global interpreter lock`, only one thread runs Python bytecode at | ||
| a time, so threads do not speed up CPU-bound Python code. See also | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Free-threading? |
||
| :term:`I/O-bound`. | ||
|
|
||
| CPython | ||
| The canonical implementation of the Python programming language, as | ||
| distributed on `python.org <https://www.python.org>`_. The term "CPython" | ||
|
|
@@ -767,6 +779,15 @@ Glossary | |
| compare unequal (except with themselves), and their hash value is derived | ||
| from their :func:`id`. | ||
|
|
||
| I/O-bound | ||
| Describes a task that spends most of its time waiting for input or | ||
| output, such as a network response or a disk read, and little of it using | ||
| the CPU. While one I/O-bound task waits, the program can work on | ||
| another, so running such tasks :term:`concurrently <concurrency>` lets | ||
| their waits overlap, even on a single CPU core. For example, three | ||
| downloads that each take one second can finish in about one second when | ||
| run concurrently, instead of three. See also :term:`CPU-bound`. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "instead of three when run sequentially." |
||
|
|
||
| IDLE | ||
| An Integrated Development and Learning Environment for Python. | ||
| :ref:`idle` is a basic editor and interpreter environment | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1061,13 +1061,14 @@ Running in threads | |
| Return a coroutine that can be awaited to get the eventual result of *func*. | ||
|
|
||
| This coroutine function is primarily intended to be used for executing | ||
| IO-bound functions/methods that would otherwise block the event loop if | ||
| :term:`I/O-bound` functions/methods that would otherwise block | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use it for CPU-bound tasks too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I guess that's change of intention so maybe should be different PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh but I see it's mentioned later, so it is OK with caveats. |
||
| the event loop if | ||
| they were run in the main thread. For example:: | ||
|
|
||
| def blocking_io(): | ||
| print(f"start blocking_io at {time.strftime('%X')}") | ||
| # Note that time.sleep() can be replaced with any blocking | ||
| # IO-bound operation, such as file operations. | ||
| # I/O-bound operation, such as file operations. | ||
| time.sleep(1) | ||
| print(f"blocking_io complete at {time.strftime('%X')}") | ||
|
|
||
|
|
@@ -1098,9 +1099,10 @@ Running in threads | |
| .. note:: | ||
|
|
||
| Due to the :term:`GIL`, ``asyncio.to_thread()`` can typically only be used | ||
| to make IO-bound functions non-blocking. However, for extension modules | ||
| to make I/O-bound functions non-blocking. However, for extension modules | ||
| that release the GIL or alternative Python implementations that don't | ||
| have one, ``asyncio.to_thread()`` can also be used for CPU-bound functions. | ||
| have one, ``asyncio.to_thread()`` can also be used for | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like free-threading? |
||
| :term:`CPU-bound` functions. | ||
|
|
||
| .. versionadded:: 3.9 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,8 @@ asyncio and free-threaded Python | |
|
|
||
| asyncio uses an event loop as a scheduler to enable highly efficient | ||
| concurrency by switching between tasks to allow non-blocking I/O | ||
| operations. This results in better performance for I/O-bound use | ||
| cases. It also allows off-loading CPU-bound work to a thread or | ||
| operations. This results in better performance for :term:`I/O-bound` use | ||
| cases. It also allows off-loading :term:`CPU-bound` work to a thread or | ||
| process pool, but that is still limited by the :term:`global | ||
| interpreter lock` in CPython. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, could mention free-threading. |
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"most of its time computing, for example doing mathematical calculations, or string manipulation, or or operating on an in-memory data structure. This keeps CPU core busy."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer we don't add too many examples, it overloads the definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think I'll just use a better example than string sorting. I'll give it a think. At one point this talked about compressing in-memory files but that makes the reader think about I/O which is exactly what this entry isn't about.