I am making a program that have a interface using tkinter in the main thread. Once the user click the button run a cherrypy server starts in another thread.
...
def startServer():
class HelloWorld(object):
@cherrypy.expose
def printText(self):
print("Printing Some Text")
return {"sucess": "true"}
cherrypy.config.update({
'server.socket_host': '127.0.0.1',
'server.socket_port': 8080,
})
cherrypy.quickstart(HelloWorld(), '/', conf)
def serverExec():
t = threading.Thread(target=startServer, args=())
t.start()
root = Tk()
button = Button(top, text="Run", command=serverExec)
button.grid(row=0, column=0, columnspan=2, sticky=W + E)
root.mainloop()
I want to know how can I stop the the Cherrypy thread when I click in a button "Stop". I can't figure out how to communicate with Cherrypy thread, since cherrypy block it in a loop.
Don't call quickstart. Read its (short!) code and use the parts you need instead. In this case, just leave off the engine.block()
call and it won't block (and you won't need to start it in a separate thread).
Then call engine.stop()
(not exit
--that's for shutting down the process) from your Stop
button. Call engine.start()
again from another button if you like.