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
d8f11e92
Kaydet (Commit)
d8f11e92
authored
Ara 09, 2013
tarafından
Victor Stinner
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
asyncio: another Future example using add_done_callback()
üst
45c2fd9f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
2 deletions
+34
-2
asyncio-task.rst
Doc/library/asyncio-task.rst
+34
-2
No files found.
Doc/library/asyncio-task.rst
Dosyayı görüntüle @
d8f11e92
...
@@ -409,8 +409,8 @@ Details:
...
@@ -409,8 +409,8 @@ Details:
* ``wait_task()`` stops the event loop when ``print_sum()`` is done.
* ``wait_task()`` stops the event loop when ``print_sum()`` is done.
Example: Future
and get result
Example: Future
with run_until_complete()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^
Example combining a :class:`Future` and a :ref:`coroutine <coroutine>`::
Example combining a :class:`Future` and a :ref:`coroutine <coroutine>`::
...
@@ -432,3 +432,35 @@ The example waits for the completion of the future (which takes 1 second). The
...
@@ -432,3 +432,35 @@ The example waits for the completion of the future (which takes 1 second). The
coroutine is responsible of the computation. The event loop is notified when
coroutine is responsible of the computation. The event loop is notified when
the future is done (see the :meth:`Future.set_result` method).
the future is done (see the :meth:`Future.set_result` method).
Example: Future with run_until_complete()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The previous example can be written differently using the
:meth:`Future.add_done_callback` method::
import asyncio
@asyncio.coroutine
def slow_operation(future):
yield from asyncio.sleep(1)
future.set_result('Future in done!')
def exit(future):
print(future.result())
loop.stop()
loop = asyncio.get_event_loop()
future = asyncio.Future()
asyncio.Task(slow_operation(future))
future.add_done_callback(exit)
loop.run_forever()
loop.close()
The future is now responsible to display the result and stop the loop using the
``exit()`` callback.
.. note::
The coroutine is only executed when the event loop starts running, so it is
possible to add a "done callback" to the future after creating the task
scheduling the coroutine.
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