Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'tornado.ioloop.IOLoop.instance().start()' giving me an error?

I'm new to tornado. I built this very basic tornado request handler that I expected would return "Hello World" on a GET:

import tornado
import tornado.web

class HelloWorldHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous 
    def get(self, *args):
        self.write("Hello World")
        self.finish() 

if __name__=="__main__":
    app = tornado.web.Application([
        (r'/help', HelloWorldHandler),
    ], cookie_secret="__SHH_DONT_TELL__")

    app.listen(5001)
    tornado.ioloop.IOLoop.instance().start()

But when I run it I get:

Traceback (most recent call last):

  File "<ipython-input-1-4bf86d0b596e>", line 1, in <module>
    runfile('D:/Python/notebooks/my_tornado/temp.py', wdir='D:/Python/notebooks/my_tornado')

  File "C:\WinPython-64bit-3.5.2.3\python-3.5.2.amd64\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\WinPython-64bit-3.5.2.3\python-3.5.2.amd64\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Python/notebooks/my_tornado/temp.py", line 23, in <module>
    tornado.ioloop.IOLoop.instance().start()

  File "C:\WinPython-64bit-3.5.2.3\python-3.5.2.amd64\lib\site-packages\zmq\eventloop\ioloop.py", line 162, in start
    super(ZMQIOLoop, self).start()

  File "C:\WinPython-64bit-3.5.2.3\python-3.5.2.amd64\lib\site-packages\tornado\ioloop.py", line 752, in start
    raise RuntimeError("IOLoop is already running")

RuntimeError: IOLoop is already running

However, it seems to work. When I hit http://localhost:5001/help from Chrome, I get:

Hello World

If I kill it and comment out the last line (#tornado.ioloop.IOLoop.instance().start()), then

  1. I don't get the error
  2. It still seems like everything works.

All the docs say I need the last line, but it doesn't seem like I do. Should I just leave it off? Can anyone explain why I'm seeing this?

NOTE1: The 'help' for app.listen explicitly states:

Note that after calling this method you still need to call
IOLoop.current().start() to start the server.

However, I get the error if I use current() or instance() and elsewhere in the docs it says using instance() is preferred in this case.

NOTE2: I am definitely not running another instance of tornado or this program. This happens when I do a clean boot of my system and then run the code. I am running the code inside Spyder with an IronPython console, not sure if that is important or not.

UPDATE: I ran this script on AWS inside of a docker running python2. I didn't see any error (not sure if they were just suppressed.) Seems like this is specific to my Windows install? I am running an updated WinPython with the most current tornado.

like image 338
RobertB Avatar asked Mar 13 '17 17:03

RobertB


1 Answers

The IPython Notebook is already running a Tornado IOLoop. That's why you see that "IOLoop already running" error. You'll also notice that IPython has configured a special kind of loop, the ZMQIOLoop, which is not Tornado's default loop type.

It's best not to use IPython when you are developing and testing a Tornado web application.

like image 119
A. Jesse Jiryu Davis Avatar answered Oct 22 '22 16:10

A. Jesse Jiryu Davis