web-dev-qa-db-ja.com

スレッド化された非ブロッキングWebSocketクライアント

Python)でプログラムを実行し、Webソケットを介して毎秒メッセージをTornadoサーバーに送信したいと思っています。websocket-clientで例を使用しています。

ws.run_forever()は、whileループの実行を停止するため、この例は機能しません。

誰かがこれをスレッド化されたクラスとして正しく実装する方法の例を教えてもらえますか?

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)
13
Chris

彼らの github page に例があり、それはまさにそれを行います。あなたはその例から始めて、on_openから毎秒メッセージを送信するコードを取り、run_forever呼び出し、そのBTWはソケットが切断されるまで実行されます。

多分あなたはここで基本的な概念に問題を抱えています。ソケットのリスニング専用のスレッドが常に存在します(この場合、run_foreverメッセージを待っている間にループに入るメインスレッド)。他に何かしたい場合は、別のスレッドが必要になります。

以下は、コード例の異なるバージョンです。メインスレッドを「ソケットリスナー」として使用する代わりに、別のスレッドが作成され、run_foreverが実行されますそこ。 on_openコールバックを使用できる一方でソケットが接続されていることを確認するコードを記述する必要があるため、これはもう少し複雑だと思いますが、おそらくそれはあなたが理解するのに役立ちます。

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
16