Python 从未调用停止()gevent Web服务器

Python 从未调用停止()gevent Web服务器,python,pyside2,qthread,gevent,Python,Pyside2,Qthread,Gevent,我无法停止Gevent Web服务器,因为从未调用我的stop()函数 我的Gevent Web服务器(永远服务())在单个QThread线程中运行。 我的主要应用程序是用PySide2编写的(QCoreApplication) 我尝试的 从主应用程序向线程发出停止信号->线程中调用stop()的插槽从未激活->我猜想,服务器永远()正在阻止它 我在谷歌上搜索到,stop()不能从同一个线程中调用->不幸的是,我不知道如何安全地从另一个线程调用方法。我想这正是信号/插槽的用途 我尝试使用gev

我无法停止Gevent Web服务器,因为从未调用我的
stop()
函数

我的Gevent Web服务器(
永远服务()
)在单个QThread线程中运行。 我的主要应用程序是用PySide2编写的(QCoreApplication)

我尝试的

  • 从主应用程序向线程发出停止信号->线程中调用
    stop()
    的插槽从未激活->我猜想,服务器永远()正在阻止它
  • 我在谷歌上搜索到,
    stop()
    不能从同一个线程中调用->不幸的是,我不知道如何安全地从另一个线程调用方法。我想这正是信号/插槽的用途
  • 我尝试使用gevent.signal.signal()而不是@QtCore.Slot()。->此函数不接受自定义信号,或者我做错了
使用工作示例更新:

main.py

T\u CTRL.py

T\u网络工作者


请提供一个我添加的最小工作示例。我希望这有帮助,谢谢。这些代码不是MRE,因为有很多元素没有定义。请告诉我你指的是哪一部分?我阅读了MRE的常见问题。据我所知,它应该是可运行的代码,不会向很多不必要的代码显示。我只能看到一个未在构造函数中定义的变量
http\u服务器
。我认为在这种情况下是有道理的。关键是:“可运行”。如果缺少元素,那么您认为代码是可运行的吗?缺少导入,“self.app”等。
import sys
from PySide2.QtCore import QCoreApplication, QTimer
from T_CTRL import T_CTRL

def main():

    # create application
    app = QCoreApplication.instance()
    if app is None: 
        app = QCoreApplication(sys.argv)
    
    # connect: cleanup application before quitting
    app.aboutToQuit.connect(app.deleteLater)

    # create my control object
    myCtrl = T_CTRL() 
    
    # # create helper timer, so that quit signals can be received
    timerHelper = QTimer()
    timerHelper.start(500)
    timerHelper.timeout.connect(lambda: None)  # Let the interpreter run each 500 ms.

    # run application and create main event handler
    return app.exec_()
    
if __name__ == '__main__':
    main()
from PySide2 import QtCore
from PySide2.QtCore import QThread, QCoreApplication

from T_Web_Worker import ThreadWorker
import signal
from functools import partial

class T_CTRL(QtCore.QObject): 
    
    # signals    
    sigShutdown = QtCore.Signal()   
    
    def __init__(self):  
        # init 
        super(T_CTRL, self).__init__()      

        # create objects
        self.t = QThread()
        self.worker = ThreadWorker()
    
        # connect quit signals to sigint_handler
        signal.signal(signal.SIGINT,  partial(self.sigint_handler, str1=""))
        signal.signal(signal.SIGTERM, partial(self.sigint_handler, str1=""))
        
        # move worker to thread
        self.worker.moveToThread(self.t)        
        
        # connect: startServer() when thread has started
        self.t.started.connect(self.worker.startServer)
                
        # connect: requestServerStop() when Sigint handler has been activated
        self.sigShutdown.connect(self.worker.requestServerStop)
        
        # connect: webserver told that is has shut down, now stop the thread
        self.worker.sigServerShutdownFinished.connect(self.t.quit)
        self.worker.sigServerShutdownFinished.connect(self.worker.deleteLater)

        # connect: thread has finished, now quit the application        
        self.t.finished.connect(self.t.deleteLater)
        self.t.finished.connect(self.quit_now)
       
        # start the thread
        self.t.start()

    def sigint_handler(self, signal, frame, str1):
       print ("T_CTRL: Slot 'sigint_handler' activated. Emitting signal 'sigShutdown'")
       self.sigShutdown.emit()
       QCoreApplication.processEvents()

    @QtCore.Slot()
    def quit_now(self): 
       print ("_CTRL: slot 'quit_now' activated, quitting application")
       QCoreApplication.quit()         
from PySide2 import QtCore
from PySide2.QtCore import QCoreApplication
from flask import Flask
from gevent.pywsgi import WSGIServer

class ThreadWorker(QtCore.QObject): 
    
    # init flask & tornado
    app = Flask(__name__)
    http_server = None
    
    # signal to feedback, that worker has finished
    sigServerShutdownFinished = QtCore.Signal()
    
        
    def __init__(self):
        super(ThreadWorker, self).__init__() 
        
    # start requested by main thread
    @QtCore.Slot()
    def startServer(self):
        print ("T_Web_Worker: Slot 'startServer' activated")
        self.http_server = WSGIServer(('127.0.0.1', 5000), self.app)       
        self.http_server.serve_forever()
        QCoreApplication.processEvents()

    # shutdown requested by main thread
    @QtCore.Slot()
    def requestServerStop(self):   
        print("T_Web_Worker: Slot 'requestServerStop' activated. Emitting signal 'sigServerShutdownFinished'")
        self.http_server.stop(timeout=5)
        self.sigServerShutdownFinished.emit()
        QCoreApplication.processEvents()