Python中文
首页
教程
问答
标签
搜索
登录
注册
我应该如何处理中的重新连接twisted.application.internet.客户端服务?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试图在twisted应用程序中使用<a href="http://labs.twistedmatrix.com/2016_04_01_archive.html" rel="nofollow">recently introduced </a><a href="https://twistedmatrix.com/documents/current/api/twisted.application.internet.ClientService.html" rel="nofollow">^{<cd1>}</a>类,该应用程序使用<code>pymodbus</code>进行简单的modbus tcp轮询。我觉得我的问题与我正在使用的modbus协议没有任何关系,因为我已经使用较低级别的twisted API创建了许多其他工作原型;但是这个新的<code>ClientService</code>看起来正好符合我的需要,因此如果我能让它工作的话,它应该会减少我的代码占用并保持整洁。在</p> <p>我的测试显示<code>ClientService</code>处理重新连接的方式与预期的一样,而且我可以轻松访问第一个连接<code>Protocol</code>。我遇到的问题是为重新连接获取后续的<code>Protocol</code>对象。以下是我遇到问题的代码的简化版本:</p> <pre><code>from twisted.application import internet, service from twisted.internet.protocol import ClientFactory from twisted.internet import reactor, endpoints from pymodbus.client.async import ModbusClientProtocol class ModbusPollingService(internet.ClientService): def __init__(self, addrstr, numregs=5): self.numregs=numregs internet.ClientService.__init__(self, endpoints.clientFromString(reactor, addrstr), ClientFactory.forProtocol(ModbusClientProtocol)) def startService(self): internet.ClientService.startService(self) self._pollWhenConnected() def _pollWhenConnected(self): d = self.whenConnected() d.addCallback(self._connected) d.addErrback(self._connfail) def _connected(self, p): self._log.debug("connected: {p}", p=p) self._mbp = p self._poll() return True def _connfail(self, failstat): self._log.failure('connection failure', failure=failstat) self._mbp = None self._pollWhenConnected() def _poll(self): self._log.debug("poll: {n}", n=self.numregs) d = self._mbp.read_holding_registers(0, self.numregs) d.addCallback(self._regs) d.addErrback(self._connfail) def _regs(self, res): self._log.debug("regs: {r}", r=res.registers) # Do real work of dealing storing registers here reactor.callLater(1, self._poll) return res application = service.Application("ModBus Polling Test") mbpollsvc = ModbusPollingService('tcp:127.0.0.1:502') mbpollsvc.setServiceParent(application) </code></pre> <p>当连接失败(无论什么原因),从<code>read_holding_registers()</code>返回的<code>deferred</code>的<code>errback</code>将被调用,目的是我的服务可以放弃<code>Protocol</code>,并返回到等待<code>whenConnected()</code>回调返回的新连接<code>Protocol</code>的状态。。。然而,似乎正在发生的是,ClientService还没有意识到连接已断开,并返回相同的断开协议,给我一个日志,其中包含:</p> ^{pr2}$ <p>或者非常相似,请注意重复的ModbusClientProtocol对象地址。在</p> <p>我很确定我可能只是为这个API选择了一个糟糕的模式,但是我已经迭代了一些不同的可能性,比如基于<code>ModbusClientProtocol</code>创建我自己的<code>Protocol</code>和{<cd14>},并完全在该类中处理轮询机制;但是传递持久配置和机制来存储有点混乱通过这种方式,在ClientService级别或更高级别处理这些数据似乎是一种更干净的方法,但我无法找到跟踪当前连接的协议的最佳方法。我想我真正想要的是一个关于在扩展轮询情况下使用<code>ClientService</code>类的最佳实践建议。在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>你没打电话来自传输丢失连接()根据你的投票结果,我能看到的任何地方,就twisted所知,你实际上并没有断开连接。可能以后,当你停止在旧的交通工具上做任何事情时,但到那时你已经失去了对事物的了解。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在乒乓球比赛中预测球的轨迹,对于AI球拍预测?
5 回答
如何在乒乓球游戏中阻止球
10 回答
如何在乘法和模中不乘空间?
1 回答
如何在乘法和除以2个不同的数字之间进行交换?
2 回答
如何在也是数据一部分的单个字符上拆分大字符串
8 回答
如何在乾草堆中找到針,有更好的解決方案嗎?
4 回答
如何在事件wxWidgets中传递自定义数据
6 回答
如何在事件中使用lambda i=i?
3 回答
如何在事件中心只接收最近的数据
8 回答
如何在事件发生之前保持云函数运行?
2 回答
如何在事件发生后使页面重定向到同一页面
8 回答
如何在事件回调之间保持python生成器的状态
7 回答
如何在事件处理程序(pythonsocket、sphinx)中保留docstring
6 回答
如何在事件处理程序中更改wxRichTextCtrl的光标位置?
7 回答
如何在事件处理程序中访问外部对象?
3 回答
如何在事件循环中将协程打包为正常函数?
8 回答
如何在事件循环之外运行协同程序?
5 回答
如何在事件循环结束时为并发未来的所有线程调用类方法?
8 回答
如何在事件文件中只保留一份摘要?
1 回答
如何在事件模板中添加事件
6 回答