如何退出在自己的线程上启动的wsgiserver?

时间:2016-02-01 05:05:48

标签: python windows multithreading process wsgi

我有一个项目,我希望能够在那里工作:

  • 在自己的线程上启动wsgiserver
  • 做事(其中一些涉及与wsgiserver交互
  • 关闭主题
  • 结束程序

我可以做前两个步骤,但我在最后两个步骤中遇到了麻烦。我提供了一个更简单的项目版本,展示了我可以从上面开始前两个步骤的问题,而不是最后两个步骤。

几个问题:

  • 如何让线程停止wsgi服务器?
  • 我是否只需要提取wsgiserver代码并在自己的进程中启动它?

我的项目的一些细节可能会引发一些问题:

  • 我的项目目前正在转发其他与wsgi服务器通信的进程。我可以解决所有问题并让我的进程与我的服务器通信,但是我无法正常关闭。此代码示例旨在提供一个相对简单的'可以更容易审核的样本。
  • 在代码中有一些尝试失败的尝试失败,希望它们不会让人分心。

    #Simple echo program
    #listens on port 3000 and returns anything posted by http to that port
    
    #installing required libraries
    #download/install Microsoft Visual C++ 9.0 for Python
    #https://www.microsoft.com/en-us/download/details.aspx?id=44266
    #pip install greenlet
    #pip install gevent
    
    import sys
    import threading
    import urllib
    import urllib2
    import time
    import traceback
    from gevent.pywsgi import WSGIServer, WSGIHandler
    from gevent import socket
    
    server = ""
    
    def request_error(start_response):
        global server
        # Send error to atm - must provide start_response
        start_response('500', [])
        #server.stop()
        return ['']
    
    def handle_transaction(env, start_response):
        global server
        try:
            result = env['wsgi.input'].read()
            print("Received: " + result)
            sys.stdout.flush()
            start_response('200 OK', [])
            if (result.lower()=="exit"):
                #server.stop()
                return result
            else:
                return result
        except:
            return request_error(start_response)
    
    class ErrorCapturingWSGIHandler(WSGIHandler):
        def read_requestline(self):
            result = None
            try:
                result = WSGIHandler.read_requestline(self)
            except:
                protocol_error()
                raise # re-raise error, to not change WSGIHandler functionality
            return result
    
    class ErrorCapturingWSGIServer(WSGIServer):
        handler_class = ErrorCapturingWSGIHandler
    
    def start_server():
        global server
        server = ErrorCapturingWSGIServer(
        ('', 3000), handle_transaction, log=None)
    
        server.serve_forever()
    
    def main():
        global server
    
        #start server on it's own thread
        print("Echoing...")
        commandServerThread = threading.Thread(target=start_server)
        commandServerThread.start()
    
        #now that the server is started, send data
        req = urllib2.Request("http://127.0.0.1:3000", data='ping')
        response = urllib2.urlopen(req)
        reply = response.read()
        print(reply)
    
        #take a look at the threading info
        print(threading.active_count())
    
        #try to exit
        req = urllib2.Request("http://127.0.0.1:3000", data='exit')
        response = urllib2.urlopen(req)
        reply = response.read()
        print(reply)
    
        #Now that I'm done, exit
        #sys.exit(0)
        return    
    
    if __name__ == '__main__':
        main()
    

0 个答案:

没有答案