Skip to content

Commit 7d7f223

Browse files
authored
aiohttp base nginx setup (#9796)
* aiohttp base nginx setup * apply review fixes * tune nginx worker settings * use linux socket * use nginx as default aiohttp server * remove gunicorn from requirements
1 parent fd07bef commit 7d7f223

File tree

9 files changed

+157
-12
lines changed

9 files changed

+157
-12
lines changed

frameworks/Python/aiohttp/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ This will switch which database engine the app uses to execute queries with test
3535

3636
### Server
3737

38-
gunicorn+uvloop on CPython
38+
nginx+uvloop on CPython
3939

4040
## Test URLs
4141

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM python:3.13
2+
3+
ADD ./ /aiohttp
4+
5+
WORKDIR aiohttp
6+
7+
RUN pip3 install cython==3.0.11 gunicorn==23.0.0 && \
8+
pip3 install -r /aiohttp/requirements.txt
9+
10+
ENV CONNECTION=RAW
11+
12+
EXPOSE 8080
13+
14+
CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
FROM python:3.13
22

3-
ADD ./ /aiohttp
3+
RUN apt-get update && apt-get install -y nginx
44

5-
WORKDIR aiohttp
5+
ADD ./requirements.txt /aiohttp/requirements.txt
66

77
RUN pip3 install cython==3.0.11 && \
88
pip3 install -r /aiohttp/requirements.txt
99

10+
ADD ./ /aiohttp
11+
1012
WORKDIR /aiohttp
1113

14+
ENV CONNECTION=ORM
15+
1216
EXPOSE 8080
1317

14-
CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py
18+
RUN chmod +x /aiohttp/nginx-entrypoint.sh
19+
20+
ENTRYPOINT ["/aiohttp/nginx-entrypoint.sh"]
21+
22+
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
FROM python:3.13
22

3-
ADD ./ /aiohttp
3+
RUN apt-get update && apt-get install -y nginx
44

5-
WORKDIR aiohttp
5+
ADD ./requirements.txt /aiohttp/requirements.txt
66

77
RUN pip3 install cython==3.0.11 && \
88
pip3 install -r /aiohttp/requirements.txt
99

10+
ADD ./ /aiohttp
11+
12+
WORKDIR /aiohttp
13+
1014
ENV CONNECTION=RAW
1115

1216
EXPOSE 8080
1317

14-
CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py
18+
RUN chmod +x /aiohttp/nginx-entrypoint.sh
19+
20+
ENTRYPOINT ["/aiohttp/nginx-entrypoint.sh"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import uvloop
2+
from aiohttp import web
3+
import argparse
4+
from .main import create_app
5+
6+
7+
if __name__ == '__main__':
8+
uvloop.install()
9+
parser = argparse.ArgumentParser()
10+
parser.add_argument('--socket', type=str, required=True)
11+
args = parser.parse_args()
12+
13+
app = create_app()
14+
web.run_app(app, path=args.socket)

frameworks/Python/aiohttp/benchmark_config.json

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,35 @@
1717
"flavor": "Python3",
1818
"orm": "Raw",
1919
"platform": "asyncio",
20-
"webserver": "gunicorn",
20+
"webserver": "nginx",
2121
"os": "Linux",
2222
"database_os": "Linux",
2323
"display_name": "aiohttp",
2424
"notes": "uses asyncpg for database access"
2525
},
26+
"gunicorn": {
27+
"json_url": "/json",
28+
"db_url": "/db",
29+
"query_url": "/queries/",
30+
"fortune_url": "/fortunes",
31+
"update_url": "/updates/",
32+
"plaintext_url": "/plaintext",
33+
"port": 8080,
34+
"approach": "Realistic",
35+
"classification": "Micro",
36+
"database": "Postgres",
37+
"framework": "aiohttp",
38+
"language": "Python",
39+
"flavor": "Python3",
40+
"orm": "Raw",
41+
"platform": "asyncio",
42+
"webserver": "nginx",
43+
"os": "Linux",
44+
"database_os": "Linux",
45+
"display_name": "aiohttp-gunicorn",
46+
"notes": "uses gunicorn as proxy server",
47+
"versus": "default"
48+
},
2649
"orm": {
2750
"db_url": "/db",
2851
"query_url": "/queries/",
@@ -37,7 +60,7 @@
3760
"flavor": "Python3",
3861
"orm": "Full",
3962
"platform": "asyncio",
40-
"webserver": "gunicorn",
63+
"webserver": "nginx",
4164
"os": "Linux",
4265
"database_os": "Linux",
4366
"display_name": "aiohttp-orm",

frameworks/Python/aiohttp/config.toml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,26 @@ database_os = "Linux"
1515
os = "Linux"
1616
orm = "Raw"
1717
platform = "asyncio"
18-
webserver = "gunicorn"
18+
webserver = "nginx"
1919
versus = "None"
2020

21+
[gunicorn]
22+
urls.plaintext = "/plaintext"
23+
urls.json = "/json"
24+
urls.db = "/db"
25+
urls.query = "/queries/"
26+
urls.update = "/updates/"
27+
urls.fortune = "/fortunes"
28+
approach = "Realistic"
29+
classification = "Micro"
30+
database = "Postgres"
31+
database_os = "Linux"
32+
os = "Linux"
33+
orm = "Raw"
34+
platform = "asyncio"
35+
webserver = "gunicorn"
36+
versus = "default"
37+
2138
[orm]
2239
urls.db = "/db"
2340
urls.query = "/queries/"
@@ -30,5 +47,5 @@ database_os = "Linux"
3047
os = "Linux"
3148
orm = "Full"
3249
platform = "asyncio"
33-
webserver = "gunicorn"
50+
webserver = "nginx"
3451
versus = "default"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/sh
2+
set -e
3+
CORES=$(nproc)
4+
echo "$CORES cores detected, starting $CORES aiohttp workers..."
5+
6+
for i in $(seq 0 $((CORES-1))); do
7+
SOCKET="/run/aiohttp-$i.sock"
8+
echo "Starting worker on socket $SOCKET"
9+
python3 -O -m app.app --socket $SOCKET &
10+
done
11+
12+
echo "Waiting for all workers to be ready..."
13+
for i in $(seq 0 $((CORES-1))); do
14+
SOCKET="/run/aiohttp-$i.sock"
15+
until [ -S "$SOCKET" ]; do
16+
echo "Waiting for socket $SOCKET..."
17+
sleep 0.2
18+
done
19+
chown root:www-data "$SOCKET"
20+
chmod 660 "$SOCKET"
21+
done
22+
23+
cat > /aiohttp/nginx.conf <<EOF
24+
user www-data;
25+
worker_processes auto;
26+
27+
events {
28+
worker_connections 65535;
29+
}
30+
31+
http {
32+
keepalive_requests 10000000;
33+
34+
upstream aiohttp {
35+
least_conn;
36+
EOF
37+
38+
for i in $(seq 0 $((CORES-1))); do
39+
echo " server unix:/run/aiohttp-$i.sock fail_timeout=0;" >> /aiohttp/nginx.conf
40+
done
41+
42+
cat >> /aiohttp/nginx.conf <<EOF
43+
keepalive 32;
44+
}
45+
46+
server {
47+
listen 8080 reuseport;
48+
49+
access_log off;
50+
error_log stderr error;
51+
52+
location / {
53+
proxy_pass http://aiohttp;
54+
proxy_http_version 1.1;
55+
proxy_set_header Connection "";
56+
proxy_redirect off;
57+
proxy_buffering off;
58+
}
59+
}
60+
}
61+
EOF
62+
63+
echo "Starting Nginx..."
64+
nginx -c /aiohttp/nginx.conf -g "daemon off;"

frameworks/Python/aiohttp/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
aiohttp==3.11.16
22
asyncpg==0.30.0
3-
gunicorn==23.0.0
43
jinja2==3.1.6
54
SQLAlchemy==2.0.40
65
orjson==3.10.16

0 commit comments

Comments
 (0)