5

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.

1
  • I'm not encountering this error. Are you running the code from a specific IDE or from the command line?
    – Unlocked
    Mar 13, 2017 at 19:58

1 Answer 1

6

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.

1
  • 1
    Well that explains it. It matches what I am experiencing. I did not know that. I am using Spyder, so I am able to use either a "Python" console or an "IPython" console and using the former, it works without error, using the latter it gives the error.
    – RobertB
    Mar 14, 2017 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.