2016-12-06 34 views
0

调用函数我有twisted.internet.reactor问题,我所有的客户有完全一致的环境中,但只有一些经验,这个问题:扭曲反应堆无法从线程正确

他们正确地通过ws和交流第一几条消息connectTCP到服务器。在约一分钟,他们应该通过

def execute(self, message, callback=None): 
    print(">>>", message, flush=True) 
    reactor.callFromThread(self._client_protocol_instance.send, message, callback) 

self._client_protocol_instance.send方法将消息发送到服务器的定义如下:

def send(self, command, callback): 
    print("send", command, callback, flush=True) 
    timestamp = int(time() * 1000000) 
    msg = (command.strip() + " --timestamp:" + str(timestamp)) 
    if _self._debug: 
     _self._commands[str(timestamp)] = msg 
    if callback is not None: 
     _self._callbacks[str(timestamp)] = callback 
    payload = msg.encode() 
    _self._status_controller.set_state(payload) 
    self.sendMessage(payload) 

首先print显示了在标准输出,但第二个没有。我假设send没有得到执行。在reactor.run()之后,这是整个程序中唯一参考reactor

服务器立即检测到发生这种情况后终止客户端的过程,因此当时连接仍然存在。

这可能是什么原因造成的?

回答

0

我找到了解决方案,问题在于以前的任务有时不会完成,因为它试图发送消息。

我解决了这个问题,将所有cpu-heavy响应处理逻辑移入线程以释放其他消息的反应器。