Flask和Flask插座:从外部modu内部接收事件

2024-03-28 14:08:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个应用程序,从科学仪器中读取一些信息,并使用远程web客户端显示这些信息。我用烧瓶和烧瓶来达到这个目的。我有一个主application.py文件,其中包含一个名为app的烧瓶实例。我把它和Flask-SocketIO一起使用。Flask部署的HTML页面包含一个Javascript Socketio实例,该实例与服务器应用程序(python)通信。在

此外,我有一个外部模块,代表我的科学仪器。我想要从这个模块类的网站(用Javascript)接收事件。我尝试将appsocketiopython对象传递给构造函数,以便从烧瓶app请求上下文。这只适用于从模块发出事件。

简而言之,application.py具有以下基本结构:

from gevent import monkey
monkey.patch_all()
from flask import Flask, Response, render_template, session, request, g
from flask_socketio import SocketIO, emit, disconnect, Namespace
from Instrument import *

app = Flask(__name__)
app.debug = True
socketio = SocketIO(app)

# some stuff here with @app.routes
@app.route('/instrument')
def inst(arg):
    t = Instrument(arg, app, socketio)

if __name__ == '__main__':
    socketio.run(app, port=8000, log_output=True)

对于外部模块Instrument.py,我试图使用decorator语法,但没有成功(有点{},我对它们不太熟悉)。所以我尝试使用以下代码:

^{pr2}$

我得到的错误如下(编辑:添加了两行有用的行):

[2017-03-03 06:31:31,456][INFO] - _handle_event: received event "my event" from a40724e9e60e4a61ace9e19e59ceabda [/telescope]
[2017-03-03 06:31:31,604][INFO] - handle_get_request: a40724e9e60e4a61ace9e19e59ceabda: Received request to upgrade to websocket
[2017-03-03 06:31:33 +0000] [6333] [ERROR] Error handling request /socket.io/?EIO=3&transport=websocket&sid=24e7d134ea4c4840888c28e0e3ff1f6d
Traceback (most recent call last):
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 52, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 152, in handle_request
    super(GeventWorker, self).handle_request(*args)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 103, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/flask_socketio/__init__.py", line 42, in __call__
    start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/middleware.py", line 47, in __call__
    return self.engineio_app.handle_request(environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/socketio/server.py", line 353, in handle_request
    return self.eio.handle_request(environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/server.py", line 260, in handle_request
    environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/socket.py", line 86, in handle_get_request
    start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/socket.py", line 127, in _upgrade_websocket
    return ws(environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/async_gevent.py", line 34, in __call__
    raise RuntimeError('You need to use the gevent-websocket server. '
RuntimeError: You need to use the gevent-websocket server. See the Deployment section of the documentation for more information.

当应用程序初始化时,日志显示:

2017-03-04 02:52:04 [23530] [INFO] Starting gunicorn 18.0
2017-03-04 02:52:04 [23530] [INFO] Listening at: http://127.0.0.1:8000 (23530)
2017-03-04 02:52:04 [23530] [INFO] Using worker: gevent
2017-03-04 02:52:04 [23534] [INFO] Booting worker with pid: 23534

所以我试着用gunicorn -k gevent -w 1 module:app和{}来配置文件/etc/init/myapp.conf,但是我得到了相同的错误。在

在浏览器中,我看到另一个错误,它与“主”错误同时出现:

WebSocket connection to 'ws://somewebpage.com:2080/socket.io/?
EIO=3&transport=websocket&sid=572923d1b8fd402795bba50823941520' 
failed: Error during WebSocket handshake: Unexpected response code: 500

如何从外部模块正确接收事件?也许有更好的方法来达到我想要的结果。如果有人能帮我一把,我将不胜感激。在


Tags: pyapphomevenvrequestlibpackageslocal
3条回答

我认为你犯的错误与你的问题无关。这意味着您正在为gevent使用标准的gunicorn worker,但是您需要使用gevent websocket worker来工作。请参阅本文档的section中的最后一个示例。在

你能试着在^{中添加policy_server=False

我自己回答。我找到了following post,它解释了错误:

WebSocket connection to 'ws://somewebpage.com:2080/socket.io/?
EIO=3&transport=websocket&sid=572923d1b8fd402795bba50823941520' 
failed: Error during WebSocket handshake: Unexpected response code: 500

是由于nginx配置文件中的错误配置导致的。应该是这样的:

^{pr2}$

现在工作起来就像一个魅力:从导入的模块和主python脚本发出和接收事件。在

相关问题 更多 >