python的websocket客户端的心跳包需要开线程吗?

#!/usr/bin/env python
import _thread
import asyncio
import json
import subprocess
import asyncio
import time
import websockets
import multiprocessing

import asyncio
import websockets

process = []


def run_chrome(num, command):
    for i in (0, num):
        child = subprocess.Popen(command)
        pid = child.pid
        process.append({pid: child})
        print(pid)

    print(process)
    child.wait()


async def hello():
    uri = "ws://localhost:8080"
    async with websockets.connect(uri) as websocket:
        j = json.dumps(
            {
                "TOKEN": "A-asdasd",
                "ACTION": "RIGISTER",
                "IP": "127.0.0.1"
            })
        print(j)

        await websocket.send(j)

        while True:
            json_receive = await websocket.recv()
            try:
                if json.loads(json_receive)['TYPE'] == "RUN":
                    print("运行")
            except Exception as e:
                print(e)
            print('收到指令:', json_receive)

            # run_chrome(1,["python3", "/Users/interface/Desktop/amazon/youtube.py"])


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(hello())
阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进