我正在尝试部署aiohttp网络应用程序,但无法弄清楚如何让应用程序通过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) 使用socket创建一个套接字作为参数放在loop.create_server()函数中,但我无法得到任何工作。 (我也不知道他的代码中的应用程序是否是同一个web.Application对象)
有人知道如何让这项工作成功吗?谢谢!
答案 0 :(得分:1)
问题是GunicornWebWorker
不支持unix域套接字。它来自GunicornWebWorker.make_handler(self, app, host, port)
,它需要参数:host
和port
。显然,如果你使用的是unix socket,你就没有它们,而是拥有socket的路径。
让我们看一下GunicornWebWorker._run()
的开头:
def _run(self):
for sock in self.sockets:
handler = self.make_handler(self.wsgi, *sock.cfg_addr)
...
如果-b localhost:8000
sock.cfg_addr
为['localhost', 8000]
,则-b unix:my_sock.sock
只有'my_sock.sock'
。这是错误TypeError: make_handler() takes 4 positional arguments but 11 were given
的来源。它是unpacks字符串,而不是列表。
解决此问题的快捷方法是将GunicornWebWorker
子类化,并重新定义GunicornWebWorker.make_handler()
以忽略host
和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
存储库中打开了issue。