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
c6fba92e
Kaydet (Commit)
c6fba92e
authored
Ara 03, 2013
tarafından
Victor Stinner
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
asyncio doc: add one more example of coroutines
üst
cc157516
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
1 deletion
+65
-1
asyncio-task.rst
Doc/library/asyncio-task.rst
+65
-1
No files found.
Doc/library/asyncio-task.rst
Dosyayı görüntüle @
c6fba92e
...
@@ -231,10 +231,14 @@ Task functions
...
@@ -231,10 +231,14 @@ Task functions
the timeout occurs are returned in the second set.
the timeout occurs are returned in the second set.
Examples
--------
.. _asyncio-hello-world-coroutine:
.. _asyncio-hello-world-coroutine:
Example: Hello World (coroutine)
Example: Hello World (coroutine)
--------------------------------
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Print ``Hello World`` every two seconds, using a coroutine::
Print ``Hello World`` every two seconds, using a coroutine::
...
@@ -253,3 +257,63 @@ Print ``Hello World`` every two seconds, using a coroutine::
...
@@ -253,3 +257,63 @@ Print ``Hello World`` every two seconds, using a coroutine::
.. seealso::
.. seealso::
:ref:`Hello World example using a callback <asyncio-hello-world-callback>`.
:ref:`Hello World example using a callback <asyncio-hello-world-callback>`.
Example: Chains coroutines and parallel execution
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Example chaining coroutines and executing multiple coroutines in parallel::
import asyncio
@asyncio.coroutine
def compute(x, y):
print("Start computing %s + %s" % (x, y))
yield from asyncio.sleep(3.0)
return x + y
@asyncio.coroutine
def print_sum(x, y):
result = yield from compute(x, y)
print("%s + %s = %s" % (x, y, result))
@asyncio.coroutine
def wait_task(task):
while 1:
done, pending = yield from asyncio.wait([task], timeout=1.0)
if done:
break
print("Compute in progress...")
asyncio.get_event_loop().stop()
print("Schedule tasks")
task = asyncio.async(print_sum(1, 2))
asyncio.async(wait_task(task))
print("Execute tasks")
loop = asyncio.get_event_loop()
loop.run_forever()
loop.close()
Output::
Schedule tasks
Execute tasks
Start computing 1 + 2
Compute in progress...
Compute in progress...
1 + 2 = 3
Details:
* ``compute()`` is chained to ``print_sum()``: ``print_sum()`` coroutine waits
until ``compute()`` is complete. Coroutines are executed in parallel:
``wait_task()`` is executed while ``compute()`` is blocked in
``asyncio.sleep(3.0)``.
* Coroutines are not executed before the loop is running: ``"Execute tasks"``
is written before ``"Start computing 1 + 2"``.
* ``wait_task()`` stops the event loop when ``print_sum()`` is done.
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