2016-05-17 111 views
0

我想構建一個使用Tornado和Asyncio的tcp服務器。在龍捲風的documentation他們說,awaitasync應該爲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' 

回答

1

變化Server(io_loop=IOLoop.current().asyncio_loop)Server(io_loop=IOLoop.current())因爲服務器等待IOLoop,而不是ASYNCIO循環本身。

+0

現在我有這個錯誤 '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'從來沒有等待 回調(連接,地址)' –

+0

這似乎與這個問題有關:http://stackoverflow.com/qu estions/35542864 /如何使用的 - 蟒蛇-3-5的風格,異步和等待功能於龍捲風 – IdleGandalf