django - 无尽的 Django websocket
问题描述
有一个页面有一个按钮,我可以单击该按钮并通过 Websocket 在服务器上启动该过程。我通过 Websocket 获取有关进程的信息。如何关闭并重新打开页面以查看进程的当前状态,而不是一开始就没有断开连接?
async def worker_status(send):
for i in range(numbers):
some_function()
await asyncio.sleep(1)
async def websocket_application(scope, receive, send):
while True:
event = await receive()
if event['type'] == 'websocket.connect':
await send({
'type': 'websocket.accept'
})
if event['type'] == 'websocket.disconnect':
break
if event['type'] == 'websocket.receive':
if event['text'] == 'StartAct':
loop = asyncio.get_event_loop()
loop.create_task(worker_status())
await send({
'type': 'websocket.send',
'text': str("In process")
})
if event['text'] == 'Status':
await send({
'type': 'websocket.send',
'text': str("Ready to process")
})
解决方案
推荐阅读
- javascript - TextArea 创建了太多空间。如何使其局限于对象的大小?
- sql - 如何从多个工作中获得前 X 记录而不重叠?
- python - Python 矩阵求解器
- sql - 如何选择提供年份作为函数参数的位置
- ajax - 如何在 ReactJS 中使用 XMLHttpRequest 将驻留在同一应用程序中的文件作为 blob 导入
- c - 如何在另一个头文件中包含 c 头文件?
- postgresql - 有没有办法在 PostgreSQL 9.0 版中从备用服务器查看主详细信息?
- php - 在 Woocommerce 中通过选择的付款方式预先选择的一种运输方式
- c# - 将文本框添加到datagridview的每一行的问题
- c - 使用传递引用/取消引用的奇怪行为