Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在键盘中断时终止子线程_Python_Multithreading_Cherrypy - Fatal编程技术网

Python 如何在键盘中断时终止子线程

Python 如何在键盘中断时终止子线程,python,multithreading,cherrypy,Python,Multithreading,Cherrypy,cherrypy服务器的启动方式如下: root = Root() cherrypy.engine.loggingPlugin = LoggerPlugin(cherrypy.engine) cherrypy.engine.loggingPlugin.init(root.grapher.writequeue) cherrypy.engine.loggingPlugin.subscribe() cherrypy.quickstart(Root(), "/", conf) LoggerPlugin

cherrypy服务器的启动方式如下:

root = Root()
cherrypy.engine.loggingPlugin = LoggerPlugin(cherrypy.engine)
cherrypy.engine.loggingPlugin.init(root.grapher.writequeue)
cherrypy.engine.loggingPlugin.subscribe()
cherrypy.quickstart(Root(), "/", conf)
LoggerPlugin的定义如下:

class LoggerPlugin(plugins.SimplePlugin):
"""Intended to catch the stop signal and terminate WriteLog"""
def init(self, queue):
    self.logger = WriteLog("viewerlog.log", queue, 2)

def start(self):
    self.logger.start()

def stop(self):
    print "Exit"
    self.logger.stop()
    print "Exited"
最后,WriteLog是:

class WriteLog (threading.Thread):
def __init__(self, filename, queue, freq):
    self.out = open(filename, "a+")
    self.queue = queue
    self.freq = freq # The time to sleep after every empty queue, in seconds
    self.exit = False
    super(WriteLog, self).__init__()

def stop(self):
    self.exit = True

def run(self):
    while True:
        if self.exit:
            sys.exit(0)

        """ do stuff """
当我按下Ctrl+C时,控制台看起来像:

 ENGINE Bus STOPPING
 ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer shut down
 ENGINE Stopped thread '_TimeoutMonitor'.
 ENGINE Bus STOPPED
 ENGINE Bus EXITING
 ENGINE Bus EXITED
 ENGINE Waiting for child threads to terminate...
 ENGINE Waiting for thread Thread-1.
之后什么也没发生


因为WriteLog是唯一产生的线程,所以它应该是罪魁祸首。即使当WriteLog.exit设置为True时应该调用sys.exit(),也不会发生这种情况

嗯,你用的是什么版本的python和cherrypy?我运行了这段代码,线程正确终止

import cherrypy 
import sys
import threading            

class Root(object):
    def asdf(self):
        return 'hi'

    def grapher(self):
        return 'hi'

class LoggerPlugin(cherrypy.process.plugins.SimplePlugin):
    """Intended to catch the stop signal and terminate WriteLog"""
    def init(self, queue):
        self.logger = WriteLog("viewerlog.log", queue, 2)

    def start(self):
        self.logger.start()

    def stop(self):
        print("Exit")
        self.logger.stop()
        print("Exited")

class WriteLog (threading.Thread):
    def __init__(self, filename, queue, freq):
        self.out = open(filename, "a+")
        self.queue = queue
        self.freq = freq # The time to sleep after every empty queue, in seconds
        self.exit = False
        super(WriteLog, self).__init__()

    def stop(self):
        print("WriteLog Exited")
        self.exit = True

    def run(self):
        while True:
            if self.exit:
                sys.exit(0)

            """ do stuff """

root = Root()
cherrypy.engine.loggingPlugin = LoggerPlugin(cherrypy.engine)
cherrypy.engine.loggingPlugin.init(root.grapher)
cherrypy.engine.loggingPlugin.subscribe()
cherrypy.quickstart(Root(), "/")
这是我的输出

[user@dev Scratch_Pad]$ /opt/python3/bin/python3.3 webapp.py
[10/Apr/2014:08:28:26] ENGINE Listening for SIGTERM.
[10/Apr/2014:08:28:26] ENGINE Listening for SIGHUP.
[10/Apr/2014:08:28:26] ENGINE Listening for SIGUSR1.
[10/Apr/2014:08:28:26] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.

[10/Apr/2014:08:28:26] ENGINE Started monitor thread 'Autoreloader'.
[10/Apr/2014:08:28:26] ENGINE Started monitor thread '_TimeoutMonitor'.
[10/Apr/2014:08:28:27] ENGINE Serving on 127.0.0.1:8080
[10/Apr/2014:08:28:27] ENGINE Bus STARTED
^C[10/Apr/2014:08:28:29] ENGINE Keyboard Interrupt: shutting down bus
[10/Apr/2014:08:28:29] ENGINE Bus STOPPING
[10/Apr/2014:08:28:29] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('127.0.0.1', 8080)) shut down
[10/Apr/2014:08:28:29] ENGINE Stopped thread 'Autoreloader'.
Exit
WriteLog Exited
Exited
[10/Apr/2014:08:28:29] ENGINE Stopped thread '_TimeoutMonitor'.
[10/Apr/2014:08:28:29] ENGINE Bus STOPPED
[10/Apr/2014:08:28:29] ENGINE Bus EXITING
[10/Apr/2014:08:28:29] ENGINE Bus EXITED
[10/Apr/2014:08:28:29] ENGINE Waiting for child threads to terminate...
[10/Apr/2014:08:28:29] ENGINE Waiting for thread Thread-1.
这是一个消失的过程

500       5526  5452 99 08:23 pts/1    00:00:42 /opt/python3/bin/python3.3 webapp.py
我相信这条线并不能确定它会消失。您是否仍看到进程正在运行


希望这有帮助

你能展示grapher的代码吗?我正在Ubuntu的包服务器上使用CherryPy和Python 2.7。看起来线程仍然没有关闭——最后显示“等待线程线程1”。问题是它阻止引擎重新启动或正常结束。即使我做了一个py更改并保存它,我的服务器也会停止并正确重新启动。。。“[10/Apr/2014:10:39:30]引擎正在等待子线程终止…[10/Apr/2014:10:39:30]引擎重新生成/home/programmer/Aptana Studio 3 Workspace/Scratch_Pad/webapp.py”。所以对我来说,威胁肯定消失了。有可能是Python 2.7的bug吗除了print()函数的不同,我的代码与您的代码相同,但我的程序在线程1等待后挂起。我用python 2.7和cherrypy 3.2.4在winxp上测试了它,它仍然正常工作。在另一个系统上尝试该代码。在CentOS 6.5上使用Python2.7.5和cherrypy 3.2.4再次测试,该代码正确停止并重新启动。