Table of contents
1.
Introduction 
2.
Dispatchers
3.
Plugins 
4.
Tools
5.
Frequently Asked Questions
5.1.
What is the function of the CherrPy engine?
5.2.
What are the three main components of CherryPy?
5.3.
What are the key advantages of using CherryPy?
6.
Conclusion
Last Updated: Mar 27, 2024

Organising Code in CherryPy

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

CherryPy is a python web framework based on object-oriented concepts. It helps build web applications just like any other object-oriented program. It is simple, fast and very stable. Organising code is essential while developing applications in any language. There are many ways to organise code, but we shall focus on dispatchers, plugins and tools.

Dispatchers

CherryPy dispatcher objects collect config for a request based on the request attributes and the application architecture to determine which piece of code would handle the incoming request. Dispatchers are called during the request processing phase. They can be implemented by importing the cherrypy._cpdispatch module.

CherryPy Dispatcher walks a tree of objects rooted at cherrypy.request.app.root. When a dispatcher is called, the core passes a path_info argument whose each hierarchical component matches a corresponding attribute of the root object. The built-in default dispatcher for CherryPy is cherrypy._cpdispatch.Dispatcher(dispatch_method_name=None, translate=...)

The dispatch_method_name can be set to “cp_dispatch” to provide a dynamic dispatch algorithm optionally. 

File_handler is a function that returns a page handler and an object containing a list of virtual-path components. These components are passed as positional arguments to the handler.

Plugins 

An application requires functions outside the request cycle to run background tasks and manage various connections. Plugins closely work with the CherryPy engine and extend it to perform various other operations. Plugins can be implemented using the cherrypy.process.plugins module. Some of the commonly used plugins are:

  • cherrypy.process.plugins.monitor: It is a Web Site Process bus listener to periodically run a callback in its thread.
     
  • cherrypy.process.plugins.Autoreloader: It restarts the process if any changes in files are detected. By default, it monitors all imported modules.
     
  • cherrypy.process.plugin.BackgroundTask: It is a subclass of threading.Thread that runs repeating tasks.
     
  • cherrypy.process.plugins.ThreadManager: It manages all HTTP request threads.

Tools

It is a CherryPy mechanism that helps execute a variety of requests. It is typically a piece of code that runs to perform additional work as per the requests received. They can be used as a callable in any function whenever needed. Tools can also be used as decorators to configure page handlers in CherrPy. Built-in tools are either modules or instances of the tools.Tool class. Basic Authentication and caching tools are the most commonly used tools in CherryPy.

Frequently Asked Questions

What is the function of the CherrPy engine?

The CherryPy engine handles the creation and management of requests, responses and events. It also controls and manages all CherryPy objects.

What are the three main components of CherryPy?

The CherryPy engine handles events and controls process setup. The CherryPy server configures the HTTP server. CherryPy tools is a toolbox that helps execute HTTP requests.

What are the key advantages of using CherryPy?

CherryPy is simple and easily adjustable. Object-oriented concepts like inheritance, data abstraction and encapsulation can be implemented quickly. CherryPy has a web server of its own and is self-contained. This helps with fast deployments of applications.

Conclusion

This blog discusses organising code in CherryPy. It explains the three main mechanisms: dispatchers, plugins and tools. Check out our articles on JavaScript, CSS and images in CherryPyPyTest and code coverage in CherryPy and REST with CherryPy. Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and Algorithms, Machine Learning, Deep Learning, Cloud Computing and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio! 

Looking for questions from tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations. Upvote our blogs if you find them insightful and engaging! Happy Coding!

Thank you image

Live masterclass