I am building a application that uses CherryPy to serve a REST API, and another thread that does background work (in fact, it reads data from a serial port).
import cherrypy
import threading
class main:
@cherrypy.expose
def index(self):
return "Hello World."
def run():
while running == True:
# read data from serial port and store in a variable
running = True
t = threading.Thread(target = run)
t.start()
if __name__ == '__main__':
cherrypy.quickstart(main())
running = False
Both api.pc_main()
and run
work fine. The trouble is, I use a running
boolean to stop my thread, but that piece of code is never reached, because CherryPy waits for that thread to finish when I push Ctrl-C. I actually have to use kill -9
to stop the process.