CherryPy with additional threads for custom jobs CherryPy with additional threads for custom jobs multithreading multithreading

CherryPy with additional threads for custom jobs


Subscribe a Monitor instance:

from cherrypy.process.plugins import Monitordef foo():    my.store.collect_data('things', 'stuff')Monitor(cherrypy.engine, foo, frequency=300).subscribe()

This will run the foo function every 300 seconds in its own thread, and that thread will start when you call engine.start and stop when you call engine.stop (or at process exit).


I would like to complete fumanchu's excellent answer by sharing the script I wrote for testing this before using it. It uses python bottle, proving the advice is also working with it.

#!/usr/bin/env python3import bottleimport timeimport threadingimport cherrypyfrom cherrypy.process.plugins import Monitorsrv = bottle.Bottle()lock = threading.Lock()x = 0y = 0def increment ():    print("incrementing...")    with lock:        global x        time.sleep(1)        x += 1    print("Done.")monitor = Monitor(cherrypy.engine, increment, frequency=1)@srv.get("/")def read ():    with lock:        return "Got: %d %d." % (x, y)@srv.post("/periodic/<x:int>")def periodic (x):    if x: monitor.start()    else: monitor.stop()@srv.put("/<V:int>")def write (V):    print("Serving")    with lock:        global x        global y        x = V        time.sleep(5)        y = V        print("gtfo")    return "OK. Current: %d." % xsrv.run(server='cherrypy')

Usage:

After enabling the server, use curl http://localhost:8080 to read, curl http://localhost:8080/<value> to write some value (takes 5 seconds, while all the reads will be hanging), and finally curl http://localhost:8080/periodic/0 and curl http://localhost:8080/periodic/1 to disable/enable respectively the periodic write. Each write will take 1 second, during which reads and modifications will be hanging.

P.s. Working with python2 and python3