Skip to content

Commit 355cd28

Browse files
committed
use linux socket
1 parent 5cf5fd9 commit 355cd28

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

frameworks/Python/aiohttp/aiohttp-nginx.dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
FROM python:3.13
22

3-
RUN apt-get update && apt-get install -y \
4-
nginx \
5-
netcat-openbsd
3+
RUN apt-get update && apt-get install -y nginx
64

75
ADD ./requirements.txt /aiohttp/requirements.txt
86

frameworks/Python/aiohttp/app/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
if __name__ == '__main__':
88
uvloop.install()
99
parser = argparse.ArgumentParser()
10-
parser.add_argument('--port', type=int, required=True)
10+
parser.add_argument('--socket', type=str, required=True)
1111
args = parser.parse_args()
1212

1313
app = create_app()
14-
web.run_app(app, port=args.port)
14+
web.run_app(app, path=args.socket)

frameworks/Python/aiohttp/nginx-entrypoint.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ CORES=$(nproc)
44
echo "$CORES cores detected, starting $CORES aiohttp workers..."
55

66
for i in $(seq 0 $((CORES-1))); do
7-
PORT=$((8000 + i))
8-
echo "Starting worker on port $PORT"
9-
python3 -O -m app.app --port $PORT &
7+
SOCKET="/run/aiohttp-$i.sock"
8+
echo "Starting worker on socket $SOCKET"
9+
python3 -O -m app.app --socket $SOCKET &
1010
done
1111

1212
echo "Waiting for all workers to be ready..."
1313
for i in $(seq 0 $((CORES-1))); do
14-
PORT=$((8000 + i))
15-
until nc -z localhost $PORT; do
16-
echo "Waiting for port $PORT..."
14+
SOCKET="/run/aiohttp-$i.sock"
15+
until [ -S "$SOCKET" ]; do
16+
echo "Waiting for socket $SOCKET..."
1717
sleep 0.1
1818
done
19+
chown root:www-data "$SOCKET"
20+
chmod 660 "$SOCKET"
1921
done
2022

2123
cat > /aiohttp/nginx.conf <<EOF
24+
user www-data;
2225
worker_processes auto;
2326
2427
events {
@@ -33,7 +36,7 @@ http {
3336
EOF
3437

3538
for i in $(seq 0 $((CORES-1))); do
36-
echo " server 127.0.0.1:$((8000 + i));" >> /aiohttp/nginx.conf
39+
echo " server unix:/run/aiohttp-$i.sock fail_timeout=0;" >> /aiohttp/nginx.conf
3740
done
3841

3942
cat >> /aiohttp/nginx.conf <<EOF

0 commit comments

Comments
 (0)