aiohttp unix socket 启动简单说明
aiohttp 官方推荐了不少部署模式,以下是关于unix socket 部署的简单说明
参考配置
- web.py
from aiohttp import web
import argparse
parser = argparse.ArgumentParser(description="aiohttp app")
parser.add_argument('--path')
async def hello(request):
return web.Response(text="Hello, world")
if __name__ == '__main__':
args = parser.parse_args()
app = web.Application()
app.add_routes([web.get('/', hello)])
# 核心是path
web.run_app(app,path=args.path)
- 启动
python tests/web.py --path=demo.sock
访问
- curl 模式格式
curl --unix-socket demo.sock http://localhost/
- 实际访问
推荐基于Supervisord的多集成模式,可以运行多个unix socket 这样可以提高系统资源利用率
nginx 参考配置
http {
upstream aiohttp {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response
# Unix domain servers
server unix:/tmp/web_1.sock fail_timeout=0;
server unix:/tmp/web_2.sock fail_timeout=0;
server unix:/tmp/web_3.sock fail_timeout=0;
server unix:/tmp/web_4.sock fail_timeout=0;
# Unix domain sockets are used in this example due to their high performance,
# but TCP/IP sockets could be used instead:
# server 127.0.0.1:8081 fail_timeout=0;
# server 127.0.0.1:8082 fail_timeout=0;
# server 127.0.0.1:8083 fail_timeout=0;
# server 127.0.0.1:8084 fail_timeout=0;
}
}
Supervisord
[program:aiohttp]
numprocs = 4
numprocs_start = 1
process_name = web%(process_num)s
; Unix socket paths are specified by command line.
command=/path/to/web.py --path=/tmp/web_%(process_num)s.sock
user=nobody
autostart=true
autorestart=true
aiohttp 对于unix socket 的处理
- aiohttp/web.py
可以看到如path 不为空,aiohttp 在sites 就会添加unix socket,同时还会判断是否是多个
if path is not None:
if isinstance(path, (str, os.PathLike)):
sites.append(
UnixSite(
runner,
path,
ssl_context=ssl_context,
backlog=backlog,
)
)
else:
for p in path:
sites.append(
UnixSite(
runner,
p,
ssl_context=ssl_context,
backlog=backlog,
)
)
- UnixSite 处理
start 方法中通过asyncio创建的unix_server
async def start(self) -> None:
await super().start()
loop = asyncio.get_event_loop()
server = self._runner.server
assert server is not None
self._server = await loop.create_unix_server(
server,
self._path,
ssl=self._ssl_context,
backlog=self._backlog,
)
说明
为了提高系统的性能基于unix socket 是一个很不错的选择,也是不少项目优先选择
参考资料
https://docs.aiohttp.org/en/stable/deployment.html
http://supervisord.org/
aiohttp/web.py
aiohttp/web_runner.py