title: Stop() gevent webserver is never called
body: <p>I am not able to stop my Gevent webserver because my <code>stop()</code> function is never called.</p>
  <p>My Gevent webserver (<code>serve_forever()</code>) is running in a single QThread thread.
  My main application is (QCoreApplication) written with PySide2.</p>
  <p><em><strong>What i tried</strong></em></p>
  <ul>
  <li>Emitting a stop signal from the main application to the thread -&gt; The slot in the thread, which calls <code>stop()</code>is never activated -&gt; I presume, that server_forever() is blocking it?!</li>
  <li>I googled, that <code>stop()</code> shall not be called from within the same thread -&gt; Unfortnuatelly, I wouldn't know how to safely call a method from another thread. I thought that's exactly what signal/slots are for</li>
  <li>I tried to use gevent.signal.signal() instead of a @QtCore.Slot(). -&gt; This function is not accepting a custom signal, or i did it wrong</li>
  </ul>
  <p>Update with working example:</p>
  <p><em><strong>main.py</strong></em></p>
  <pre><code>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()
  </code></pre>
  <p><em><strong>T_CTRL.py</strong></em></p>
  <pre><code>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=&quot;&quot;))
          signal.signal(signal.SIGTERM, partial(self.sigint_handler, str1=&quot;&quot;))
          
          # 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 (&quot;T_CTRL: Slot 'sigint_handler' activated. Emitting signal 'sigShutdown'&quot;)
         self.sigShutdown.emit()
         QCoreApplication.processEvents()
  
      @QtCore.Slot()
      def quit_now(self): 
         print (&quot;_CTRL: slot 'quit_now' activated, quitting application&quot;)
         QCoreApplication.quit()         
  </code></pre>
  <p><em><strong>T_Web_Worker</strong></em></p>
  <pre><code>from PySide2 import QtCore
  from PySide2.QtCore import QCoreApplication
  from flask import Flask
  from gevent.pywsgi import WSGIServer
  
  class ThreadWorker(QtCore.QObject): 
      
      # init flask &amp; 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 (&quot;T_Web_Worker: Slot 'startServer' activated&quot;)
          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(&quot;T_Web_Worker: Slot 'requestServerStop' activated. Emitting signal 'sigServerShutdownFinished'&quot;)
          self.http_server.stop(timeout=5)
          self.sigServerShutdownFinished.emit()
          QCoreApplication.processEvents()
  </code></pre>