cherrypy.process.plugins – Engine Plugins

Site services for use with a Web Site Process Bus.

Classes

class cherrypy.process.plugins.SimplePlugin(bus)

Plugin base class which auto-subscribes methods for known channels.

bus = None

A Bus, usually cherrypy.engine.

subscribe()

Register this object as a (multi-channel) listener on the bus.

unsubscribe()

Unregister this object as a listener on the bus.

class cherrypy.process.plugins.ThreadManager(bus)

Bases: cherrypy.process.plugins.SimplePlugin

Manager for HTTP request threads.

If you have control over thread creation and destruction, publish to the ‘acquire_thread’ and ‘release_thread’ channels (for each thread). This will register/unregister the current thread and publish to ‘start_thread’ and ‘stop_thread’ listeners in the bus as needed.

If threads are created and destroyed by code you do not control (e.g., Apache), then, at the beginning of every HTTP request, publish to ‘acquire_thread’ only. You should not publish to ‘release_thread’ in this case, since you do not know whether the thread will be re-used or not. The bus will call ‘stop_thread’ listeners for you when it stops.

acquire_thread()

Run ‘start_thread’ listeners for the current thread.

If the current thread has already been seen, any ‘start_thread’ listeners will not be run again.

graceful()

Release all threads and run all ‘stop_thread’ listeners.

release_thread()

Release the current thread and run ‘stop_thread’ listeners.

stop()

Release all threads and run all ‘stop_thread’ listeners.

threads = None

A map of {thread ident: index number} pairs.

Monitors

class cherrypy.process.plugins.BackgroundTask(interval, function, args=, []kwargs={}, bus=None)

A subclass of threading.Thread whose run() method repeats.

Use this class for most repeating tasks. It uses time.sleep() to wait for each interval, which isn’t very responsive; that is, even if you call self.cancel(), you’ll have to wait until the sleep() call finishes before the thread stops. To compensate, it defaults to being daemonic, which means it won’t delay stopping the whole process.

class cherrypy.process.plugins.PerpetualTimer(*args, **kwargs)

A responsive subclass of threading.Timer whose run() method repeats.

Use this timer only when you really need a very interruptible timer; this checks its ‘finished’ condition up to 20 times a second, which can results in pretty high CPU usage

class cherrypy.process.plugins.Monitor(bus, callback, frequency=60, name=None)

Bases: cherrypy.process.plugins.SimplePlugin

WSPBus listener to periodically run a callback in its own thread.

callback = None

The function to call at intervals.

frequency = 60

The time in seconds between callback runs.

graceful()

Stop the callback’s background task thread and restart it.

start()

Start our callback in its own background thread.

stop()

Stop our callback’s background task thread.

thread = None

A BackgroundTask thread.

class cherrypy.process.plugins.Autoreloader(bus, frequency=1, match='.*')

Bases: cherrypy.process.plugins.Monitor

Monitor which re-executes the process when files change.

This plugin restarts the process (via os.execv()) if any of the files it monitors change (or is deleted). By default, the autoreloader monitors all imported modules; you can add to the set by adding to autoreload.files:

cherrypy.engine.autoreload.files.add(myFile)

If there are imported files you do not wish to monitor, you can adjust the match attribute, a regular expression. For example, to stop monitoring cherrypy itself:

cherrypy.engine.autoreload.match = r'^(?!cherrypy).+'

Like all Monitor plugins, the autoreload plugin takes a frequency argument. The default is 1 second; that is, the autoreloader will examine files once each second.

files = None

The set of files to poll for modifications.

frequency = 1

The interval in seconds at which to poll for modified files.

graceful()

Stop the callback’s background task thread and restart it.

match = '.*'

A regular expression by which to match filenames.

run()

Reload the process if registered files have been modified.

start()

Start our own background task thread for self.run.

stop()

Stop our callback’s background task thread.

subscribe()

Register this object as a (multi-channel) listener on the bus.

sysfiles()

Return a Set of sys.modules filenames to monitor.

unsubscribe()

Unregister this object as a listener on the bus.