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 python3
import bottle
import time
import threading
import cherrypy
from cherrypy.process.plugins import Monitor
srv = bottle.Bottle()
lock = threading.Lock()
x = 0
y = 0
def 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." % x
srv.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