Introduction
Plugins in CherryPy's helper class automatically subscribes your start and stop functions to the relevant channels. These methods are called appropriately when the start and stop channels are published.

A plugins in Cherrypy, also known as an add-on or extension, is a piece of computer software that enables the addition of additional features without changing the host program itself. Plugins in Cherrypy, which are extensively used in digital music, video, and Web browsing, allow programmers to update a host software while keeping the user inside the program's context.
Plugins in Cherrypy
Simply explained, Plugins in Cherrypy are objects that interact with the bus by publishing to or subscribing to channels—typically both simultaneously.

Whenever you need certain functionalities, Plugins in Cherrypy come in quite handy.
- Accessible throughout the entire application server
- Associated with the life cycle of the application
- Avoid becoming too tightly bound to the application.
Create a new Plugins in Cherrypy

A typical Plugins in Cherrypy appears as follows:
import cherrypy
from cherrypy.process import wspbus, plugins
class DatabasePlugin(plugins.SimplePlugin):
def __init__(self, bus, db_klass):
plugins.SimplePlugin.__init__(self, bus)
self.db = db_klass()
def start(self):
self.bus.log('Start up DB access')
self.bus.subscribe("db-save", self.save_it)
def stop(self):
self.bus.log('Stop down DB access')
self.bus.unsubscribe("db-save", self.save_it)
def save_it(self, entity):
self.db.save(entity)
CherryPy Process Plug-Ins CherryPy offers the utility class known as SimplePlugin, which will automatically subscribe your start and stop methods to the relevant channels.
These methods are called appropriately when the start and stop channels are published.
So, take note of how our plugin joins the db-save channel to receive messages from the bus.
Deploy a plugin
The plugin must be registered to the bus as follows in order to be enabled:
DatabasePlugin(cherrypy.engine, SQLiteDB).subscribe()
This instance of SQLiteDB is a fictitious class that serves as our database provider.
Turn off a plugin
Also, you can do the following to deregister a plugin:
someplugin.unsubscribe()
This is frequently used when you wish to stop CherryPy from starting the default HTTP server, for instance if you run on top of another HTTP server (WSGI capable):
cherrypy.server.unsubscribe()
Let's use this default application for an example:
import cherrypy
class Root(object):
@cherrypy.expose
def index(self):
return "Hi Plugin"
if __name__ == '__main__':
cherrypy.quickstart(Root())
When using this program, for instance, you might see something like this:
[09/Aug/2022:06:24:13] ENGINE Listening for SIGHUP.
[09/Aug/2022:06:24:13] ENGINE Listening for SIGTERM.
[09/Aug/2022:06:24:13] ENGINE Listening for SIGUSR1.
[09/Aug/2022:06:24:13] ENGINE Bus STARTING
[09/Aug/2022:06:24:13] ENGINE Started monitor thread 'Autoreloader'.
[09/Aug/2022:06:24:13] ENGINE Serving on http://127.0.0.1:8080
[09/Aug/2022:06:24:13] ENGINE Bus STARTED
Let's now terminate the HTTP server:
import cherrypy
class Root(object):
@cherrypy.expose
def index(self):
return "Hi Plugin"
if __name__ == '__main__':
cherrypy.server.
unsubscribe()
cherrypy.quickstart(Root())
What we get is this:
[09/Aug/2022:06:31:59] ENGINE Listening for SIGHUP.
[09/Aug/2022:06:31:59] ENGINE Listening for SIGTERM.
[09/Aug/2022:06:31:59] ENGINE Listening for SIGUSR1.
[09/Aug/2022:06:31:59] ENGINE Bus STARTING
[09/Aug/2022:06:31:59] ENGINE Started monitor thread 'Autoreloader'.
[09/Aug/2022:06:31:59] ENGINE Bus STARTED
The server is not launched, as you can see. The lacking
[09/Aug/2022:06:35:25] ENGINE Serving on http://127.0.0.1:8080







