为aiohttp低级服务器选择部署选项

时间:2015-07-23 11:30:00

标签: python python-asyncio aiohttp

我绝对迷失了我的aiohttp服务器的部署选项; 有多种方法可以在生产中移动服务,我无法选择正确的方法:

  1. systemd script / supervisord + api hour 缺点:我不明白api小时是如何工作的,似乎啊控制服务器的守护程序协程,但我的服务器的coro运行(低级别leio aiohttp服务器/连接处理程序)asyncio.gather功能与其他coro(主管/州守护者的类永远运行方法)并且用api小时处理第二个协程可能会有问题
  2. gunicorn虽然喜欢在1
  3. nginx + unix socket + systemd脚本 缺点:看起来很难部署和支持;
  4. systemd脚本下的纯aiohttp服务器: 缺点:处理pid文件,性能低下(?) 我很高兴看到有关我的问题的任何想法,thanx!
  5. 我的运行功能如下:

    def run():
        startTime = time.time()
        logging.basicConfig(level=logging.INFO, format="%(asctime)s [ %(levelname)s ]: %(message)s")
        cfg = ConfigStore("server_config.cfg")
        loop = asyncio.get_event_loop()
        logging.info("Starting server")
        db = DBProxyLayer.fabric(cfg("dbCredentials"))
        logging.info("DBLayer initialized {}".format(db))
        try:
            supervisor = QPSServerSupervisor(loop=loop, db=db, config=cfg)
            srvCoro = loop.create_server(lambda: QPSServer(
                        supervisor=supervisor, debug=False, keep_alive_on=False, loop=loop),
                        host=cfg("command_host"), port=cfg("command_port"))
            loop.run_until_complete(asyncio.gather(*(srvCoro, supervisor.startToServe())))
        except Exception as e:
            logging.critical("Server start failure {}\n{}".format(e, traceback.print_exc()))
            sys.exit(-1)
        else:
            logging.info("Server started for {0:.2f} seconds".format(time.time()-startTime))
        try:
            loop.run_forever()
        except Exception as e:
            logging.info(e)
            loop.close()
            sys.exit(-1)
    

1 个答案:

答案 0 :(得分:0)

loop.create_server可以在预先存在的套接字上激活sock=关键字。我的方法是

try:
    import systemd.daemon
except ImportError:
    def listeners(hosts, port):
        for host in hosts:
            _log.info("Starting server at %s on port %d", host, port)
            yield dict(host=host, port=port)
else:
    def listeners(hosts, port):
        daemon_fds = list(
            fd
            for fd in systemd.daemon.listen_fds()
            if systemd.daemon.is_socket_inet(
                fd,
                type=socket.SOCK_STREAM,
                listening=1,
            )
        )

        if len(daemon_fds) == 0:
            for host in hosts:
                _log.info("Starting server at %s on port %d", host, port)
                yield dict(host=host, port=port)
        else:
            _log.info("Starting server from systemd-provided file descriptors %r", daemon_fds)
            yield from (
                dict(sock=fd)
                for fd in daemon_fds
            )

然后在run中,我循环遍历yield ed dicts:

coros = []
for listener_args in listeners(hosts, port):
    coro = loop.create_server(factory, ssl=ssl_context, **listener_args)
    coros.append(coro)
coros.append(supervisor.startToServe())
results = loop.run_until_complete(asyncio.gather(*coros))