Tornado: recommended usage of AsyncIOMainLoop throws assertion

Created on 20 Apr 2016  路  4Comments  路  Source: tornadoweb/tornado

from tornado.platform.asyncio import AsyncIOMainLoop import asyncio AsyncIOMainLoop().install() asyncio.get_event_loop().run_forever()

install method throws assertion:
packages/tornado/ioloop.py", line 176, in install
assert not IOLoop.initialized()
AssertionError

Most helpful comment

the debug=True or autoreload=True option will also init ioloop.
and I should put:

     from tornado.platform.asyncio import AsyncIOMainLoop
     AsyncIOMainLoop().install()

in the first two line of the application file.
and at last, write

    application = Application()
    options.parse_command_line()
    application.listen(options.port)

    asyncio.get_event_loop().run_forever()

to start my application.

All 4 comments

Those four lines on their own are fine. If you're getting this error then it means that something _before_ these lines is initializing the IOLoop. Look through your code for anything that accesses IOLoop.current() or IOLoop.instance() at import time.

indeed there was a call of tornado.web.Application.listen, which leads to IOLoop.current() in tcpserver.add_sockets

the debug=True or autoreload=True option will also init ioloop.
and I should put:

     from tornado.platform.asyncio import AsyncIOMainLoop
     AsyncIOMainLoop().install()

in the first two line of the application file.
and at last, write

    application = Application()
    options.parse_command_line()
    application.listen(options.port)

    asyncio.get_event_loop().run_forever()

to start my application.

Those four lines on their own are fine. If you're getting this error then it means that something _before_ these lines is initializing the IOLoop. Look through your code for anything that accesses IOLoop.current() or IOLoop.instance() at import time.

thank you work for me

Was this page helpful?
0 / 5 - 0 ratings