diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-10-11 14:16:27 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-10-11 14:16:27 (GMT) |
commit | 04e6df330ddbec28cbbcba0f36235040e267692d (patch) | |
tree | a5ace7b5a2966b84b74fcdc5cecee7398197c350 /Doc/library/asyncio-eventloop.rst | |
parent | 6888b96cee44364f5414b0e4dbfc69dba8b639b4 (diff) | |
download | cpython-04e6df330ddbec28cbbcba0f36235040e267692d.zip cpython-04e6df330ddbec28cbbcba0f36235040e267692d.tar.gz cpython-04e6df330ddbec28cbbcba0f36235040e267692d.tar.bz2 |
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.rst | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index ed38512..59682d3 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() - |