3

我正在尝试部署一个 aiohttp Web 应用程序,但不知道如何让应用程序通过 unix 套接字提供服务,我认为我需要这样才能让 nginx 和 gunicorn 相互通信。

aiohttp 文档中保存为 app.py 的简单示例应用程序:

import asyncio
from aiohttp import web

@asyncio.coroutine
def hello(request):
    return web.Response(body=b'Hello')

app = web.Application()
app.router.add_route('GET', '/', hello)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    handler = app.make_handler()
    f = loop.create_server(handler, '0.0.0.0', 8080)
    srv = loop.run_until_complete(f)
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        loop.run_until_complete(handler.finish_connections(1.0))
        srv.close()
        loop.run_until_complete(srv.wait_closed())
        loop.run_until_complete(app.finish())
    loop.close()

直接用 gunicorn 运行它:

gunicorn -k aiohttp.worker.GunicornWebWorker -b 0.0.0.0:8000 app:app

但是,当我尝试将其绑定到 unix 套接字时,出现以下错误。

gunicorn -k aiohttp.worker.GunicornWebWorker -b unix:my_sock.sock app:app

追溯:

[2015-08-09 12:26:05 -0700] [26898] [INFO] Booting worker with pid: 26898
[2015-08-09 12:26:06 -0700] [26898] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/home/claire/absapp/venv/lib/python3.4/site-  packages/gunicorn/arbiter.py", line 507, in spawn_worker 
    worker.init_process()
  File "/home/claire/absapp/venv/lib/python3.4/site-packages/aiohttp/worker.py", line 28, in init_process
    super().init_process()
  File "/home/claire/absapp/venv/lib/python3.4/site-packages/gunicorn/workers/base.py", line 124, in init_process
    self.run()
  File "/home/claire/absapp/venv/lib/python3.4/site-packages/aiohttp/worker.py", line 34, in run
    self.loop.run_until_complete(self._runner)
  File "/usr/lib/python3.4/asyncio/base_events.py", line 268, in run_until_complete
    return future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/asyncio/tasks.py", line 236, in _step
    result = next(coro)
  File "/home/claire/absapp/venv/lib/python3.4/site-packages/aiohttp/worker.py", line 81, in _run
    handler = self.make_handler(self.wsgi, *sock.cfg_addr)
TypeError: make_handler() takes 4 positional arguments but 11 were given
[2015-08-09 12:26:06 -0700] [26898] [INFO] Worker exiting (pid: 26898)

我在 aiohttp 问题 ( https://github.com/KeepSafe/aiohttp/issues/136 ) 中遇到了一些问题,它使用套接字创建一个套接字作为参数放入 loop.create_server() 函数中,但我只是不能没有任何工作。(我也不知道他代码里的app是不是同一个web.Application对象)

有谁知道我怎样才能做到这一点?谢谢!

4

1 回答 1

1

问题是它GunicornWebWorker不支持 unix 域套接字。它来自GunicornWebWorker.make_handler(self, app, host, port),它需要参数:hostport。显然,如果您使用的是 unix 套接字,则没有它们,而是有到套接字的路径。

让我们看一下开头GunicornWebWorker._run()

def _run(self):
    for sock in self.sockets:
        handler = self.make_handler(self.wsgi, *sock.cfg_addr)
    ...

在 is 的情况下-b localhost:8000 sock.cfg_addr['localhost', 8000]但对于-b unix:my_sock.sockit's just 'my_sock.sock'。这就是错误TypeError: make_handler() takes 4 positional arguments but 11 were given的来源。它解包字符串,而不是列表。

修复它的快速方法是子类化GunicornWebWorker并重新定义GunicornWebWorker.make_handler()以忽略hostand port。无论如何,它们都没有被使用。你可以这样做:

class FixedGunicornWebWorker(worker.GunicornWebWorker):
    def make_handler(self, app, *args):
        if hasattr(self.cfg, 'debug'):
            is_debug = self.cfg.debug
        else:
            is_debug = self.log.loglevel == logging.DEBUG

        return app.make_handler(
            logger=self.log,
            debug=is_debug,
            timeout=self.cfg.timeout,
            keep_alive=self.cfg.keepalive,
            access_log=self.log.access_log,
            access_log_format=self.cfg.access_log_format)

注意你需要在你的PYTHONPATH. 否则 Gunicorn 将无法找到它。例如,如果您将固定工作人员放在fixed_worker.py您运行的同一目录内的文件中gunicorn,您可以像这样使用它:

$ PYTHONPATH="`pwd`:$PYTHONPATH" gunicorn -k fixed_worker.FixedGunicornWebWorker -b unix:my_sock.sock app:app

UPD还在存储库中打开了问题aiohttp

于 2015-08-19T14:11:36.337 回答