知乎

国内知名网站知乎 ...

↑我也要推荐

管理多个 gevent server

发布时间:2011-06-22 06:48:25, 关注:+6779, 赞美:+7, 不爽:+9

原始出处: gist.github

代码如下:

# Class for managing multiple servers or anything with start() and stop() methods

class ServerRack(object):

    def __init__(self, servers):
        self.servers = servers

    def start(self):
        started = []
        try:
            for server in self.servers[:]:
                server.start()
                started.append(server)
                name = getattr(server, 'name', None) or server.__class__.__name__ or 'Server'
                log('%s started on %s', name, server.address)
        except:
            self.stop(started)
            raise

    def stop(self, servers=None):
        if servers is None:
            servers = self.servers[:]
        for server in servers:
            try:
                server.stop()
            except:
                if hasattr(server, 'loop'): gevent >= 1.0
                    server.loop.handle_error(server.stop, *sys.exc_info())
                else: # gevent <= 0.13
                    import traceback
                    traceback.print_exc()


# example: run WSGI app on HTTP and HTTPS
rack = ServerRack([WSGIServer(('', 80), application)),
                   WSGIServer(('', 443), application, keyfile=keyfile, certfile=certfile])
rack.start()

如果你觉得本站对你有帮助,欢迎向本站赞助 :P

使用支付宝捐赠

Copyright© Python4cn(news, jobs) simple-is-better.com, 技术驱动:powered by web.py 空间主机:Webfaction

版权申明:文章转载已注明出处,如有疑问请来信咨询。本站为 python 语言推广公益网站,与 python 官方没有任何关系。

联系/投搞/留言: en.simple.is.better@gmail.com 向本站捐赠