import asyncio
asyncio.tasks._DEBUG = True
class EchoServer(asyncio.Protocol):
def connection_made(self, transport):
name = transport.get_extra_info('sockname')
peername = transport.get_extra_info('peername')
print('Connection:',name,'<--',peername)
self.transport = transport
def data_received(self, data):
name = self.transport.get_extra_info('sockname')
peername = self.transport.get_extra_info('peername')
print('Got data:',name,'<--',peername,':',data.decode() )
if name[1] == 8888:
print("Making connection")
reader, writer = yield from asyncio.open_connection('127.0.0.1', 8889, loop=asyncio.get_event_loop())
else:
self.transport.write(data)
self.transport.close()
loop = asyncio.get_event_loop()
coro_1 = loop.create_server(EchoServer, '127.0.0.1', 8888)
coro_2 = loop.create_server(EchoServer, '127.0.0.1', 8889)
server_1 = loop.run_until_complete(coro_1)
server_2 = loop.run_until_complete(coro_2)
print('Serving on {}'.format(server_1.sockets[0].getsockname()))
print('Serving on {}'.format(server_2.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
print("^C caught, exiting")
finally:
server_1.close()
server_2.close()
loop.close()
但是,当我运行它时,它似乎甚至没有运行该data_received
函数:
Serving on ('127.0.0.1', 8888)
Serving on ('127.0.0.1', 8889)
Connection: ('127.0.0.1', 8888) <-- ('127.0.0.1', 36580)
但是当我注释掉这yield from
条线时,它会按照我期望的方式运行。
Serving on ('127.0.0.1', 8888)
Serving on ('127.0.0.1', 8889)
Connection: ('127.0.0.1', 8888) <-- ('127.0.0.1', 36581)
Got data: ('127.0.0.1', 8888) <-- ('127.0.0.1', 36581) : Hi, gaise!
Making connection
我试过添加:
@asyncio.coroutine
def do_a_thing():
print("hey, it's a thing!")
def try_stuff():
print('Trying')
yield from asyncio.async(do_a_thing())
try_stuff
而是打电话。这确实会输出“正在建立连接”,但不会输出“正在尝试”。通过将yield from
线路更改为简单的调用确实会输出“正在尝试”。如果我从中删除协程装饰器,do_a_thing
我会得到输出。
对我来说重要的是我正在尝试创建到第二台服务器的异步连接。但显然,存在yield from
阻止了任何进一步的处理(至少我可以说 - 它肯定会随着打印而停止)。
那么我做错了什么,我该如何解决这个问题?