Python websocket客户端发送入运行循环
我正在使用管道Python websocket客户端发送入运行循环,python,multithreading,websocket,Python,Multithreading,Websocket,我正在使用管道stdin连接到websocket。我正在连接到websocket,如下所示: ws = WebSocketApp('ws://example.com/websocket') ws.run_forever(ping_interval=15) while True: try: line = sys.stdin.readline() ws.send(line) except KeyboardInterrupt: brea
stdin
连接到websocket。我正在连接到websocket,如下所示:
ws = WebSocketApp('ws://example.com/websocket')
ws.run_forever(ping_interval=15)
while True:
try:
line = sys.stdin.readline()
ws.send(line)
except KeyboardInterrupt:
break
if not line:
break
永远运行
启动无限循环。我想同时从sys.stdin
读取如下内容:
ws = WebSocketApp('ws://example.com/websocket')
ws.run_forever(ping_interval=15)
while True:
try:
line = sys.stdin.readline()
ws.send(line)
except KeyboardInterrupt:
break
if not line:
break
有没有办法在两个循环之间交替使用线程?还是我走错了路
这可能是通过Tornado或Twisted实现的,但如果有办法通过标准库实现,我宁愿将依赖项保持在最小值。当您输入某个内容时会发生什么?当前,
ws.run\u forever()
是运行的最后一行。它永远不会到达第二个循环WebSocketApp
和create\u connection
之间有什么区别?根据文档中的示例,要中断,如果不是line.strip(),您可能需要添加,但如果希望代码结束,则可能需要ws.close()