回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在开发一个应用程序,从科学仪器中读取一些信息,并使用远程web客户端显示这些信息。我用烧瓶和烧瓶来达到这个目的。我有一个主<code>application.py</code>文件,其中包含一个名为<code>app</code>的烧瓶实例。我把它和<code>Flask-SocketIO</code>一起使用。Flask部署的HTML页面包含一个Javascript Socketio实例,该实例与服务器应用程序(python)通信。在</p>
<p>此外,我有一个外部模块,代表我的科学仪器。我想要从这个模块类的网站(用Javascript)接收事件。我尝试将<code>app</code>和<code>socketio</code>python对象传递给构造函数,以便从烧瓶<code>app</code>请求上下文。这只适用于从模块发出事件。</em></p>
<p>简而言之,<code>application.py</code>具有以下基本结构:</p>
<pre><code>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)
</code></pre>
<p>对于外部模块<code>Instrument.py</code>,我试图使用decorator语法,但没有成功(有点{<cd9>},我对它们不太熟悉)。所以我尝试使用以下代码:</p>
^{pr2}$
<p>我得到的错误如下(编辑:添加了两行有用的行):</p>
<pre><code>[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.
</code></pre>
<p>当应用程序初始化时,日志显示:</p>
<pre><code>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
</code></pre>
<p>所以我试着用<code>gunicorn -k gevent -w 1 module:app</code>和{<cd12>}来配置文件<code>/etc/init/myapp.conf</code>,但是我得到了相同的错误。在</p>
<p>在浏览器中,我看到另一个错误,它与“主”错误同时出现:</p>
<pre><code>WebSocket connection to 'ws://somewebpage.com:2080/socket.io/?
EIO=3&transport=websocket&sid=572923d1b8fd402795bba50823941520'
failed: Error during WebSocket handshake: Unexpected response code: 500
</code></pre>
<p>如何从外部模块正确接收事件?也许有更好的方法来达到我想要的结果。如果有人能帮我一把,我将不胜感激。在</p>