mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
bpo-38192: Fix remaining passing of "loop" in the protocol examples (GH-16202)
See https://bugs.python.org/issue38192 . https://bugs.python.org/issue38192
This commit is contained in:
parent
f669581a95
commit
5d359cc62e
1 changed files with 6 additions and 5 deletions
|
|
@ -792,7 +792,7 @@ data, and waits until the connection is closed::
|
||||||
message = 'Hello World!'
|
message = 'Hello World!'
|
||||||
|
|
||||||
transport, protocol = await loop.create_connection(
|
transport, protocol = await loop.create_connection(
|
||||||
lambda: EchoClientProtocol(message, on_con_lost, loop),
|
lambda: EchoClientProtocol(message, on_con_lost),
|
||||||
'127.0.0.1', 8888)
|
'127.0.0.1', 8888)
|
||||||
|
|
||||||
# Wait until the protocol signals that the connection
|
# Wait until the protocol signals that the connection
|
||||||
|
|
@ -870,8 +870,8 @@ method, sends data and closes the transport when it receives the answer::
|
||||||
class EchoClientProtocol:
|
class EchoClientProtocol:
|
||||||
def __init__(self, message, on_con_lost):
|
def __init__(self, message, on_con_lost):
|
||||||
self.message = message
|
self.message = message
|
||||||
self.transport = None
|
|
||||||
self.on_con_lost = on_con_lost
|
self.on_con_lost = on_con_lost
|
||||||
|
self.transport = None
|
||||||
|
|
||||||
def connection_made(self, transport):
|
def connection_made(self, transport):
|
||||||
self.transport = transport
|
self.transport = transport
|
||||||
|
|
@ -927,9 +927,9 @@ Wait until a socket receives data using the
|
||||||
|
|
||||||
class MyProtocol(asyncio.Protocol):
|
class MyProtocol(asyncio.Protocol):
|
||||||
|
|
||||||
def __init__(self, loop):
|
def __init__(self, on_con_lost):
|
||||||
self.transport = None
|
self.transport = None
|
||||||
self.on_con_lost = loop.create_future()
|
self.on_con_lost = on_con_lost
|
||||||
|
|
||||||
def connection_made(self, transport):
|
def connection_made(self, transport):
|
||||||
self.transport = transport
|
self.transport = transport
|
||||||
|
|
@ -950,13 +950,14 @@ Wait until a socket receives data using the
|
||||||
# Get a reference to the event loop as we plan to use
|
# Get a reference to the event loop as we plan to use
|
||||||
# low-level APIs.
|
# low-level APIs.
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
|
on_con_lost = loop.create_future()
|
||||||
|
|
||||||
# Create a pair of connected sockets
|
# Create a pair of connected sockets
|
||||||
rsock, wsock = socket.socketpair()
|
rsock, wsock = socket.socketpair()
|
||||||
|
|
||||||
# Register the socket to wait for data.
|
# Register the socket to wait for data.
|
||||||
transport, protocol = await loop.create_connection(
|
transport, protocol = await loop.create_connection(
|
||||||
lambda: MyProtocol(loop), sock=rsock)
|
lambda: MyProtocol(on_con_lost), sock=rsock)
|
||||||
|
|
||||||
# Simulate the reception of data from the network.
|
# Simulate the reception of data from the network.
|
||||||
loop.call_soon(wsock.send, 'abc'.encode())
|
loop.call_soon(wsock.send, 'abc'.encode())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue