4
Jsonify似乎無法在應用程序上下文之外工作,是否有解決方法?不能在Flask websocket中使用Jsonify
我正在用websockets替換一些ajax請求,因爲它是性能和網絡問題所需要的。我在我的環境中用pip安裝了Flask-WebSocket。現在,我得到一個錯誤:
RuntimeError: working outside of application context
我的應用程序的框架如下:
app/
├── forms
├── static
│ ├── css
│ ├── img
│ │ └── DefaultIcon
│ │ ├── eps
│ │ └── png
│ └── js
├── templates
├── ups
└── views
的WebSockets的蟒蛇文件位於視圖/ ajax.py:
# -*- coding: utf-8 -*-
# OS Imports
import time
# Flask Imports
from flask import jsonify
from .. import sockets
from app.functions import get_cpu_load, get_disk_usage, get_vmem
# Local Imports
from app import app
from app.views.constants import info, globalsettings
@sockets.route('/_system')
def _system(ws):
"""
Returns the system informations, JSON Format
CPU, RAM, and Disk Usage
"""
while True:
message = ws.receive()
if message == "update":
cpu = round(get_cpu_load())
ram = round(get_vmem())
disk = round(get_disk_usage())
ws.send(jsonify(cpu=cpu, ram=ram, disk=disk)
我使用此命令啓動我的應用程序:
gunicorn -k flask_sockets.worker app:app
這是我在app /文件夾__init__.py
:
# -*- coding: utf-8 -*-
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
app.config.from_object('config')
from app import views as application
爲什麼不jsonify的工作,我可以用呢?