You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Twisted Reactor在树莓派上无法启动任务的问题求助

Twisted Reactor在树莓派上无法启动任务的问题求助

大家好,我遇到一个非常棘手的问题:下面这段基于Twisted和Autobahn的WebSocket客户端代码,在我的Windows电脑上运行一切正常,但部署到树莓派上后就直接卡住,完全无法建立连接、收发消息。

我的代码如下:

from twisted.internet import reactor, ssl, threads
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol
import queue
from queue import Empty
import threading
import time

connection_signal = threading.Event()


class MyWebSocketClientProtocol(WebSocketClientProtocol):
    def onOpen(self):
        print("WebSocket connection opened.")
        self.factory.client_protocol = self

    def onConnect(self, response):
        print(f"Server connected: {response.peer}")
        connection_signal.set()

    def onClose(self, wasClean, code, reason):
        print(f"WebSocket connection closed: {reason}")

    def onMessage(self, payload, isBinary):
        print(f"Message received: {payload}")


class WebSocketManager:
    def __init__(self, queue):
        self.queue = queue
        self.client_protocol = None

    def connect(self, host, port):
        self.factory = WebSocketClientFactory(f"wss://{host}:{port}")
        self.factory.protocol = MyWebSocketClientProtocol
        context_factory = ssl.ClientContextFactory()
        reactor.connectSSL(host, port, self.factory, context_factory)
        connection_signal.wait()

    def disconnect(self):
        self.factory.client_protocol.sendCloseFrame()

    def send_message(self, message):
        self.factory.client_protocol.sendMessage(message.encode("utf-8"))


def reactor_thread(queue):
    manager = WebSocketManager(queue)

    def process_queue():
        while True:
            try:
                msg = queue.get(timeout=0.1)
                if msg:
                    if msg == "connect":
                        manager.connect("192.168.2.98", 8000)
                        time.sleep(0.1)  # allow the connection to be established
                    elif msg == "disconnect":
                        manager.disconnect()
                    else:
                        manager.send_message(msg)
            except Empty:
                pass

    reactor.callInThread(process_queue)
    reactor.run(installSignalHandlers=False)


def main():
    message_queue = queue.Queue()
    reactor_thread_instance = threading.Thread(
        target=reactor_thread, args=(message_queue,)
    )
    reactor_thread_instance.start()

    message_queue.put("connect")
    message_queue.put("Hello WebSocket!")
    message_queue.put("disconnect")
    time.sleep(5)
    # reactor.stop()
    # reactor_thread_instance.join()


if __name__ == "__main__":
    main()

我已经做了以下排查,但还是找不到问题所在:

  • 对比了Reactor实现:Windows上默认用的是select reactor,树莓派默认是epoll reactor,但我尝试在树莓派上强制用select、在Windows上强制用epoll,问题依旧存在
  • 调试发现关键差异:树莓派上MyWebSocketClientProtocol类的__init__方法完全没被调用,而Windows上这个方法会正常触发(说明连协议实例都没创建出来)
  • 确认了环境一致性:两边的虚拟环境依赖版本完全相同,Python版本也都是3.12,具体依赖:
    • autobahn == 23.6.2
    • twisted == 23.10.0
    • pyopenssl == 24.2.1

有没有朋友遇到过类似的问题?或者能帮我分析下可能的原因?

备注:内容来源于stack exchange,提问作者Spoutnik16

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 16:03:08