Re: [cherrypy-users] Cleanly stopping CherryPy with my own loop running.

2,032 views
Skip to first unread message

Eugene Van den Bulke

unread,
Jun 12, 2013, 1:46:28 AM6/12/13
to cherrypy-users
Use cherrypy.quickstart to make your life easier

or you will need to catch the signal ctrl+c and call cherrypy.engine.stop()


On Wed, Jun 12, 2013 at 12:43 PM, Ben Jeffery <b...@bj0.org> wrote:
I can't seem to have both my own loop running and have my program exit cleanly with CTRL-C.

This is a simplified version of my code right now:

cherrypy.tree.mount(mainPage(), "/")
cherrypy.engine.start()

while True:
print 'hello world'
time.sleep(5)

The loop and CherryPy both work, though when I try to exit the program with CTRL-C, only my loop will stop. CherryPy will still happily serve out pages to my browser. I know I can stop CherryPy with cherrypy.engine.stop(), though I'm not sure how I use it in this specific situation.

My programming experience is pretty limited, so I'm stumped right now. Any help is appreciated.

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-user...@googlegroups.com.
To post to this group, send email to cherryp...@googlegroups.com.
Visit this group at http://groups.google.com/group/cherrypy-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
EuGeNe -- follow me http://twitter.com/3kwa

Sylvain Hellegouarch

unread,
Jun 12, 2013, 1:48:43 AM6/12/13
to cherryp...@googlegroups.com
Hi Ben,

Try adding this:

cherrypy.engine.signals.subscribe()

Before the engine.start() call.


On Wed, Jun 12, 2013 at 4:43 AM, Ben Jeffery <b...@bj0.org> wrote:
I can't seem to have both my own loop running and have my program exit cleanly with CTRL-C.

This is a simplified version of my code right now:

cherrypy.tree.mount(mainPage(), "/")
cherrypy.engine.start()

while True:
print 'hello world'
time.sleep(5)

The loop and CherryPy both work, though when I try to exit the program with CTRL-C, only my loop will stop. CherryPy will still happily serve out pages to my browser. I know I can stop CherryPy with cherrypy.engine.stop(), though I'm not sure how I use it in this specific situation.

My programming experience is pretty limited, so I'm stumped right now. Any help is appreciated.

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-user...@googlegroups.com.
To post to this group, send email to cherryp...@googlegroups.com.
Visit this group at http://groups.google.com/group/cherrypy-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

Sylvain Hellegouarch

unread,
Jun 12, 2013, 4:50:46 AM6/12/13
to cherryp...@googlegroups.com
Eugene,

On Wed, Jun 12, 2013 at 7:46 AM, Eugene Van den Bulke <eugene.va...@gmail.com> wrote:
Use cherrypy.quickstart to make your life easier

or you will need to catch the signal ctrl+c and call cherrypy.engine.stop()




Indeed quickstart is a good option since it will handle the blocking main loop for you. However, if Ben wants to keep control over the loop, just engine.start will suffice (and publishing to the 'main' channel from your own loop is always a good idea). 

eugene.va...@gmail.com

unread,
Jun 12, 2013, 5:07:58 AM6/12/13
to cherryp...@googlegroups.com
On 12/06/2013, at 6:50 PM, Sylvain Hellegouarch <s...@defuze.org> wrote:

> Publishing to the 'main' channel from your own loop is always a good idea

Could you elaborate on that?

Sylvain Hellegouarch

unread,
Jun 12, 2013, 5:55:42 AM6/12/13
to cherryp...@googlegroups.com
Sure.

When you use engine.block(), the internal loop will publish to the 'main' channel every iteration. If you have a plugin subscribed to that channel, and you don't use the CherryPy's mainloop itself, e.g. no call to block(), then your plugin will never be hit. Therefore, you'd have to make sure that your own mainloop does publish to that channel. Just a call to cherrypy.engine.publish('main') would be required every iteration.

I can't recall if any of the built-in plugins subscribes to that channel.

eugene.va...@gmail.com

unread,
Jun 12, 2013, 6:00:20 AM6/12/13
to cherryp...@googlegroups.com
On 12/06/2013, at 7:55 PM, Sylvain Hellegouarch <s...@defuze.org> wrote:

On Wed, Jun 12, 2013 at 11:07 AM, <eugene.va...@gmail.com> wrote:
On 12/06/2013, at 6:50 PM, Sylvain Hellegouarch <s...@defuze.org> wrote:

> Publishing to the 'main' channel from your own loop is always a good idea

Could you elaborate on that?

When you use engine.block(), the internal loop will publish to the 'main' channel every iteration. If you have a plugin subscribed to that channel, and you don't use the CherryPy's mainloop itself, e.g. no call to block(), then your plugin will never be hit. Therefore, you'd have to make sure that your own mainloop does publish to that channel. Just a call to cherrypy.engine.publish('main') would be required every iteration.

Thanks! Very informative.

Ben Jeffery

unread,
Jun 12, 2013, 6:36:37 PM6/12/13
to cherryp...@googlegroups.com
Thanks for replying everyone,

I tried cherrypy.engine.signals.subscribe(), though I got an error. I looked into CherryPy's signal handlers and saw that it's now called: cherrypy.engine.signal_handler.subscribe()

Unfortunately, adding cherrypy.engine.signal_handler.subscribe() before cherrypy.engine.start() had no effect on my problem. My while loop terminates and CherryPy keeps running. 

When I use quickstart, CherryPy will serve out the webpage though the loop will only run when I press CTRL-C, which stops CherryPy. After pressing CTRL-C again, the program will exit just fine. To illustrate, this is what happens:

127.0.0.1 - - [12/Jun/2013:18:18:47] "GET / HTTP/1.1" 200 525 "" "Mozilla/5.0 (W
indows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.11
0 Safari/537.36"
[12/Jun/2013:18:23:03] ENGINE Keyboard Interrupt: shutting down bus
[12/Jun/2013:18:23:03] ENGINE Bus STOPPING
[12/Jun/2013:18:23:04] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer((
'0.0.0.0', 8080)) shut down
[12/Jun/2013:18:23:04] ENGINE Stopped thread '_TimeoutMonitor'.
[12/Jun/2013:18:23:04] ENGINE Stopped thread 'Autoreloader'.
[12/Jun/2013:18:23:04] ENGINE Bus STOPPED
[12/Jun/2013:18:23:04] ENGINE Bus EXITING
[12/Jun/2013:18:23:04] ENGINE Bus EXITED
[12/Jun/2013:18:23:04] ENGINE Waiting for child threads to terminate...
hello world
hello world
hello world
Traceback (most recent call last):
  File "genshitest.py", line 27, in <module>
    time.sleep(5)
KeyboardInterrupt


Calling cherrypy.engine.publish('main') with each iteration sounds promising, though I can't call it my loop doesn't even run. 

Ben Jeffery

unread,
Jun 12, 2013, 6:55:05 PM6/12/13
to cherryp...@googlegroups.com
I looked into catching CTRL-C and calling cherrypy.engine.stop() and it does what I need it to! This is the code that I used:

import signal

def signal_handler(signal, frame):
print 'Exiting...'
cherrypy.engine.stop()
sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

It does complain, is this anything to worry about?

C:\Python27\lib\site-packages\cherrypy\process\wspbus.py:225: RuntimeWarning: Th
e main thread is exiting, but the Bus is in the states.STOPPED state; shutting i
t down automatically now. You must either call bus.block() after start(), or cal
l bus.exit() before the main thread exits.
  "main thread exits." % self.state, RuntimeWarning)
[12/Jun/2013:18:53:32] ENGINE Bus STOPPING

Eugene Van den Bulke

unread,
Jun 12, 2013, 7:15:38 PM6/12/13
to cherrypy-users
On Thu, Jun 13, 2013 at 8:55 AM, Ben Jeffery <b...@bj0.org> wrote:

It does complain, is this anything to worry about?

C:\Python27\lib\site-packages\cherrypy\process\wspbus.py:225: RuntimeWarning: Th
e main thread is exiting, but the Bus is in the states.STOPPED state; shutting i
t down automatically now. You must either call bus.block() after start(), or cal
l bus.exit() before the main thread exits.
  "main thread exits." % self.state, RuntimeWarning)
[12/Jun/2013:18:53:32] ENGINE Bus STOPPING

My bad, try cherrypy.engine.exit() instead ;)

Ben Jeffery

unread,
Jun 12, 2013, 7:33:37 PM6/12/13
to cherryp...@googlegroups.com
Works perfectly, Thanks EuGeNe!

Eugene Van den Bulke

unread,
Jun 12, 2013, 7:36:16 PM6/12/13
to cherrypy-users
Thank Robert, Sylvain etc. for making CherryPy so awesome ;) Enjoy!


--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-user...@googlegroups.com.
To post to this group, send email to cherryp...@googlegroups.com.
Visit this group at http://groups.google.com/group/cherrypy-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages