2024-06-16 04:34:51 发布
网友
我正在使用“ws.run_forever”启动Python Websocket,另一个source声明我应该使用“run_until_complete()”,但这些函数似乎只对Python asyncio可用。
如何阻止websocket客户端?或者如何开始它而不是永远运行。
在python websockets中,可以使用“ws.keep_running=False”来停止“永远运行”websocket。
这可能有点不直观,您可以选择另一个可能整体工作得更好的库。
下面的代码对我有效(使用ws.keep_running=False)。
class testingThread(threading.Thread): def __init__(self,threadID): threading.Thread.__init__(self) self.threadID = threadID def run(self): print str(self.threadID) + " Starting thread" self.ws = websocket.WebSocketApp("ws://localhost/ws", on_error = self.on_error, on_close = self.on_close, on_message=self.on_message,on_open=self.on_open) self.ws.keep_running = True self.wst = threading.Thread(target=self.ws.run_forever) self.wst.daemon = True self.wst.start() running = True; testNr = 0; time.sleep(0.1) while running: testNr = testNr+1; time.sleep(1.0) self.ws.send(str(self.threadID)+" Test: "+str(testNr)+") self.ws.keep_running = False; print str(self.threadID) + " Exiting thread"
在python websockets中,可以使用“ws.keep_running=False”来停止“永远运行”websocket。
这可能有点不直观,您可以选择另一个可能整体工作得更好的库。
下面的代码对我有效(使用ws.keep_running=False)。
相关问题 更多 >
编程相关推荐