使用Websockets在Python中连续流输出程序中的输出

问题描述

我想创建一个websocket,它将持续地将程序的输出流传输到HTML网页。

我的程序执行的过程需要2分钟才能完成,并且在执行时记录输出。现在,我的代码每次程序完成执行时都会更新网页,然后立即在网页上显示所有日志。我想持续更新我的网页,即:实时流输出。我的服务器代码如下:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
  <div><input type='radio' id="one" /></div>
  <div><input type='radio' id="two" /></div>
</div>
<div class="content">
  <div><input type='radio' id="three" /></div>
  <div><input type='radio' id="four" /></div>
</div>
<div class="content">
  <div><input type='radio' id="five" /></div>
  <div><input type='radio' id="six" /></div>
</div>

我的客户端看起来像这样:

import nest_asyncio
nest_asyncio.apply()
import websockets
import subprocess

async def time(websocket,path):
    while True:
        command = ['myprogram','args']
        process = subprocess.Popen(command,stdout=subprocess.PIPE,universal_newlines=True,bufsize=-1)
        Now = process.stdout.read()
        await websocket.send(Now)

start_server = websockets.serve(time,"127.0.0.1",5670)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

关于如何做到这一点的任何想法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)