Run background process on successful quick start in cherry
I have a REST WebService built on top of a cherry tree. The service is called online with a call cherrypy.quickstart()
.
I want to start a background process with help subprocess.Popen()
right after the service starts on the network. The call is cherrypy.quickstart()
blocked. How do I add a callback to start a background process?
source to share
If the background task is simple and not CPU bound, I suggest you use cherrypy.process.plugins.BackgroundTask
. This is a thread based solution. Here's an answer with a complete example.
Generally, in CherryPy, we don't pass callbacks for internal components. We use Plugins instead . CherryPy native components like session expiration or request timeout, demonizer and PID-writer and others are plugins. The plugin lifecycle is tied to the message bus . FSM diagram illustrates state changes. In your plugin, you just need to handle some states that make sense for your task.
O
|
V
STOPPING --> STOPPED --> EXITING -> X
A A |
| \___ |
| \ |
| V V
STARTED <-- STARTING
There is a Plugin example in this answer . Also take a look at Sylvain Hellegouarch's CherryPys Bus Process Control.
source to share