Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask, Python and Socket.io: multithreading app is giving me "RuntimeError: working outside of request context"

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

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)

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

I have tried adding a with app.app_context(): before my emit:

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

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.

like image 351
Sebasthian Ogalde Avatar asked Jul 27 '15 07:07

Sebasthian Ogalde


2 Answers

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

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.

so your function should be

def someotherfunction():
    with app.test_request_context('/'):
        emit('anEvent', jsondata, namespace='/test')
like image 172
Mensur Avatar answered Oct 11 '22 14:10

Mensur


You are using the wrong emit here. You have to use the emit of the socketio object you created. so instead of

emit('anEvent', jsondata, namespace='/test') # here occurs the error use: socketio.emit('anEvent', jsondata, namespace='/test') # here occurs the error

like image 42
Burton B Williams Avatar answered Oct 11 '22 13:10

Burton B Williams