Tornado: RuntimeError: There is no current event loop in thread 'Thread-1'.

Created on 9 Apr 2018  路  2Comments  路  Source: tornadoweb/tornado

I have this code:

#!/usr/bin/env python3

import threading
import tornado.ioloop
import tornado.web
from tornado import gen

class RequestHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous
    @gen.coroutine
    def get(self, path):
        self.write("Test")
        self.finish()

class WebServer(threading.Thread):
    def run(self):
        application = tornado.web.Application([
            (r"/(.*)", RequestHandler)])
        application.listen(12345)
        tornado.ioloop.IOLoop.instance().start()

WebServer().start()

It works just fine on Linux, however, on Mac I get an exception:

$ python3 testtornado.rb
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "testtornado.rb", line 20, in run
    application.listen(12345)
  File "/usr/local/lib/python3.6/site-packages/tornado/web.py", line 1986, in listen
    server.listen(port, address)
  File "/usr/local/lib/python3.6/site-packages/tornado/tcpserver.py", line 145, in listen
    self.add_sockets(sockets)
  File "/usr/local/lib/python3.6/site-packages/tornado/tcpserver.py", line 159, in add_sockets
    sock, self._handle_connection)
  File "/usr/local/lib/python3.6/site-packages/tornado/netutil.py", line 219, in add_accept_handler
    io_loop = IOLoop.current()
  File "/usr/local/lib/python3.6/site-packages/tornado/ioloop.py", line 282, in current
    loop = asyncio.get_event_loop()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py", line 694, in get_event_loop
    return get_event_loop_policy().get_event_loop()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py", line 602, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-1'.

Any ideas?

I'm using tornado (5.0.2) and Python 3.6.5.

Most helpful comment

Ah, bad searching on my part, I found the solution here: https://github.com/tornadoweb/tornado/issues/2308

e.g.

#!/usr/bin/env python3

import threading
import tornado.ioloop
import tornado.web
from tornado import gen
import asyncio

class RequestHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous
    @gen.coroutine
    def get(self, path):
        self.write("Test")
        self.finish()

class WebServer(threading.Thread):
    def run(self):
        asyncio.set_event_loop(asyncio.new_event_loop())
        application = tornado.web.Application([
            (r"/(.*)", RequestHandler)])
        application.listen(12345)
        tornado.ioloop.IOLoop.instance().start()

WebServer().start()

All 2 comments

Ah, bad searching on my part, I found the solution here: https://github.com/tornadoweb/tornado/issues/2308

e.g.

#!/usr/bin/env python3

import threading
import tornado.ioloop
import tornado.web
from tornado import gen
import asyncio

class RequestHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous
    @gen.coroutine
    def get(self, path):
        self.write("Test")
        self.finish()

class WebServer(threading.Thread):
    def run(self):
        asyncio.set_event_loop(asyncio.new_event_loop())
        application = tornado.web.Application([
            (r"/(.*)", RequestHandler)])
        application.listen(12345)
        tornado.ioloop.IOLoop.instance().start()

WebServer().start()

@rgaufman I did the same, but still getting that error:

# start listening
    logger.info('Tornado start listening on PORT %s' % PORT)
    app.listen(int(PORT))
    # LP: see https://github.com/tornadoweb/tornado/issues/2352
    asyncio.set_event_loop(asyncio.new_event_loop())
    ioloop.IOLoop.instance().start()

with error
2019-10-25 17:47:28 - ERROR - error There is no current event loop in thread 'ThreadPoolExecutor-2_0'.

in the handler I did

@tornado.web.asynchronous
    @gen.coroutine
    def get(self, *args):
        #...

problem is that using asyncio.set_event_loop(asyncio.new_event_loop()) I get any response at all from any handler!

Was this page helpful?
0 / 5 - 0 ratings