Killing tornado with Ctrl+C/ Mixing with PyQt

477 views
Skip to first unread message

Ram

unread,
Jun 24, 2012, 7:21:12 PM6/24/12
to python-...@googlegroups.com

How can I run the webserver so that it can be killed using keyboard ctrl+c? I am running HTTPServer.listen() + IOLoop.instance.start. Also, are there any examples of mixing this IOLoop with PyQTs event loop. Is there something in particular that I need to keep in mind?

Thanks,
--Ram

Ben Darnell

unread,
Jun 24, 2012, 9:33:23 PM6/24/12
to python-...@googlegroups.com
In general, ctrl-c should work to stop a tornado server. (One known
exception is if you're using debug mode/autoreload on Windows or older
versions of Mac OS). However, there are a number of
non-tornado-specific ways that ctrl-c handling can go awry in python.
The most likely culprit is either a background thread without the
daemon attribute set, or if you're blocking on a call to a C function
that is not interruptible (Many of the synchronization methods in the
threading module are not interruptible when called without timeouts.
I'm sure there are non-threading-related examples too, but these are
the ones I run into the most). You can make ctrl-c more forceful (at
the expense of not printing a stack trace or running any finally
blocks when it happens) by running signal.signal(signal.SIGINT,
signal.SIG_DFL) at the start of your program.

My recommendation for combining multiple event loops is to give each
one its own thread. Use IOLoop.add_callback to transfer control from
the QT thread to the tornado thread, and an analogous method to send
things from tornado to QT. Be sure to use locks for any shared data
structures.

-Ben

Ram

unread,
Jun 25, 2012, 9:08:28 AM6/25/12
to python-...@googlegroups.com

Hi Ben,

On windows, when I run the following code, Ctrl+C does not work: (perhaps you can help me modify it so that it does work?)

import os
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from myapp import app

http_server = HTTPServer(WSGIContainer(app), ssl_options={
        "certfile": os.path.join(os.path.dirname(__file__), "test.crt"),
        "keyfile": os.path.join(os.path.dirname(__file__), "test.key")   
})

portnum = get_open_port()
http_server.listen(8888)
IOLoop.instance().start()

Or perhaps I'm doing something the wrong way.  BTW, I also found that ipython, that uses tornado, has the same problem. It prints out "press ctrl-c to exit" on windows for its notebook, and it wont exit when ctrl+c is pressed. I think it might be a similar issue.

Thanks,
--Ram

Ben Darnell

unread,
Jun 27, 2012, 1:04:04 PM6/27/12
to python-...@googlegroups.com
This code looks fine as far as tornado is concerned; I don't see why
ctrl-c wouldn't work.

I don't have any development experience on windows, but this post
suggests that select() on windows is uninterruptible:
http://code.activestate.com/lists/python-list/193293/
Adding a PeriodicCallback that does nothing may be enough to get the
system to check for interrupts.

-Ben
Reply all
Reply to author
Forward
0 new messages