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

时间:2019-09-27 09:08:44

标签: python python-2.7 flask

我正在尝试用ROS和烧瓶控制机器人。问题是,当我用ctrl-c(SIGINT)烧瓶杀死ROS时,此过程的速度变慢,因为它没有立即关闭。我已经实现了一个signal_handler来处理ctrl-c并关闭烧瓶。

问题是当我运行此程序并按

ctrl-c我立即关闭了所有内容,但出现以下错误:

  

RuntimeError:在请求上下文之外工作。

如何解决此错误?

#!/usr/bin/env python

import rospy
from raspimouse_ros.msg import MotorFreqs
from time import sleep
from flask import Flask, request
from os.path import join, dirname
from signal import signal, SIGINT

cwd = dirname(__file__)
open(join(cwd, "file.html"))

app = Flask(__name__)

deltaX = 0
deltaY = 0

pub = rospy.Publisher('/motor_raw', MotorFreqs, queue_size=10)
rospy.init_node('control')

msg = MotorFreqs()

def signal_handler(signal_received, frame):
    msg.left = 0
    msg.right = 0
    pub.publish(msg)
    print("Quitting .......")
    func = request.environ.get('werkzeug.server.shutdown')
    func()

signal(SIGINT,signal_handler)

@app.route("/")
def main():
    with open(join(cwd, "file.html"), 'r') as f:
        program = f.read()
    return program

@app.route("/SetSpeed")
def SetSpeed():
    global deltaX
    global deltaY
    deltaX = int(float(request.args.get('x')) * 4)
    deltaY = int(float(request.args.get('y')) * 10)
    publisher()
    return ""

def publisher():
    msg.left = int(-deltaY+deltaX)
    msg.right = int(-deltaY-deltaX)
    rospy.loginfo(msg)
    pub.publish(msg) 

app.run(host="0.0.0.0")
[control-1] killing on exit
Quitting .......
Traceback (most recent call last):
  File "/home/pi/workspace/src/manual_control/scripts/control.py", line 54, in <module>
    app.run(host="0.0.0.0")
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 841, in run
    run_simple(host, port, self, **options)
  File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 708, in run_simple
    inner()
  File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 673, in inner
    srv.serve_forever()
  File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 511, in serve_forever
    HTTPServer.serve_forever(self)
  File "/usr/lib/python2.7/SocketServer.py", line 231, in serve_forever
    poll_interval)
  File "/usr/lib/python2.7/SocketServer.py", line 150, in _eintr_retry
    return func(*args)
  File "/home/pi/workspace/src/manual_control/scripts/control.py", line 28, in signal_handler
    func = request.environ.get('werkzeug.server.shutdown')
  File "/usr/lib/python2.7/dist-packages/werkzeug/local.py", line 343, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/usr/lib/python2.7/dist-packages/werkzeug/local.py", line 302, in _get_current_object
    return self.__local()
  File "/usr/lib/python2.7/dist-packages/flask/globals.py", line 37, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.
shutting down processing monitor...
... shutting down processing monitor complete
done

1 个答案:

答案 0 :(得分:0)

请求上下文在请求到达时处于活动状态,并在完成后被销毁,因此signal_handler函数中不可用,但是您可以访问app_context