Force CherryPy Child Threads
Asked Answered
A

2

10

Well, I want cherrypy to kill all child threads on auto-reload instead of "Waiting for child threads to terminate" because my program has threads of its own and I don't know how to get past this. CherryPy keeps hanging on that one line and I don't know what to do to get the 'child threads' to terminate...

`

[05/Jan/2010:01:14:24] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('127.0.0.1', 8080)) shut down
[05/Jan/2010:01:14:24] ENGINE Stopped thread '_TimeoutMonitor'.
[05/Jan/2010:01:14:24] ENGINE Bus STOPPED
[05/Jan/2010:01:14:24] ENGINE Bus EXITING
[05/Jan/2010:01:14:24] ENGINE Bus EXITED
[05/Jan/2010:01:14:05] ENGINE Waiting for child threads to terminate...

`

it never continues.. So i want to force the child threads to close ...

I know it is because my application is using threads of its own and I guess cherrypy wants those threads to quit along with CherryPy's.... Can I overcome this ?

Adiathermancy answered 5/1, 2010 at 6:24 Comment(1)
I'm starting to think I should overload CherryPy's auto-reload to kill my own client.thread... But how, i dont kno.Adiathermancy
E
14

You need to write code that stops your threads, and register it as a listener for the 'stop' event:

from cherrypy.process import plugins

class MyFeature(plugins.SimplePlugin):
    """A feature that does something."""

    def start(self):
        self.bus.log("Starting my feature")
        self.threads = mylib.start_new_threads()

    def stop(self):
        self.bus.log("Stopping my feature.")
        for t in self.threads:
            mylib.stop_thread(t)
            t.join()

my_feature = MyFeature(cherrypy.engine)
my_feature.subscribe()

See http://www.cherrypy.org/wiki/BuiltinPlugins and http://www.cherrypy.org/wiki/CustomPlugins for more details.

Entail answered 5/1, 2010 at 16:48 Comment(2)
Okay. I will look into this. I'm using the quickstart method. Can I put these start and stop methods inside my root class that I use with cherrypy.quickstart() ? Or can you tell me how I would use this class MyFeature(), with my already root class I'm using Root().. Sorry, I haven't had extensive use with CherryPy..Adiathermancy
Sure; you can put that code anywhere you like; the only important thing is that you subscribe it before you run quickstart.Entail
I
0

This works with quickstart

def stopit():
    print 'stop handler invoked'
    #...
stopit.priority = 10
cherrypy.engine.subscribe('stop', stopit)

In order to support its life-cycle, CherryPy defines a set of common channels that will be published to at various states:

“start”: When the bus is in the “STARTING” state

“main”: Periodically from the CherryPy’s mainloop

“stop”: When the bus is in the “STOPPING” state

“graceful”: When the bus requests a reload of subscribers

“exit”: When the bus is in the “EXITING” state

This channel will be published to by the engine automatically. Register therefore any subscribers that would need to react to the transition changes of the engine.

..

In order to work with the bus, the implementation provides the following simple API:

cherrypy.engine.publish(channel, *args):

The channel parameter is a string identifying the channel to which the message should be sent to

*args is the message and may contain any valid Python values or objects.

cherrypy.engine.subscribe(channel, callable):

The channel parameter is a string identifying the channel the callable will be registered to.

callable is a Python function or method which signature must match what will be published.

Icterus answered 9/8, 2013 at 8:36 Comment(1)
Why was this down-voted? This is recommended by cherrypi docs too. docs.cherrypy.org/en/latest/extend.html#built-in-channels Couldn't find info on the priority field though.Augie

© 2022 - 2024 — McMap. All rights reserved.