Latching signal handling?

258 views
Skip to first unread message

Simon Haines

unread,
May 13, 2012, 9:45:54 PM5/13/12
to cherryp...@googlegroups.com
Some of my modules used as CherryPy applications spin up their own threads (via threading.Timer) for background processing. However, I can't find a way to latch cherrypy.engine.signal_handler or cherrypy.engine.console_control_handler to properly clean things up when Ctrl-C is pressed on a Linux/Windows console.

My server startup code looks like this (Python 2.7.1, CherryPy 3.1.2):

# Register of background jobs
jobs = []
def handle_terminate(event):
  for job in jobs:
    print('Terminating %s' % job.name)
    job.stop()

if hasattr(engine, 'signal_handler'):
  engine.signal_handler.set_handler('SIGTERM', handle_terminate)
  engine.signal_handler.subscribe()
if hasattr(engine, 'console_control_handler'):
  # Best guess as there is no documentation for console_control_handler
  engine.console_control_handler.set_handler('SIGTERM', handle_terminate)
  engine.console_control_handler.subscribe()

Obviously I'd like to register a listener for the Ctrl-C signal, have the listener clean up the background jobs, and then chain the default handler. Without this, the server never terminates waiting for other threads to finish. Is this possible?
Thanks, Simon.


Sylvain Hellegouarch

unread,
May 14, 2012, 9:42:14 AM5/14/12
to cherryp...@googlegroups.com
Hi Simon,

You might simple want to subscribe your function to the CherryPy engine as follow:

cherrypy.engine.subscribe('exit',  handle_terminate)

That function should not take any arguments though.

- Sylvain



--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cherrypy-users/-/KRXNmnzVDrQJ.
To post to this group, send email to cherryp...@googlegroups.com.
To unsubscribe from this group, send email to cherrypy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.



--
- Sylvain
http://www.defuze.org
http://twitter.com/lawouach

Simon Haines

unread,
May 15, 2012, 2:52:50 AM5/15/12
to cherryp...@googlegroups.com
Hi Sylvian, thanks for this idea. Much cleaner than my current solution which is to set the timer threads as daemonized so the process need not wait for them to finish.
Simon.


On Monday, May 14, 2012 11:42:14 PM UTC+10, Sylvain Hellegouarch wrote:
Hi Simon,

You might simple want to subscribe your function to the CherryPy engine as follow:

cherrypy.engine.subscribe('exit',  handle_terminate)

That function should not take any arguments though.

- Sylvain

On Mon, May 14, 2012 at 3:45 AM, Simon Haines wrote:
Some of my modules used as CherryPy applications spin up their own threads (via threading.Timer) for background processing. However, I can't find a way to latch cherrypy.engine.signal_handler or cherrypy.engine.console_control_handler to properly clean things up when Ctrl-C is pressed on a Linux/Windows console.

My server startup code looks like this (Python 2.7.1, CherryPy 3.1.2):

# Register of background jobs
jobs = []
def handle_terminate(event):
  for job in jobs:
    print('Terminating %s' % job.name)
    job.stop()

if hasattr(engine, 'signal_handler'):
  engine.signal_handler.set_handler('SIGTERM', handle_terminate)
  engine.signal_handler.subscribe()
if hasattr(engine, 'console_control_handler'):
  # Best guess as there is no documentation for console_control_handler
  engine.console_control_handler.set_handler('SIGTERM', handle_terminate)
  engine.console_control_handler.subscribe()

Obviously I'd like to register a listener for the Ctrl-C signal, have the listener clean up the background jobs, and then chain the default handler. Without this, the server never terminates waiting for other threads to finish. Is this possible?
Thanks, Simon.


--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cherrypy-users/-/KRXNmnzVDrQJ.
To post to this group, send email to cherrypy-users@googlegroups.com.
To unsubscribe from this group, send email to cherrypy-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.
Reply all
Reply to author
Forward
0 new messages