Ben Flask,Python ve Flask-Socket.io kütüphaneyi kullanan bir uygulamayı geliştirmekteyiz. Ben sorun Aşağıdaki kod nedeniyle bazı bağlamlarda konuylaFlask, Python ve Socket.io: çoklu kullanım uygulaması bana veriyor "RuntimeError: istek bağlamı çalışma dışında"
RuntimeError: working outside of request context
Şimdiye kadar tüm programı için yalnızca bir piton dosyası yazıyorum doğru bir emit
gerçekleştirmez olmasıdır. Bu benim kodu (test.py): İşte Stack Exchange içinde
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)
Ben bazı çözümler okuma olmuştur, ancak işe yaramadı. Neyi yanlış yaptığımı bilmiyorum. Ben with app.app_context():
benim emit
önce ekleyerek denedim:
def someotherfunction():
# some other tasks
with app.app_context():
emit('anEvent', jsondata, namespace='/test') # same error here
Denedim Başka bir çözüm someotherfunction()
önce dekoratör copy_current_request_context
ekliyor ancak dekoratör yerel kapsamında olması gerektiğini söylüyor. İlk satırda someotherfunction()
'un içine koydum ama aynı hata.
Birisi bana bununla yardım edebilirse memnun olurum. Şimdiden teşekkürler.
Ben şişeye bir uzman değilim ama setupconnect' 'üzerinde' küresel someThread' sorunun bir parçası olduğunu düşünürdüm. Ayrıca, belki bu iş parçacığı size yardımcı olabilir: http://stackoverflow.com/questions/9931738/flask-throwing-working-outside-of-request-context-when-starting-sub-thread – cnluzon