Gevent使用多进程例子

class GEventServer():
    """ gevent wsgi服务器定义,可利用多进程
    """
    def __init__(self,handler,host,port):
        self.handler = handler
        self.host = host
        self.port = port


    def start(self):
        from multiprocessing import Process
        from gevent import monkey
        monkey.patch_socket()
        monkey.patch_os()
        from gevent.wsgi import WSGIServer
        server = WSGIServer((self.host, self.port), self.handler)
        server.pre_start()
        def serve_forever():
            print 'starting server'
            try:
                server.start_accepting()

                try:
                    server._stopped_event.wait()
                except:
                    raise
            except KeyboardInterrupt:
                pass                
        for i in range(2):
            Process(target=serve_forever, args=tuple()).start()
        serve_forever()

你可能感兴趣的:(代码推荐)