Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python websocket客户端发送入运行循环_Python_Multithreading_Websocket - Fatal编程技术网

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()