Tornado asyncio tcp服务器抛出AttributeError:'_UnixSelectorEventLoop'
问题描述:
我想构建一个使用Tornado和Asyncio的tcp服务器。在龙卷风的documentation他们说,await
和async
应该为tornado.gen.coroutine
装饰更换工作,但我在开始本服务器有问题。我在这里做错了什么?Tornado asyncio tcp服务器抛出AttributeError:'_UnixSelectorEventLoop'
from tornado.ioloop import IOLoop
from tornado.tcpserver import TCPServer
from tornado.iostream import StreamClosedError
from tornado.platform.asyncio import to_asyncio_future
class Server(TCPServer):
async def handle_stream(self, stream, address):
"""Called when new IOStream object is ready for usage"""
print('Incoming connection from %r', address)
while True:
try:
message = await to_asyncio_future(stream.read_until('\n'.encode('utf8')))
print("Message: ", message)
except StreamClosedError:
print("Good bye!!")
break
if __name__ == "__main__":
IOLoop.configure('tornado.platform.asyncio.AsyncIOLoop')
server = Server(io_loop=IOLoop.current().asyncio_loop)
server.listen(7000)
IOLoop.current().start()
这是错误:
Traceback (most recent call last):
File "tornadoasyncioserver.py", line 41, in <module>
server.listen(7000)
File "/Users/user/tornadotcp/venv/lib/python3.5/site-packages/tornado/tcpserver.py", line 127, in listen
self.add_sockets(sockets)
File "/Users/user/tornadotcp/venv/lib/python3.5/site-packages/tornado/tcpserver.py", line 144, in add_sockets
io_loop=self.io_loop)
File "/Users/user/tornadotcp/venv/lib/python3.5/site-packages/tornado/netutil.py", line 275, in add_accept_handler
io_loop.add_handler(sock, accept_handler, IOLoop.READ)
AttributeError: '_UnixSelectorEventLoop' object has no attribute 'add_handler'
答
变化Server(io_loop=IOLoop.current().asyncio_loop)
到Server(io_loop=IOLoop.current())
因为服务器等待IOLoop,而不是ASYNCIO循环本身。
现在我有这个错误 'Traceback(最近调用最后一个): 文件“/Users/user/tornadotcp/venv/lib/python3.5/site-packages/tornado/tcpserver.py”,第271行,在_handle_connection self.io_loop.add_future(未来,拉姆达F:f.result()) 文件 “/Users/user/tornadotcp/venv/lib/python3.5/site-packages/tornado/ioloop.py”,行589,in add_future assert is_future(future) AssertionError /Users/user/tornadotcp/venv/lib/python3.5/site-packages/tornado/netutil.py:274:RuntimeWarning:coroutine'Server.handle_stream'从来没有等待 回调(连接,地址)' –
这似乎与这个问题有关:http://*.com/qu estions/35542864 /如何使用的 - 蟒蛇-3-5的风格,异步和等待功能于龙卷风 – IdleGandalf