Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
CherryPy is aPython-based framework that makes it simple for programmers to design and encapsulate web logic around HTTP protocols thanks to its Object Orientation approach. Why CherryPy's key strength is its simplicity. It supports Object Relational Mapper (ORM) and extensions from templating languages for database and templating purposes.
Server-wide functions
Support for aliases is a little-known but helpful functionality offered by the cherrypy.expose() decorator.
Both an HTTP library and a web application framework, CherryPy, can be used. In the latter scenario, its architecture offers tools to allow server instance-wide activities. As server-wide services exist outside the request processing, this presents an entire canvas for continuous operations. As long as the bus operates, they are accessible for the whole procedure.
Common usage cases
maintaining a pool of connections to external servers so you don't have to open them again for each request (database connections, for instance).
Background operations (say you need work done without blocking the request).
Publish/Subscribe pattern
The core of CherryPy is a bus system that uses a straightforward publish/subscribe messaging structure. Simply put, that bus controls every aspect of CherryPy.
The bus can readily be visualized as the belt of a sushi bar, as shown in the image below.
On a bus, you can subscribe to and publish to channels. A channel functions somewhat like a unique identifier inside the bus. The bus will send a message to every channel subscriber when a message is published to that channel.
A pubsub pattern's promotion of decoupling between a caller and a callee is an intriguing feature. A message that is published will eventually elicit a response, but the publisher is unaware of the source of that answer.
A CherryPy application can readily use features without needing to hold a reference to the item delivering those functionalities because of the decoupling. Instead, all that has to be done is for the application to publish to the bus and wait for the correct answer.
Typical pattern
Take the following fictitious application:
import cherrypy
class ECommerce(object):
def __init__(self, db):
self.mydb = db
@cherrypy.expose
def save_kart(self, cart_data):
cart = Cart(cart_data)
self.mydb.save(cart)
if __name__ == '__main__':
cherrypy.quickstart(ECommerce(), '/')
The program references the database, however, this results in a reasonably tight coupling between the application and the database provider.
Using a pubsub process is another way to get around the coupling:
import cherrypy
class ECommerce(object):
@cherrypy.expose
def save_kart(self, cart_data):
cart = Cart(cart_data)
cherrypy.engine.publish('db-save', cart)
if __name__ == '__main__':
cherrypy.quickstart(ECommerce(), '/')
In this illustration, a cart instance is published to the db-save channel. Then, one or more subscribers can respond to that message without needing the application to be made aware of their actions.
Output
[17/Aug/2022:23:47:07] ENGINE Listening for SIGHUP.
[17/Aug/2022:23:47:09] ENGINE Listening for SIGTERM.
[17/Aug/2022:23:47:09] ENGINE Listening for SIGUSR1.
[17/Aug/2022:23:48:23] ENGINE Creating database
[17/Aug/2022:23:48:23] ENGINE Started monitor thread 'Autoreloader'.
[17/Aug/2022:23:48:23] ENGINE Serving cart
[17/Aug/2022:23:50:01] ENGINE ECommerce STARTED
Implementation details
The bus implementation in CherryPy is straightforward because it only registers functions to channels. Each registered function is used with the message supplied as a parameter whenever a message is published to a channel.
The entire behavior is synchronous, so if one subscriber takes too long to process a message, it will delay the remaining subscribers.
The bus in CherryPy is not a sophisticated pubsub messaging broker system like those offered by zeromq or rabbitmq. Use it with the knowledge that there can be a cost.
Engine as a pubsub bus
As was previously said, CherryPy is designed around a pubsub bus. A single bus instance called the engine serves as the foundation for all the entities that the framework maintains at runtime.
Therefore, the bus implementation offers a set of standard channels that characterize the lifecycle of the application:
The channels are disclosed when the statuses change, allowing subscribers to respond.
One excellent illustration is the HTTP server switches from a "STOPPED" state to a "STARTED" state each time a message is published to the start channel.
Frequently Asked Questions
What accomplishes CherryPy expose?
In other words, it is your responsibility as a developer to offer the tools necessary to implement the logic of your application after CherryPy has been discovered and is called an exposed method. CherryPy believes that you, the developer, are the expert.
CherryPy, a server for the web?
In Brief CherryPy WSGI Web Server is described as a fast, production-ready, thread-pooled, general HTTP server by the [CherryPy] team. This modular component can serve any Python WSGI web application.
CherryPy has several threads?
Application server with several threads. The multithreading idea served as the foundation for CherryPy's design. The multi-threaded environment is used each time a developer obtains or sets a value in the CherryPy namespace.
Describe the CherryPy framework.
The Python programming language is used by CherryPy, an object-oriented web application framework. By wrapping the HTTP protocol, it is intended to speed up the creation of web applications, although it remains low level and does not provide much more than what is specified in RFC 7231.
Describe Falcon API.
Falcon is a lightning-quick, lightweight Python web API framework for creating reliable app backends and microservices. The framework performs admirably with both gevent/Meinhold and asyncio (ASGI) (WSGI).
Conclusion
We have talked about pub/sub, also known as Publish/Subscribe Patterns in cherrypy, their Server-wide functions, typical patterns, Implementation details of pub/sub, etc.
If you face any doubt, please comment, and we will love to answer your questions.
Want expertise inPython for your next web development project? Check out ourcourse.
Nevertheless, you may consider our paidcourses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!