CherryPy integrates file and dictionary configuration

I am creating a CherryPy application and would like to have most of the settings in the .conf file as follows:

[global] server.socketPort = 8080 server.threadPool = 10 server.environment = "production" 

However, I would also like to set up a few with a dictionary in the code, for example:

 conf = {'/': {'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(current_dir, 'templates')}} cherrypy.quickstart(HelloWorld(), config=conf) 

Is it possible to combine both configurations into one and then pass it to the config quick launch option?

+6
python cherrypy configuration
source share
2 answers

quickstart for fast sites. If you do something as complex as having multiple configurations, time is running out. Take a look at the source code of the quick launch function (it's not scary!): You are going to unpack it into your startup script. So instead of quickstart write:

 cherrypy.config.update(conffile) cherrypy.config.update(confdict) app = cherrypy.tree.mount(HelloWorld(), '/', conffile) app.merge(confdict) if hasattr(cherrypy.engine, "signal_handler"): cherrypy.engine.signal_handler.subscribe() if hasattr(cherrypy.engine, "console_control_handler"): cherrypy.engine.console_control_handler.subscribe() cherrypy.engine.start() cherrypy.engine.block() 

We added two lines to the quick launch code. First, we have an additional call to config.update ; which integrates config dict into global configuration. Secondly, app.merge(confdict) ; that to combine multiple configurations into each application.

It is quite normal to do this in the reverse order if you want the config file to override the dict. It is also good to stick with the dict-based configuration in HelloWorld._cp_config as described in the docs.

+11
source share

These are two different configurations. Cherrypy has two configurations: One is the global configuration, and the other is the application configuration. You can use both options:

 cherrypy.config.update('my_file.ini') cherrypy.quickstart(HelloWorld(), config=conf) 

Please note that your sample configuration file is incorrect - instead of server.socketPort it should be server.socket_port , and instead of server.threadPool it should be server.threadPool . See config docs for more information.

+2
source share

All Articles