Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
e247b46c
Unverified
Kaydet (Commit)
e247b46c
authored
Eyl 20, 2018
tarafından
Yury Selivanov
Kaydeden (comit)
GitHub
Eyl 20, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
bpo-33649: More improvements (GH-9439)
üst
8213eadd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
176 additions
and
47 deletions
+176
-47
asyncio-eventloop.rst
Doc/library/asyncio-eventloop.rst
+45
-4
asyncio-task.rst
Doc/library/asyncio-task.rst
+131
-43
No files found.
Doc/library/asyncio-eventloop.rst
Dosyayı görüntüle @
e247b46c
...
@@ -755,7 +755,7 @@ Watching file descriptors
...
@@ -755,7 +755,7 @@ Watching file descriptors
invoke *callback* with the specified arguments once *fd* is available for
invoke *callback* with the specified arguments once *fd* is available for
writing.
writing.
Use :func:`functools.partial` :ref:`to pass keywords
Use :func:`functools.partial` :ref:`to pass keyword
argument
s
<asyncio-pass-keywords>` to *func*.
<asyncio-pass-keywords>` to *func*.
.. method:: loop.remove_writer(fd)
.. method:: loop.remove_writer(fd)
...
@@ -969,7 +969,7 @@ Unix signals
...
@@ -969,7 +969,7 @@ Unix signals
Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
Raise :exc:`RuntimeError` if there is a problem setting up the handler.
Raise :exc:`RuntimeError` if there is a problem setting up the handler.
Use :func:`functools.partial` :ref:`to pass keywords
Use :func:`functools.partial` :ref:`to pass keyword
argument
s
<asyncio-pass-keywords>` to *func*.
<asyncio-pass-keywords>` to *func*.
.. method:: loop.remove_signal_handler(sig)
.. method:: loop.remove_signal_handler(sig)
...
@@ -996,11 +996,52 @@ Executing code in thread or process pools
...
@@ -996,11 +996,52 @@ Executing code in thread or process pools
The *executor* argument should be an :class:`concurrent.futures.Executor`
The *executor* argument should be an :class:`concurrent.futures.Executor`
instance. The default executor is used if *executor* is ``None``.
instance. The default executor is used if *executor* is ``None``.
Use :func:`functools.partial` :ref:`to pass keywords
Example::
<asyncio-pass-keywords>` to *func*.
import asyncio
import concurrent.futures
def blocking_io():
# File operations (such as logging) can block the
# event loop: run them in a thread pool.
with open('/dev/urandom', 'rb') as f:
return f.read(100)
def cpu_bound():
# CPU-bound operations will block the event loop:
# in general it is preferable to run them in a
# process pool.
return sum(i * i for i in range(10 ** 7))
async def main():
loop = asyncio.get_running_loop()
## Options:
# 1. Run in the default loop's executor:
result = await loop.run_in_executor(
None, blocking_io)
print('default thread pool', result)
# 2. Run in a custom thread pool:
with concurrent.futures.ThreadPoolExecutor() as pool:
result = await loop.run_in_executor(
pool, blocking_io)
print('custom thread pool', result)
# 3. Run in a custom process pool:
with concurrent.futures.ProcessPoolExecutor() as pool:
result = await loop.run_in_executor(
pool, cpu_bound)
print('custom process pool', result)
asyncio.run(main())
This method returns a :class:`asyncio.Future` object.
This method returns a :class:`asyncio.Future` object.
Use :func:`functools.partial` :ref:`to pass keyword arguments
<asyncio-pass-keywords>` to *func*.
.. versionchanged:: 3.5.3
.. versionchanged:: 3.5.3
:meth:`loop.run_in_executor` no longer configures the
:meth:`loop.run_in_executor` no longer configures the
``max_workers`` of the thread pool executor it creates, instead
``max_workers`` of the thread pool executor it creates, instead
...
...
Doc/library/asyncio-task.rst
Dosyayı görüntüle @
e247b46c
...
@@ -109,50 +109,89 @@ To actually run a coroutine asyncio provides three main mechanisms:
...
@@ -109,50 +109,89 @@ To actually run a coroutine asyncio provides three main mechanisms:
Awaitables
Awaitables
==========
==========
We say that an object is an *awaitable* object if it can be used
We say that an object is an **awaitable** object if it can be used
in an :keyword:`await` expression.
in an :keyword:`await` expression. Many asyncio APIs are designed to
accept awaitables.
There are three main types of *awaitable* objects:
**coroutines**, **Tasks**, and **Futures**.
.. rubric:: Coroutines and Tasks
Python coroutines are *awaitables*::
.. rubric:: Coroutines
Python coroutines are *awaitables* and therefore can be awaited from
other coroutines::
import asyncio
async def nested():
async def nested():
return 42
return 42
async def main():
async def main():
# Will print "42":
# Nothing happens if we just call "nested()".
print(await nested())
# (a coroutine object is created but not awaited)
nested()
# Let's do it differently now and await it:
print(await nested()) # will print "42".
asyncio.run(main())
.. important::
In this documentation the term "coroutine" can be used for
two closely related concepts:
* a *coroutine function*: an :keyword:`async def` function;
* a *coroutine object*: an object returned by calling a
*coroutine function*.
asyncio also supports legacy :ref:`generator-based
<asyncio_generator_based_coro>` coroutines.
.. rubric:: Tasks
*Tasks* are used to schedule coroutines *concurrently*.
*Tasks* are used to schedule coroutines *concurrently*.
See the previous :ref:`section <coroutine>` for an introduction
to coroutines and tasks.
Note that in this documentation the term "coroutine" can be used for
When a coroutine is wrapped into a *Task* with functions like
two closely related concepts:
:func:`asyncio.create_task` the coroutine is automatically
scheduled to run soon::
import asyncio
async def nested():
return 42
* a *coroutine function*: an :keyword:`async def` function;
async def main():
# Schedule nested() to run soon concurrently
# with "main()".
task = asyncio.create_task(nested())
* a *coroutine object*: object returned by calling a
# "task" can now be used to cancel "nested()", or
*coroutine function*.
# can simply be awaited to wait until it is complete:
await task
asyncio.run(main())
.. rubric:: Futures
.. rubric:: Futures
There is a dedicated section about the :ref:`asyncio Future object
A :class:`Future` is a special **low-level** awaitable object that
<asyncio-futures>`, but the concept is fundamental to asyncio so
represents an **eventual result** of an asynchronous operation.
it needs a brief introduction in this section.
When a Future object is *awaited* it means that the coroutine will
wait until the Future is resolved in some other place.
A Future is a special **low-level** awaitable object that represents
an **eventual result** of an asynchronous operation.
Future objects in asyncio are needed to allow callback-based code
Future objects in asyncio are needed to allow callback-based code
to be used with async/await.
to be used with async/await.
Normally
,
**there is no need** to create Future objects at the
Normally **there is no need** to create Future objects at the
application level code.
application level code.
Future objects, sometimes exposed by libraries and some asyncio
Future objects, sometimes exposed by libraries and some asyncio
APIs,
should
be awaited::
APIs,
can
be awaited::
async def main():
async def main():
await function_that_returns_a_future_object()
await function_that_returns_a_future_object()
...
@@ -163,6 +202,9 @@ APIs, should be awaited::
...
@@ -163,6 +202,9 @@ APIs, should be awaited::
some_python_coroutine()
some_python_coroutine()
)
)
A good example of a low-level function that returns a Future object
is :meth:`loop.run_in_executor`.
Running an asyncio Program
Running an asyncio Program
==========================
==========================
...
@@ -192,8 +234,8 @@ Creating Tasks
...
@@ -192,8 +234,8 @@ Creating Tasks
.. function:: create_task(coro, \*, name=None)
.. function:: create_task(coro, \*, name=None)
Wrap the *coro* :ref:`coroutine <coroutine>` into a
Task and
Wrap the *coro* :ref:`coroutine <coroutine>` into a
:class:`Task`
schedule its execution. Return the Task object.
and
schedule its execution. Return the Task object.
If *name* is not ``None``, it is set as the name of the task using
If *name* is not ``None``, it is set as the name of the task using
:meth:`Task.set_name`.
:meth:`Task.set_name`.
...
@@ -259,17 +301,17 @@ Sleeping
...
@@ -259,17 +301,17 @@ Sleeping
Running Tasks Concurrently
Running Tasks Concurrently
==========================
==========================
.. awaitablefunction:: gather(\*
f
s, loop=None, return_exceptions=False)
.. awaitablefunction:: gather(\*
aw
s, loop=None, return_exceptions=False)
Run :ref:`awaitable objects <asyncio-awaitables>` in the *
f
s*
Run :ref:`awaitable objects <asyncio-awaitables>` in the *
aw
s*
sequence *concurrently*.
sequence *concurrently*.
If any awaitable in *
f
s* is a coroutine, it is automatically
If any awaitable in *
aw
s* is a coroutine, it is automatically
scheduled as a Task.
scheduled as a Task.
If all awaitables are completed successfully, the result is an
If all awaitables are completed successfully, the result is an
aggregate list of returned values. The order of result values
aggregate list of returned values. The order of result values
corresponds to the order of awaitables in *
f
s*.
corresponds to the order of awaitables in *
aw
s*.
If *return_exceptions* is ``True``, exceptions are treated the
If *return_exceptions* is ``True``, exceptions are treated the
same as successful results, and aggregated in the result list.
same as successful results, and aggregated in the result list.
...
@@ -279,7 +321,7 @@ Running Tasks Concurrently
...
@@ -279,7 +321,7 @@ Running Tasks Concurrently
If ``gather`` is *cancelled*, all submitted awaitables
If ``gather`` is *cancelled*, all submitted awaitables
(that have not completed yet) are also *cancelled*.
(that have not completed yet) are also *cancelled*.
If any Task or Future from the *
f
s* sequence is *cancelled*, it is
If any Task or Future from the *
aw
s* sequence is *cancelled*, it is
treated as if it raised :exc:`CancelledError` -- the ``gather()``
treated as if it raised :exc:`CancelledError` -- the ``gather()``
call is **not** cancelled in this case. This is to prevent the
call is **not** cancelled in this case. This is to prevent the
cancellation of one submitted Task/Future to cause other
cancellation of one submitted Task/Future to cause other
...
@@ -329,13 +371,13 @@ Running Tasks Concurrently
...
@@ -329,13 +371,13 @@ Running Tasks Concurrently
Shielding Tasks From Cancellation
Shielding Tasks From Cancellation
=================================
=================================
.. awaitablefunction:: shield(
fut
, \*, loop=None)
.. awaitablefunction:: shield(
aw
, \*, loop=None)
Protect an :ref:`awaitable object <asyncio-awaitables>`
Protect an :ref:`awaitable object <asyncio-awaitables>`
from being :meth:`cancelled <Task.cancel>`.
from being :meth:`cancelled <Task.cancel>`.
*
fut
* can be a coroutine, a Task, or a Future-like object. If
*
aw
* can be a coroutine, a Task, or a Future-like object. If
*
fut
* is a coroutine it is automatically scheduled as a Task.
*
aw
* is a coroutine it is automatically scheduled as a Task.
The statement::
The statement::
...
@@ -367,12 +409,12 @@ Shielding Tasks From Cancellation
...
@@ -367,12 +409,12 @@ Shielding Tasks From Cancellation
Timeouts
Timeouts
========
========
.. coroutinefunction:: wait_for(
fut
, timeout, \*, loop=None)
.. coroutinefunction:: wait_for(
aw
, timeout, \*, loop=None)
Wait for the *
fut
* :ref:`awaitable <asyncio-awaitables>`
Wait for the *
aw
* :ref:`awaitable <asyncio-awaitables>`
to complete with a timeout.
to complete with a timeout.
If *
fut
* is a coroutine it is automatically scheduled as a Task.
If *
aw
* is a coroutine it is automatically scheduled as a Task.
*timeout* can either be ``None`` or a float or int number of seconds
*timeout* can either be ``None`` or a float or int number of seconds
to wait for. If *timeout* is ``None``, block until the future
to wait for. If *timeout* is ``None``, block until the future
...
@@ -387,7 +429,7 @@ Timeouts
...
@@ -387,7 +429,7 @@ Timeouts
The function will wait until the future is actually cancelled,
The function will wait until the future is actually cancelled,
so the total wait time may exceed the *timeout*.
so the total wait time may exceed the *timeout*.
If the wait is cancelled, the future *
fut
* is also cancelled.
If the wait is cancelled, the future *
aw
* is also cancelled.
The *loop* argument is deprecated and scheduled for removal
The *loop* argument is deprecated and scheduled for removal
in Python 4.0.
in Python 4.0.
...
@@ -415,22 +457,22 @@ Timeouts
...
@@ -415,22 +457,22 @@ Timeouts
# timeout!
# timeout!
.. versionchanged:: 3.7
.. versionchanged:: 3.7
When *
fut
* is cancelled due to a timeout, ``wait_for`` waits
When *
aw
* is cancelled due to a timeout, ``wait_for`` waits
for *
fut
* to be cancelled. Previously, it raised
for *
aw
* to be cancelled. Previously, it raised
:exc:`asyncio.TimeoutError` immediately.
:exc:`asyncio.TimeoutError` immediately.
Waiting Primitives
Waiting Primitives
==================
==================
.. coroutinefunction:: wait(
f
s, \*, loop=None, timeout=None,\
.. coroutinefunction:: wait(
aw
s, \*, loop=None, timeout=None,\
return_when=ALL_COMPLETED)
return_when=ALL_COMPLETED)
Run :ref:`awaitable objects <asyncio-awaitables>` in the *
f
s*
Run :ref:`awaitable objects <asyncio-awaitables>` in the *
aw
s*
sequence concurrently and block until the condition specified
sequence concurrently and block until the condition specified
by *return_when*.
by *return_when*.
If any awaitable in *
f
s* is a coroutine, it is automatically
If any awaitable in *
aw
s* is a coroutine, it is automatically
scheduled as a Task.
scheduled as a Task.
Returns two sets of Tasks/Futures: ``(done, pending)``.
Returns two sets of Tasks/Futures: ``(done, pending)``.
...
@@ -471,12 +513,12 @@ Waiting Primitives
...
@@ -471,12 +513,12 @@ Waiting Primitives
Usage::
Usage::
done, pending = await asyncio.wait(
f
s)
done, pending = await asyncio.wait(
aw
s)
.. function:: as_completed(
f
s, \*, loop=None, timeout=None)
.. function:: as_completed(
aw
s, \*, loop=None, timeout=None)
Run :ref:`awaitable objects <asyncio-awaitables>` in the *
f
s*
Run :ref:`awaitable objects <asyncio-awaitables>` in the *
aw
s*
set concurrently. Return an iterator of :class:`Future` objects.
set concurrently. Return an iterator of :class:`Future` objects.
Each Future object returned represents the earliest result
Each Future object returned represents the earliest result
from the set of the remaining awaitables.
from the set of the remaining awaitables.
...
@@ -486,7 +528,7 @@ Waiting Primitives
...
@@ -486,7 +528,7 @@ Waiting Primitives
Example::
Example::
for f in as_completed(
f
s):
for f in as_completed(
aw
s):
earliest_result = await f
earliest_result = await f
# ...
# ...
...
@@ -679,6 +721,52 @@ Task Object
...
@@ -679,6 +721,52 @@ Task Object
A Task is *done* when the wrapped coroutine either returned
A Task is *done* when the wrapped coroutine either returned
a value, raised an exception, or the Task was cancelled.
a value, raised an exception, or the Task was cancelled.
.. method:: result()
Return the result of the Task.
If the Task is *done*, the result of the wrapped coroutine
is returned (or if the coroutine raised an exception, that
exception is re-raised.)
If the Task has been *cancelled*, this method raises
a :exc:`CancelledError` exception.
If the Task's result isn't yet available, this method raises
a :exc:`InvalidStateError` exception.
.. method:: exception()
Return the exception of the Task.
If the wrapped coroutine raised an exception that exception
is returned. If the wrapped coroutine returned normally
this method returns ``None``.
If the Task has been *cancelled*, this method raises a
:exc:`CancelledError` exception.
If the Task isn't *done* yet, this method raises an
:exc:`InvalidStateError` exception.
.. method:: add_done_callback(callback, *, context=None)
Add a callback to be run when the Task is *done*.
This method should only be used in low-level callback-based code.
See the documentation of :meth:`Future.add_done_callback`
for more details.
.. method:: remove_done_callback(callback)
Remove *callback* from the callbacks list.
This method should only be used in low-level callback-based code.
See the documentation of :meth:`Future.remove_done_callback`
for more details.
.. method:: get_stack(\*, limit=None)
.. method:: get_stack(\*, limit=None)
Return the list of stack frames for this Task.
Return the list of stack frames for this Task.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment