summaryrefslogtreecommitdiff
path: root/Doc/library/asyncio-eventloop.rst
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-10-11 16:16:27 +0200
committerVictor Stinner <victor.stinner@gmail.com>2014-10-11 16:16:27 +0200
commit03935e5985d271631847e29ab04ee8b32d971fa1 (patch)
treea94f722fa2a2dd8131bb68c8163ee6cf06b058c0 /Doc/library/asyncio-eventloop.rst
parent3abc5d3d59fc7a5dce5011f0de1a38e2a4112200 (diff)
downloadcpython-03935e5985d271631847e29ab04ee8b32d971fa1.tar.gz
asyncio doc: add examples showing the 3 ways to wait for data from an open
socket
Diffstat (limited to 'Doc/library/asyncio-eventloop.rst')
-rw-r--r--Doc/library/asyncio-eventloop.rst60
1 files changed, 56 insertions, 4 deletions
diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst
index ed38512080..59682d3f90 100644
--- a/Doc/library/asyncio-eventloop.rst
+++ b/Doc/library/asyncio-eventloop.rst
@@ -339,6 +339,10 @@ On Windows with :class:`ProactorEventLoop`, these methods are not supported.
Stop watching the file descriptor for write availability.
+The :ref:`watch a file descriptor for read events <asyncio-watch-read-event>`
+example uses the low-level :meth:`BaseEventLoop.add_reader` method to register
+the file descriptor of a socket.
+
Low-level socket operations
---------------------------
@@ -663,10 +667,59 @@ Print ``"Hello World"`` every two seconds using a callback scheduled by the
uses a :ref:`coroutine <coroutine>`.
-Example: Set signal handlers for SIGINT and SIGTERM
----------------------------------------------------
+.. _asyncio-watch-read-event:
+
+Watch a file descriptor for read events
+---------------------------------------
+
+Wait until a file descriptor received some data using the
+:meth:`BaseEventLoop.add_reader` method and then close the event loop::
+
+ import asyncio
+ import socket
+
+ # Create a pair of connected file descriptors
+ rsock, wsock = socket.socketpair()
+ loop = asyncio.get_event_loop()
+
+ def reader():
+ data = rsock.recv(100)
+ print("Received:", data.decode())
+ # We are done: unregister the register
+ loop.remove_reader(rsock)
+ # Stop the event loop
+ loop.stop()
+
+ # Wait for read event
+ loop.add_reader(rsock, reader)
+
+ # Simulate the reception of data from the network
+ loop.call_soon(wsock.send, 'abc'.encode())
+
+ # Run the event loop
+ loop.run_forever()
+
+ # We are done, close sockets and the event loop
+ rsock.close()
+ wsock.close()
+ loop.close()
+
+.. seealso::
+
+ The :ref:`register an open socket to wait for data using a protocol
+ <asyncio-register-socket>` example uses a low-level protocol created by the
+ :meth:`BaseEventLoop.create_connection` method.
+
+ The :ref:`register an open socket to wait for data using streams
+ <asyncio-register-socket-streams>` example uses high-level streams
+ created by the :func:`open_connection` function in a coroutine.
+
-Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`::
+Set signal handlers for SIGINT and SIGTERM
+------------------------------------------
+
+Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM` using
+the :meth:`BaseEventLoop.add_signal_handler` method::
import asyncio
import functools
@@ -688,4 +741,3 @@ Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`::
loop.run_forever()
finally:
loop.close()
-