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
7f314ed7
Kaydet (Commit)
7f314ed7
authored
Eki 15, 2014
tarafından
Victor Stinner
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
asyncio doc: write simpler examples to introduce asyncio
üst
9c422f3c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
24 deletions
+93
-24
asyncio-eventloop.rst
Doc/library/asyncio-eventloop.rst
+50
-11
asyncio-task.rst
Doc/library/asyncio-task.rst
+43
-13
No files found.
Doc/library/asyncio-eventloop.rst
Dosyayı görüntüle @
7f314ed7
...
...
@@ -649,24 +649,27 @@ Event loop examples
.. _asyncio-hello-world-callback:
Hello World with
a callback
---------------------------
Hello World with
call_soon()
---------------------------
-
Print ``"Hello World"`` every two seconds using a callback scheduled by the
:meth:`BaseEventLoop.call_soon` method::
Example using the :meth:`BaseEventLoop.call_soon` method to schedule a
callback. The callback displays ``"Hello World"`` and then stops the event
loop::
import asyncio
def
print_and_repeat
(loop):
def
hello_world
(loop):
print('Hello World')
loop.
call_later(2, print_and_repeat, loop
)
loop.
stop(
)
loop = asyncio.get_event_loop()
loop.call_soon(print_and_repeat, loop)
try:
loop.run_forever()
finally:
loop.close()
# Schedule a call to hello_world()
loop.call_soon(hello_world, loop)
# Blocking call interrupted by loop.stop()
loop.run_forever()
loop.close()
.. seealso::
...
...
@@ -674,6 +677,42 @@ Print ``"Hello World"`` every two seconds using a callback scheduled by the
uses a :ref:`coroutine <coroutine>`.
.. _asyncio-date-callback:
Display the current date with call_later()
------------------------------------------
Example of callback displaying the current date every second. The callback uses
the :meth:`BaseEventLoop.call_later` method to reschedule itself during 5
seconds, and then stops the event loop::
import asyncio
import datetime
def display_date(end_time, loop):
print(datetime.datetime.now())
if (loop.time() + 1.0) < end_time:
loop.call_later(1, display_date, end_time, loop)
else:
loop.stop()
loop = asyncio.get_event_loop()
# Schedule the first call to display_date()
end_time = loop.time() + 5.0
loop.call_soon(display_date, end_time, loop)
# Blocking call interrupted by loop.stop()
loop.run_forever()
loop.close()
.. seealso::
The :ref:`coroutine displaying the current date
<asyncio-date-coroutine>` example uses a :ref:`coroutine
<coroutine>`.
.. _asyncio-watch-read-event:
Watch a file descriptor for read events
...
...
Doc/library/asyncio-task.rst
Dosyayı görüntüle @
7f314ed7
...
...
@@ -77,30 +77,60 @@ Coroutines (and tasks) can only run when the event loop is running.
.. _asyncio-hello-world-coroutine:
Example:
"Hello World"
coroutine
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^
Example:
Hello World
coroutine
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Print ``"Hello World"`` every two seconds using a coroutine
::
Example of coroutine displaying ``"Hello World"``
::
import asyncio
@asyncio.coroutine
def greet_every_two_seconds():
def hello_world():
print("Hello World!")
loop = asyncio.get_event_loop()
# Blocking call which returns when the hello_world() coroutine is done
loop.run_until_complete(hello_world())
loop.close()
.. seealso::
The :ref:`Hello World with call_soon() <asyncio-hello-world-callback>`
example uses the :meth:`BaseEventLoop.call_soon` method to schedule a
callback.
.. _asyncio-date-coroutine:
Example: Coroutine displaying the current date
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Example of coroutine displaying the current date every second during 5 seconds
using the :meth:`sleep` function::
import asyncio
import datetime
@asyncio.coroutine
def display_date(loop):
end_time = loop.time() + 5.0
while True:
print('Hello World')
yield from asyncio.sleep(2)
print(datetime.datetime.now())
if (loop.time() + 1.0) >= end_time:
break
yield from asyncio.sleep(1)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(greet_every_two_seconds())
finally:
loop.close()
# Blocking call which returns when the display_date() coroutine is done
loop.run_until_complete(display_date(loop))
loop.close()
.. seealso::
The :ref:`Hello World with a callback <asyncio-hello-world-callback>`
example uses a callback scheduled by the :meth:`BaseEventLoop.call_soon`
method.
The :ref:`display the current date with call_later()
<asyncio-date-callback>` example uses a callback with the
:meth:`BaseEventLoop.call_later` method.
Example: Chain coroutines
^^^^^^^^^^^^^^^^^^^^^^^^^
...
...
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