I probably should not be doing this but I was curious. Given the boilerplate code below:
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
If I enter this in the Ipython console, the server serves up the page. If I hit ctrl+c or cherrypy.server.stop()
the server stops. No problem.
However, when I attempt to do the same inside Spyder or Ipython Notebook, I can serve up "hello world" just fine, but cannot call cherrypy.server.stop() or interrupt the kernel.
Why is this? Even better, is there a way around this?
The quickstart
method blocks the thread on which gets called.
Basically it calls cherrypy.engine.block
.
But you can also directly mount your application and call the methods on the engine.
>>> cherrypy.tree.mount(RootApp(), '')
>>> # you can do some config with cherrypy.config or on the mount third argument.
>>> cherrypy.engine.start()
>>> import webbrowser
>>> webbrowser.open('localhost:8080')
The interpreter would not be blocked. You can stop the engine with cherrypy.engine.stop
and can be restarted.
However, cherrypy is thread based and can get in conflict with another library that assume that it has control over all the threads, so be aware.
Also if you don't call cherrypy.engine.stop
and finish the interpreter, the interpreter will be "hanged" waiting for the threads that cherrypy
is using. So stop the engine first.
I have just tested this with the IPython notebook and is working fine.