线程化的、非阻塞的 websocket 客户端 [英] Threaded, non-blocking websocket client

查看:28
本文介绍了线程化的、非阻塞的 websocket 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Python 中运行一个程序,该程序每秒通过 Web 套接字向 Tornado 服务器发送一条消息.我一直在使用 websocket-client 上的例子;

I am wanting to run a program in Python that sends a message every second via web sockets to a Tornado server. I have been using the example on websocket-client;

这个例子不起作用,因为ws.run_forever()会停止while循环的执行.

This example does not work, because ws.run_forever() will stop the execution of the while loop.

有人能给我一个例子,说明如何将它正确地实现为一个线程类,我既可以调用它的 send 方法,也可以接收消息吗?

Can somebody give me an example of how to correctly implement this as a threaded class which I can both call the send method of, but also receive messages?

import websocket
import thread
import time

def on_message(ws, message):
    print message

def on_error(ws, error):
    print error

def on_close(ws):
    print "### closed ###"

def on_open(ws):
    pass

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

    while True:
        #do other actions here... collect data etc.
        for i in range(100):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)

推荐答案

在他们的 github 页面中有一个例子 就是这样做的.似乎您从该示例开始,并从 on_open 中取出每秒发送消息的代码,并在 run_forever 调用之后粘贴它,BTW 一直运行到套接字已断开连接.

There's an example in their github page that does exactly that. It seems like you started out of that example and took the code that sends messages every second out of the on_open and pasted it after the run_forever call, that BTW runs until the socket is disconnected.

也许您对这里的基本概念有疑问.总会有一个线程专用于侦听套接字(在这种情况下,主线程进入 run_forever 内的循环等待消息).如果您想处理其他事情,则需要另一个线程.

Maybe you are having issues with the basic concepts here. There's always going to be a thread dedicated to listening to the socket (in this case the main thread that enters a loop inside the run_forever waiting for messages). If you want to have some other thing going on you'll need another thread.

下面是他们示例代码的不同版本,其中没有使用主线程作为套接字侦听器",而是创建了另一个线程,并且 run_forever 在那里运行.我认为它有点复杂,因为您必须编写代码以确保套接字已连接,同时您可以使用 on_open 回调,但也许它会帮助您理解.

Below is a different version of their example code, where instead of using the main thread as the "socket listener", another thread is created and the run_forever runs there. I see it as a bit more complicated since you have to write code to assure the socket has connected while you could use the on_open callback, but maybe it will help you understand.

import websocket
import threading
from time import sleep

def on_message(ws, message):
    print message

def on_close(ws):
    print "### closed ###"

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_close = on_close)
    wst = threading.Thread(target=ws.run_forever)
    wst.daemon = True
    wst.start()

    conn_timeout = 5
    while not ws.sock.connected and conn_timeout:
        sleep(1)
        conn_timeout -= 1

    msg_counter = 0
    while ws.sock.connected:
        ws.send('Hello world %d'%msg_counter)
        sleep(1)
        msg_counter += 1

这篇关于线程化的、非阻塞的 websocket 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆