【问题标题】:Tkinter and CherryPy concurrencyTkinter 和 CherryPy 并发
【发布时间】:2020-09-21 20:55:43
【问题描述】:

我正在制作一个在主线程中使用 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 在循环中阻塞了它。

【问题讨论】:

  • 尝试在Stop按钮的回调中调用cherrypy.engine.exit()

标签: python multithreading tkinter cherrypy


【解决方案1】:

不要调用快速入门。阅读它的(简短!)code 并使用您需要的部分。在这种情况下,只需停止 engine.block() 调用,它就不会阻塞(并且您不需要在单独的线程中启动它)。

然后从您的Stop 按钮调用engine.stop()(不是exit——用于关闭进程)。如果您愿意,可以从另一个按钮再次拨打engine.start()

【讨论】:

  • 很抱歉接受晚了,但它确实有帮助。谢谢@fumanchu!
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
相关资源
最近更新 更多