Flask + Flask_SocketIO = RuntimeError:在请求上下文之外工作

时间:2018-04-09 05:02:30

标签: python flask flask-socketio

我有以下主文件。

from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit, send
import gpio_control
from gevent import monkey
monkey.patch_all()


simplyfishy = Flask(__name__)
simplyfishy.config['SECRET_KEY'] = 'ARG'
socketio = SocketIO(simplyfishy, async_mode='gevent')

@simplyfishy.route('/')
def index():
    return render_template('home.html')


@socketio.on('message')
def handle_message(message):
    emit(message, {'data': 'main!'})


if __name__ == '__main__':
    socketio.run(simplyfishy, host='0.0.0.0')

这是gpio_control文件

import RPi.GPIO as GPIO
from pushbullet import Pushbullet
from flask_socketio import emit

# Set the GPIO mode to Broadcom
GPIO.setmode(GPIO.BCM)

# Create a dictionary for sensors ans their status
float_switches = {
    24: {'name': 'Float Switch 1', 'state': GPIO.LOW},
    25: {'name': 'Float Switch 2', 'state': GPIO.LOW}
}

# Setup float switches
for float_switch in float_switches:
    GPIO.setup(float_switch, GPIO.IN, pull_up_down=GPIO.PUD_UP)


# Define callback function for event detection
def floatsw(channel):
    from __main__ import simplyfishy
    with simplyfishy.app_context():
        if GPIO.input(channel):
            print(float_switches[channel]['name'] + " deactivated!")
            emit('float_sw', {'data': 'deactivated!'})
        else:
            print(float_switches[channel]['name'] + " activated!")
            # pb.push_note("Simply Fishy", "Sump water level is low")
            emit('float_sw', {'data': 'activated!'})


GPIO.add_event_detect(24, GPIO.BOTH, callback=floatsw, bouncetime=1000)
GPIO.add_event_detect(25, GPIO.BOTH, callback=floatsw, bouncetime=1000)

我想要做的是,您可以看到我想向烧瓶页面发送更新,显示交换机已实时激活或停用。当我触发浮动开关时,我得到以下

Float Switch 1 activated!
Traceback (most recent call last):
  File "/home/pi/simplyfishy/gpio_control.py", line 61, in floatsw
    emit('float_sw', {'data': 'activated!'})
  File "/home/pi/.local/lib/python2.7/site-packages/flask_socketio/__init__.py", line 688, in emit
    namespace = flask.request.namespace
  File "/home/pi/.local/lib/python2.7/site-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/pi/.local/lib/python2.7/site-packages/werkzeug/local.py", line 306, in _get_current_object
    return self.__local()
  File "/home/pi/.local/lib/python2.7/site-packages/flask/globals.py", line 37, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

我用Google搜索并查看其他示例,但我似乎无法找出问题所在。我认为甚至不需要主文件中的@socketio.on('message'),但也许它是,我需要从我的gpio_control中触发,以便将其发送到页面?我觉得我错过了这个流程或简单的东西。非常感谢帮助!

编辑:更新以下代码并修复了错误消息。但是,这样做我确实有循环依赖。我相信由于以下错误消息。

ImportError:无法在控制台中导入名称simplefishy。谷歌向我展示的是循环依赖问题。

1 个答案:

答案 0 :(得分:1)

问题是emit()函数中的两个floatsw()调用没有提供足够的信息,因此他们尝试从请求上下文中获取缺少的数据。由于在没有请求上下文的情况下调用该函数,因此您会收到错误。

缺少的两个信息是emit的接收者和命名空间。看起来您没有为此应用程序使用自定义命名空间,因此您可以通过在这两个出口中添加namespace='/'作为参数来解决缺少的命名空间。

就收件人而言,您可以添加broadcast=True发送给所有已连接的客户端,或者使用room=<sid>,其中<sid>是您要发送的客户端的会话ID消息给。您还可以使用您在此处创建的任何自定义会议室的名称。

总而言之,避免错误的快速而肮脏的方法是更改​​您的发出,如下所示:

def floatsw(channel):
    with simplyfishy.app_context():
        if GPIO.input(channel):
            print(float_switches[channel]['name'] + " deactivated!")
            emit('float_sw', {'data': 'deactivated!'}, namespace='/', broadcast=True)
        else:
            print(float_switches[channel]['name'] + " activated!")
            # pb.push_note("Simply Fishy", "Sump water level is low")
            emit('float_sw', {'data': 'activated!'}, namespace='/', broadcast=True)

然后,您可能需要制定一项策略,以避免广播并仅针对特定客户。

编辑:在示例中添加了应用程序上下文。

相关问题