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
a881a7f2
Commit
a881a7f2
authored
Dec 09, 2013
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asyncio doc: explain why the loop is running twice
parent
d8f11e92
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
29 deletions
+36
-29
Doc/library/asyncio-protocol.rst
Doc/library/asyncio-protocol.rst
+36
-29
No files found.
Doc/library/asyncio-protocol.rst
View file @
a881a7f2
...
@@ -582,6 +582,40 @@ Network functions
...
@@ -582,6 +582,40 @@ Network functions
Protocol example: TCP echo server and client
Protocol example: TCP echo server and client
============================================
============================================
Echo client
-----------
TCP echo client example, send data and wait until the connection is closed::
import asyncio
class EchoClient(asyncio.Protocol):
message = 'This is the message. It will be echoed.'
def connection_made(self, transport):
transport.write(self.message.encode())
print('data sent: {}'.format(self.message))
def data_received(self, data):
print('data received: {}'.format(data.decode()))
def connection_lost(self, exc):
print('server closed the connection')
asyncio.get_event_loop().stop()
loop = asyncio.get_event_loop()
coro = loop.create_connection(EchoClient, '127.0.0.1', 8888)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()
The event loop is running twice. The
:meth:`~BaseEventLoop.run_until_complete` method is preferred in this short
example to raise an exception if the server is not listening, instead of
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 more running, so there is no need to stop the loop in case of an error.
Echo server
Echo server
-----------
-----------
...
@@ -603,8 +637,8 @@ TCP echo server example, send back received data and close the connection::
...
@@ -603,8 +637,8 @@ TCP echo server example, send back received data and close the connection::
self.transport.close()
self.transport.close()
loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop()
task
= loop.create_server(EchoServer, '127.0.0.1', 8888)
coro
= loop.create_server(EchoServer, '127.0.0.1', 8888)
server = loop.run_until_complete(
task
)
server = loop.run_until_complete(
coro
)
print('serving on {}'.format(server.sockets[0].getsockname()))
print('serving on {}'.format(server.sockets[0].getsockname()))
try:
try:
...
@@ -621,30 +655,3 @@ methods are asynchronous. ``yield from`` is not needed because these transport
...
@@ -621,30 +655,3 @@ methods are asynchronous. ``yield from`` is not needed because these transport
methods don't return coroutines.
methods don't return coroutines.
Echo client
-----------
TCP echo client example, send data and wait until the connection is closed::
import asyncio
class EchoClient(asyncio.Protocol):
message = 'This is the message. It will be echoed.'
def connection_made(self, transport):
transport.write(self.message.encode())
print('data sent: {}'.format(self.message))
def data_received(self, data):
print('data received: {}'.format(data.decode()))
def connection_lost(self, exc):
print('server closed the connection')
asyncio.get_event_loop().stop()
loop = asyncio.get_event_loop()
task = loop.create_connection(EchoClient, '127.0.0.1', 8888)
loop.run_until_complete(task)
loop.run_forever()
loop.close()
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