[英]Deploying aiohttp.web Application using gunicorn and nginx
我正在嘗試部署aiohttp Web應用程序,但無法弄清楚如何讓應用程序通過unix socket提供服務,我認為我需要這樣才能讓nginx和gunicorn相互通信。
保存為app.py的aiohttp文檔中的簡單示例應用程序:
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()函數中,但我只是不能什么都行不通。 (我也不知道他的代碼中的app是否是同一個web.Application對象)
有誰知道我怎么做這個工作? 謝謝!
問題是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
來自。 它解壓縮字符串,而不是列表。
修復它的快速方法是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
文件中的fixed worker放在你運行gunicorn
的同一目錄中,你可以使用它:
$ PYTHONPATH="`pwd`:$PYTHONPATH" gunicorn -k fixed_worker.FixedGunicornWebWorker -b unix:my_sock.sock app:app
UPD也在aiohttp
存儲庫中打開了問題 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.