【发布时间】:2020-06-16 14:07:12
【问题描述】:
TLDR;我正在尝试运行一个从 web 套接字 URI 获取数据的客户端,然后使用 Flask 来提供从套接字获取的数据。
我的预期工作流程如下:从网络套接字读取数据(异步)-> 将数据写入 Python 字典(连续)-> 使用 Flask 上的 GET 请求从 Python 字典读取数据(连续)
我面临的问题是我使用 Python 字典进行存储,但是当我从 Flask 读取该字典时,它没有显示更新的值。
我为我的问题创建了一个带有代码的插图
客户:
import asyncio
import websockets
import json
SOME_URI = "ws://localhost:8080/foo"
connections = set()
connections.add(SOME_URI)
class Storage:
storage = dict() # local storage dict for simplicity
@staticmethod
def store(data): # here I store the value
a, b, c = data
Storage.storage[a] = c
@staticmethod
def show(): # Here I just show the value to be used in the GET
return Storage.storage
async def consumer_handler(uri):
async with websockets.connect(uri) as websocket:
async for message in websocket:
await consumer(message)
async def consumer(message):
line = json.loads(message)
Storage.store(line) # adds message to dict
async def main():
await asyncio.wait([consumer_handler(uri) for uri in connections])
if __name__ == "__main__":
asyncio.run(main())
应用:
from flask import Flask
from client import Storage
app = Flask(__name__)
app.debug = True
@app.route('/bar', methods=['GET'])
def get_instruments():
res = Storage.show() # I expected here to see updated value for the dict as it fills up from websockets
return res, 200
if __name__ == "__main__":
app.run()
每当我尝试向 Flask 页面发出 GET 请求时,我都会得到一个 Empty dict 的值(不反映我从 Web 套接字添加的更改)。我希望每次 GET 请求都能获得字典的更新值。
【问题讨论】:
标签: python python-3.x flask websocket python-asyncio