小编Clé*_*ide的帖子

Python 3 Threaded websockets服务器

我正在python 3中构建一个Websocket服务器应用程序.我正在使用这个实现:https://websockets.readthedocs.io/

基本上我想管理多个客户端.另外我想从2个不同的线程发送数据(一个用于GPS +一个用于IMU)GPS线程刷新1Hz,而IMU线程刷新在25Hz

我的问题是在MSGWorker.sendData方法:只要我去掉从websocket.send行@ asyncio.coroutine和产量("{'GPS’:'%S’}"%数据)的整个方法不执行任何操作(没有打印(终端中的"发送数据:foo")

然而,这两行评论我的代码按照我的预期工作,除了我通过websocket发送任何内容.

但是,当然,我的目标是通过websocket发送数据,我只是不明白为什么它不起作用?任何的想法 ?

server.py

#!/usr/bin/env python3
import signal, sys
sys.path.append('.')
import time
import websockets
import asyncio
import threading

connected = set()
stopFlag = False



class GPSWorker (threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    self.data = 0
    self.lastData = 0
    self.inc = 0

  # Simulate GPS data
  def run(self):
    while not stopFlag:
      self.data = self.inc
      self.inc += 1
      time.sleep(1)

  def get(self):
    if self.lastData is not self.data:
      self.lastData = self.data
      return self.data



class IMUWorker (threading.Thread):
  def …
Run Code Online (Sandbox Code Playgroud)

python multithreading websocket

3
推荐指数
1
解决办法
4062
查看次数

标签 统计

multithreading ×1

python ×1

websocket ×1