Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
04e6df33
Commit
04e6df33
authored
Oct 11, 2014
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asyncio doc: add examples showing the 3 ways to wait for data from an open
socket
parent
6888b96c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
168 additions
and
10 deletions
+168
-10
Doc/library/asyncio-eventloop.rst
Doc/library/asyncio-eventloop.rst
+56
-4
Doc/library/asyncio-protocol.rst
Doc/library/asyncio-protocol.rst
+62
-6
Doc/library/asyncio-stream.rst
Doc/library/asyncio-stream.rst
+50
-0
No files found.
Doc/library/asyncio-eventloop.rst
View file @
04e6df33
...
...
@@ -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()
Doc/library/asyncio-protocol.rst
View file @
04e6df33
...
...
@@ -436,11 +436,11 @@ coroutine with ``yield from``. For example, the :meth:`StreamWriter.drain`
coroutine can be used to wait until the write buffer is flushed.
Protocol example
: TCP echo server and client
=================
===========================
Protocol example
s
=================
E
cho client
-----------
TCP e
cho client
-----------
----
TCP echo client example, send data and wait until the connection is closed::
...
...
@@ -473,8 +473,8 @@ having to write a short coroutine to handle the exception and stop the
running loop. At :meth:`~BaseEventLoop.run_until_complete` exit, the loop is
no longer running, so there is no need to stop the loop in case of an error.
E
cho server
-----------
TCP e
cho server
-----------
----
TCP echo server example, send back received data and close the connection::
...
...
@@ -511,4 +511,60 @@ TCP echo server example, send back received data and close the connection::
methods are asynchronous. ``yield from`` is not needed because these transport
methods are not coroutines.
.. _asyncio-register-socket:
Register an open socket to wait for data using a protocol
---------------------------------------------------------
Wait until a socket receives data using the
:meth:`BaseEventLoop.create_connection` method with a protocol, and then close
the event loop ::
import asyncio
import socket
# Create a pair of connected sockets
rsock, wsock = socket.socketpair()
loop = asyncio.get_event_loop()
class MyProtocol(asyncio.Protocol):
transport = None
def connection_made(self, transport):
self.transport = transport
def data_received(self, data):
print("Received:", data.decode())
# We are done: close the transport (it will call connection_lost())
self.transport.close()
def connection_lost(self, exc):
# The socket has been closed, stop the event loop
loop.stop()
# Register the socket to wait for data
connect_coro = loop.create_connection(MyProtocol, sock=rsock)
transport, protocol = loop.run_until_complete(connect_coro)
# 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:`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.
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.
Doc/library/asyncio-stream.rst
View file @
04e6df33
...
...
@@ -283,3 +283,53 @@ Usage::
python example.py http://example.com/path/page.html
or with HTTPS::
python example.py https://example.com/path/page.html
.. _asyncio-register-socket-streams:
Register an open socket to wait for data using streams
------------------------------------------------------
Coroutine waiting until a socket receives data using the
:func:`open_connection` function::
import asyncio
import socket
def wait_for_data(loop):
# Create a pair of connected sockets
rsock, wsock = socket.socketpair()
# Register the open socket to wait for data
reader, writer = yield from asyncio.open_connection(sock=rsock, loop=loop)
# Simulate the reception of data from the network
loop.call_soon(wsock.send, 'abc'.encode())
# Wait for data
data = yield from reader.read(100)
# Got data, we are done: close the socket
print("Received:", data.decode())
writer.close()
# Close the second socket
wsock.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(wait_for_data(loop))
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:`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.
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