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

查看:636
本文介绍了线程化,非阻塞的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天全站免登陆