IOError:标头已经发送 - 使用gevent_uwsgi烧瓶socketio

时间:2016-11-18 23:20:40

标签: python sockets nginx flask uwsgi

Flask,uwsgi,nginx mongo socket应用程序。

定期产生此错误

 File "/home/username/venv/local/lib/python2.7/site-packages/engineio/server.py", line 277, in handle_request
    start_response(r['status'], r['headers'] + cors_headers)
IOError: headers already sent

File "/home/username/venv/local/lib/python2.7/site-packages/flask_socketio/__init__.py", line 562, in _handle_event
    app = self.server.environ[sid]['flask.app']
KeyError: 'flask.app'

这里所有的配置:

uwsgi.ini

[uwsgi]
module = socket_app:app
chdir = /home/username/app_dir/
virtualenv = /home/usernamevenv/
touch-reload = /home/username/reload.txt
logdate = 1
logto = /home/usernamelogs/socket.log
procname = socket_username
log-maxsize  = 204800
env = LANG=ru_RU.utf8
env = LC_ALL=ru_RU.utf8
env = LC_LANG=ru_RU.utf8

额外的args:

--http :3456 --gevent 100 --http-websockets

Nginx套接字代理

server {
    listen      80;

    server_name domain;
    charset     utf-8;
    client_max_body_size 5M;   # adjust to taste

    location /socket.io {
        proxy_pass http://upstream_name/socket.io;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

}

应用程序init来源:

init.py(在没有套接字的其他网络应用中使用)

from flask_openid import OpenID
from flask_mongoengine import MongoEngine
from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
own_socketio = SocketIO()
db = MongoEngine()

base_app.py(COnfigure application&&&postforked database connection)

# coding: utf-8
from __future__ import unicode_literals
from flask import Flask, g
from flask_mongoengine import MongoEngine, MongoEngineSessionInterface
from blueprint.monitor import monitor
from init import oid, app, db
from flask_mongoengine import MongoEngine
from uwsgidecorators import postfork


def make_app():
    app.register_blueprint(monitor)
    app.config.from_pyfile('settings.py')
    #if not UWSGI_ALLOWED:
    #    db.init_app(app)
    oid.init_app(app)
    app.session_interface = MongoEngineSessionInterface(db)
    return app

@postfork
def setup_db():
    db.init_app(app)

和socket_app.py

from gevent.monkey import patch_all

patch_all()

from init import own_socketio
from init import app
from base_app import make_app

make_app()
own_socketio.init_app(app, async_mode='gevent_uwsgi', message_queue=app.config['SOCKETIO_REDIS_URL'], cookie='session')

if __name__ == '__main__':
    own_socketio.run(app, port=3456)

0 个答案:

没有答案