Flask,Python和Socket.io:多线程应用程序给我"RuntimeError:在请求上下文之外工作" [英] Flask, Python and Socket.io: multithreading app is giving me "RuntimeError: working outside of request context"

查看:499
本文介绍了Flask,Python和Socket.io:多线程应用程序给我"RuntimeError:在请求上下文之外工作"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Flask Python Flask-Socket.io 库开发应用程序.我遇到的问题是,由于某些上下文问题,以下代码将无法正确执行emit

I have been developing an app using Flask, Python and Flask-Socket.io library. The problem I have is that the following code will not perform an emit correctly due to some contexts issue

RuntimeError: working outside of request context

到目前为止,我只为整个程序编写一个python文件.这是我的代码( test.py ):

I am writing only one python file for the entire program by now. This is my code (test.py):

from threading import Thread
from flask import Flask, render_template, session, request, jsonify, current_app, copy_current_request_context
from flask.ext.socketio import *

app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

def somefunction():
    # some tasks
    someotherfunction()

def someotherfunction():
    # some other tasks
    emit('anEvent', jsondata, namespace='/test') # here occurs the error

@socketio.on('connect', namespace='/test')
def setupconnect():
    global someThread
    someThread = Thread(target=somefunction)
    someThread.daemon = True

if __name__ == '__main__':
    socketio.run(app)

在StackExchange中,我一直在阅读一些解决方案,但是它们没有用.我不知道我在做什么错.

Here in StackExchange I have been reading some solutions, but they didn't work. I do not know what I am doing wrong.

我尝试在emit之前添加with app.app_context()::

def someotherfunction():
    # some other tasks
    with app.app_context():
        emit('anEvent', jsondata, namespace='/test') # same error here

我尝试过的另一种解决方案是在someotherfunction()之前添加装饰器copy_current_request_context,但它表示装饰器必须在本地范围内.我将其放在someotherfunction()的第一行中,但存在相同的错误.

Another solution I tried is adding the decorator copy_current_request_context before someotherfunction() but it says that the decorator must be in a local scope. I put it in inside someotherfunction(), first line, but same error.

如果有人可以帮助我,我会很高兴.预先感谢.

I would be glad if someone can help me with this. Thanks in advance.

推荐答案

您的错误是在请求上下文之外工作".您试图通过推送应用程序上下文来解决它.相反,您应该推送请求上下文.请参见 http:/上有关烧瓶中上下文的说明. /kronosapiens.github.io/blog/2014/08/14/understanding-contexts-in-flask.html

Your error is 'working outside of request context'. You tried to resolve it by pushing the application context. Instead you should push the request context. See the explanation on contexts in flask on http://kronosapiens.github.io/blog/2014/08/14/understanding-contexts-in-flask.html

somefunction()中的代码可能使用了在请求上下文内部是全局的对象(如果我不得不猜测您可能使用了请求对象).如果您的代码未在新线程内执行,则可能会起作用.但是,当您在新线程中执行该函数时,您的函数将不再在原始请求上下文中执行,并且不再有权访问特定于请求上下文的对象.因此,您必须手动推动它.

The code in your somefunction() probably uses objects (if i had to guess you probably use the request object) that are global inside the request context. Your code probably works when it is not executed inside the new thread. But when you execute it in a new thread your function is not executed in the original request context anymore and it does not have access to the request context specific objects anymore. So you have to push it manually.

所以您的功能应该是

def someotherfunction():
    with app.test_request_context('/'):
        emit('anEvent', jsondata, namespace='/test')

这篇关于Flask,Python和Socket.io:多线程应用程序给我"RuntimeError:在请求上下文之外工作"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆