Question

I have a python web-app that uses Pyramid/CherryPy for the webserver.

It has a few periodic housekeeping tasks that need to be run - Clearing out stale sessions, freeing their resources, etc...

What is the proper way to manage this? I can fairly easily just run a additional "housekeeping" thread (and use a separate scheduler, like APscheduler), but having a separate thread reach into the running server thread(s) just seems like a really clumsy solution. CherryPy is already running the server in a (multi-threaded) eventloop, it seems like it should be possible to somehow schedule periodic events through that.

Was it helpful?

OTHER TIPS

I was lead to this answer by @fumanchu's answer, but I wound up using an instance of the cherrypy.process.plugins.BackgroundTask plugin:

def doHousekeeping():
    print("Housekeeper!")

-

def runServer():


    cherrypy.tree.graft(wsgi_server.app, "/")

    # Unsubscribe the default server
    cherrypy.server.unsubscribe()

    # Instantiate a new server object
    server = cherrypy._cpserver.Server()

    # Configure the server object
    server.socket_host = "0.0.0.0"
    server.socket_port = 8080
    server.thread_pool = 30

    # Subscribe this server
    server.subscribe()

    cherrypy.engine.housekeeper = cherrypy.process.plugins.BackgroundTask(2, doHousekeeping)
    cherrypy.engine.housekeeper.start()

    # Start the server engine (Option 1 *and* 2)
    cherrypy.engine.start()
    cherrypy.engine.block()

Results in doHousekeeping() being called at 2 second intervals within the CherryPy event loop.

It also doesn't involve doing something as silly as dragging in the entire OS just to call a task periodically.

Do yourself a favour and just use cron. No need to roll your own scheduling software.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top