当我跑步时,我的Flask应用程序可以在本地运行
flask run -p 8000
但当我尝试在Docker中运行这个时,我的SocketIO事件似乎无法从服务器传递到客户端。
下面是一个示例应用程序来说明我的意思:
烧瓶应用程序:
import flask
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import requests
import logging
import time
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, logger=False, engineio_logger=False)
# Flask App
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('hello')
def connected(message):
data = message
print('Message received from browser:', data)
# Thanks I got your message
emit('reply', {'data': 'Hello browser. I received your message.'})
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8000)))
Dockerfile:
FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7
RUN apk add g++
RUN apk add linux-headers
RUN apk add libc-dev
RUN apk add musl-dev
COPY requirements.txt /
RUN python -m pip install --upgrade pip
RUN pip3 install numpy
RUN pip install -r /requirements.txt
COPY . /
WORKDIR /
ENV PORT 8000
RUN chmod +x ./gunicorn_starter.sh
ENTRYPOINT ["sh","./gunicorn_starter.sh"]
gunicorn_启动器。上海:
#!/bin/bash
gunicorn --chdir / app:app -w 2 --threads 2 --workers 1 -b 0.0.0.0:8000
Docker的运行速度非常慢,SocketIO事件似乎没有注册。这就是我看到的错误:
[2022-01-27 20:35:31 +0000] [9] [ERROR] Error handling request /socket.io/?EIO=3&transport=websocket&sid=7862ffa61dcc4c1abbf38c368975d2b9
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/gunicorn/workers/gthread.py", line 279, in handle
keepalive = self.handle_request(req, conn)
File "/usr/lib/python3.6/site-packages/gunicorn/workers/gthread.py", line 336, in handle_request
resp.close()
File "/usr/lib/python3.6/site-packages/gunicorn/http/wsgi.py", line 409, in close
self.send_headers()
File "/usr/lib/python3.6/site-packages/gunicorn/http/wsgi.py", line 325, in send_headers
tosend = self.default_headers()
File "/usr/lib/python3.6/site-packages/gunicorn/http/wsgi.py", line 306, in default_headers
elif self.should_close():
File "/usr/lib/python3.6/site-packages/gunicorn/http/wsgi.py", line 229, in should_close
if self.status_code < 200 or self.status_code in (204, 304):
AttributeError: 'Response' object has no attribute 'status_code'