python - 高速公路,离开 onMessage block

标签 python websocket autobahn

我有一个带有典型 onX 的高速公路 Websocket 服务器在它的协议(protocol)中起作用。我的问题是我找不到退出的方法 onX ,同时在特定消息到达时继续做我想做的各种事情。更具体地说在我的 onMessage函数,我有时会向非常慢的 API 执行 HTTP 请求。结果,发送 websocket 消息的客户端被服务器的 onMessage 阻止了。定稿。即使我做 self.sendMessagereactor.callFromThread(<http request here>) , 或 self.transport.loseConnection()从服务器端,在 onMessage block ,onMessage仍在执行 HTTP 请求,我的客户端正在等待。

这是我客户的代码:

@asyncio.coroutine
def send(command,userPath,token):
    websocket = yield from websockets.connect('wss://127.0.0.1:7000',ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLSv1_2))
    data = json.dumps({"api_command":"session","body":command,"headers": {'X-User-Path': userPath, 'X-User-Token': token}})
    response = {}
    try:
        yield from websocket.send(data)
    finally:
        yield from websocket.close()
    if 'command' in response:
        if response['command'] == 'ACK_SESSION_COMMAND' or response['command'] == 'ACK_INITIALIZATION':
            return ('OK',200)
        else:
            return('',400)

我什至试过 websocket.send(data) ,来自客户端,但由于某种原因它没有发送数据(我没有看到它们到达服务器)。我不明白如何从 onMessage 返回阻止并继续执行我的 HTTP 请求。

为了解释我的情况,我只想向我的服务器发送 1 条 ssl websocket 消息并立即关闭连接。任何可以做到这一点的东西都适合我。

最佳答案

使用 reactor.callInThread 而不是 reactor.callFromThread 会导致应用程序流释放并且 HTTP 请求在线程中独立执行。如扭曲文档中所述:http://twistedmatrix.com/documents/13.2.0/core/howto/threading.html

关于python - 高速公路,离开 onMessage block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46115586/

相关文章:

python - PySide(或 PyQt)信号和槽基础知识

java - 如何声明我的 Java WebSocket 端点来使用和生成 JSON?

python - 在linux上运行autobahn python会出现异常。AttributeError : Router instance has no attribute 'broker'

python - 在Python中: how to write a sparse matrix to a csv file?

python - 如何使用 iterrows() 创建新的数据框?

python - 在 python 中创建成对元素列表

python - Tornadio2 + Socket.io 负载测试

node.js - Nodejs 中的 Wav 到 Blob

Python 高速公路 WAMP 消息传递协议(protocol)

javascript - Autobahn JS 订阅管理