我正在制作一个程序,它在主线程中使用tkinter接口。一旦用户单击该按钮,运行cherrypy服务器将在另一个线程中启动。
...
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()当我点击“停止”按钮时,我想知道如何停止Cherrypy线程。我不知道如何与Cherrypy线程通信,因为cherrypy在循环中阻塞了它。
发布于 2020-06-03 15:02:56
别叫快速开始。阅读它的(短!) 代码,并使用您需要的部分。在这种情况下,只需关闭engine.block()调用,它就不会阻塞(您也不需要在单独的线程中启动它)。
然后从您的engine.stop()按钮调用exit(而不是exit--这是用来关闭进程的)。如果您愿意,可以从另一个按钮再次调用engine.start()。
https://stackoverflow.com/questions/62168105
复制相似问题