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_Python 2.7_Cherrypy - Fatal编程技术网

在Python中优雅地终止线程

在Python中优雅地终止线程,python,multithreading,python-2.7,cherrypy,Python,Multithreading,Python 2.7,Cherrypy,我试图在按下Ctrl+C后立即终止一个线程。我创建了一个类,该类处理我希望线程定期执行的所有操作。问题是当键盘中断被触发(或SIGINT)时,循环成功停止,但整个程序冻结(我使用cherrypy作为web服务器)。我环顾四周,找不到解决办法。有人能启发我吗 代码: main.py from KeywordWorker import main as worker SMBinWorker = worker.KeywordHandler(SMBinDir, sqlitePath) SMBinWork

我试图在按下Ctrl+C后立即终止一个线程。我创建了一个类,该类处理我希望线程定期执行的所有操作。问题是当键盘中断被触发(或SIGINT)时,循环成功停止,但整个程序冻结(我使用cherrypy作为web服务器)。我环顾四周,找不到解决办法。有人能启发我吗

代码:

main.py

from KeywordWorker import main as worker

SMBinWorker = worker.KeywordHandler(SMBinDir, sqlitePath)
SMBinWorker.run()
signal(SIGINT, SMBinWorker.setFlag)
我的目标代码:

from os import listdir
from os.path import isfile
from os.path import join as osjoin
from time import sleep
from signal import signal, SIGINT

from extentions import *

import threading as mp

class KeywordHandler:
def __init__(self, path_to_files, sqlite_path):
    self.shutdown_flag = mp.Event()
    self.pathToFiles = path_to_files
    self.sqlitePath = sqlite_path
    self.queue = list()
    # Files Already Processed
    self.filesProcessed = set()

# start Process
def run(self):
    self.prc = mp.Thread(target=self.worker)
    self.prc.start()

def setFlag(self, foo, bar):
    print "\n  Shutting down Keyword Handler Gracefully.."
    self.shutdown_flag.set()
    self.prc.join()

# determines what to do with file
def extentionResolver(self, fileName):
    # deal with file extention
    pass

# return file count on directory
def getFileCount(self):
    return len([f for f in listdir(self.pathToFiles) if isfile(osjoin(self.pathToFiles, f))])

# return files names
def getFileList(self):
    return [f for f in listdir(self.pathToFiles) if isfile(osjoin(self.pathToFiles, f))]

# reset queue
def reset(self):
    self.queue = list()
    self.filesProcessed = set()

# compares directory to queue
def updateQueue(self):
    # update queue
    pass

# Deal with files in queue    
def resolveQueue(self):
    # resolve queue
    # loop through each file, and at the end of the loop, check shutdown_flag 
    pass


def worker(self):
    while not self.shutdown_flag.is_set():
        if self.getFileCount() > len(self.filesProcessed):
            # Check what's new
            self.updateQueue()
            sleep(0.2)
            # Process queue
            self.resolveQueue()

        sleep(10)

在使用调试器退出循环后,是否检查了进程的状态?是否可以在没有Web服务器上下文的情况下提供?您到底观察到了什么?您可能应该在配置中设置
server.shutdown\u超时:0