CherryPy的干扰扭曲关闭在Windows(CherryPy interferes with T

2019-09-17 12:51发布

我有一个运行通过启动与反应堆扭曲的应用程序reactor.run()我在主线程启动一些其他线程,包括CherryPy的Web服务器。 下面是当按下Linux,但无法在Windows按Ctrl + C干净关闭的程序:

from threading import Thread
from signal import signal, SIGINT

import cherrypy

from twisted.internet import reactor
from twisted.web.client import getPage

def stop(signum, frame):
    cherrypy.engine.exit()
    reactor.callFromThread(reactor.stop)
signal(SIGINT, stop)

class Root:
    @cherrypy.expose
    def index(self):
        reactor.callFromThread(kickoff)
        return "Hello World!"

cherrypy.server.socket_host = "0.0.0.0"
Thread(target=cherrypy.quickstart, args=[Root()]).start()

def print_page(html):
    print(html)

def kickoff():
    getPage("http://acpstats/account/login").addCallback(print_page)

reactor.run()

我相信的CherryPy是罪魁祸首在这里,因为这里有我没有的CherryPy写了一个不同的程序时,按下Ctrl + C键是干净的确在Linux和Windows关机:

from time import sleep
from threading import Thread
from signal import signal, SIGINT

from twisted.internet import reactor
from twisted.web.client import getPage

keep_going = True
def stop(signum, frame):
    global keep_going
    keep_going = False
    reactor.callFromThread(reactor.stop)
signal(SIGINT, stop)

def print_page(html):
    print(html)

def kickoff():
    getPage("http://acpstats/account/login").addCallback(print_page)

def periodic_downloader():
    while keep_going:
        reactor.callFromThread(kickoff)
        sleep(5)

Thread(target=periodic_downloader).start()
reactor.run()

没有人有任何的想法是什么问题? 这里是我的难题:

  • 在Linux一切正常
  • 在Windows中,我可以通过调用从信号处理功能reactor.callFromThread时的CherryPy未运行
  • 当CherryPy的运行过程中,没有功能了,我把使用reactor.callFromThread从信号处理都不会执行(我已经验证了信号处理程序本身也被调用)

我能做些什么呢? 如何从一个信号处理器,同时运行的CherryPy关闭扭在Windows? 这是一个错误,或者有我只是错过了文档了这两种项目的一些重要组成部分?

Answer 1:

CherryPy的,当你调用快速启动默认处理信号。 在你的情况,你应该刚刚展开快速入门,这是只有几行,并挑选。 这里的基本上是快速入门确实在后备箱:

if config:
    cherrypy.config.update(config)

tree.mount(root, script_name, config)

if hasattr(engine, "signal_handler"):
    engine.signal_handler.subscribe()
if hasattr(engine, "console_control_handler"):
    engine.console_control_handler.subscribe()

engine.start()
engine.block()

在你的情况,你不需要的信号处理程序,所以你可以省略这些。 你也不需要调用engine.block,如果你不从主线程开始CherryPy的。 Engine.block()仅仅是一种方法,使主线程不会立即终止,而是在附近等进程终止(这是使自动重载工作可靠;某些平台上有调用从任何线程,但主线程execv的问题)。

如果删除块()调用,你甚至不需要周围快速入门线程()。 因此,更换您的线路:

Thread(target=cherrypy.quickstart, args=[Root()]).start()

有:

cherrypy.tree.mount(Root())
cherrypy.engine.start()


文章来源: CherryPy interferes with Twisted shutting down on Windows