如何在 Twisted 中的reactor.connectTCP之后关闭连接
我想问一个关于如何关闭twisted RPC
中的连接的问题。
我知道有人问过类似的问题,但它似乎没有回答我的问题。
我正在做一些基本的连接,如下所示:
cfactory = pb.PBClientFactory()
reactor.connectTCP(<host>, <port>, cfactory)
dfr.addCallbacks(<callback>, <errfun>, ...)
...
(in the <callback> func) remote.callRemote('myfunc', ...)
它一切正常并且可以完成我需要的工作。
但问题是,如果我检查它,我会看到连接仍然处于活动状态(“已建立”) netstat -a
。
由于我在无限期运行的客户端和服务器之间执行此操作,因此我不能继续累积活动连接。
出于同样的原因我也无法停止反应堆。
那么,有没有一种方法可以关闭连接,而不需要创建自己的协议?
我想首先检查一下,因为除了这个事实之外,一切都处于工作状态 - 如果可能的话,我将只添加一个需要的东西,而不是从协议设置和所有开始。
感谢您的关注,如有任何一般性建议,我们将不胜感激。
托尼
I wanted to ask a question on how to close the connection in twisted RPC
.
I know a similar question was asked but it doesn't seem to answer mine.
I am doing some basic connection as sketched below:
cfactory = pb.PBClientFactory()
reactor.connectTCP(<host>, <port>, cfactory)
dfr.addCallbacks(<callback>, <errfun>, ...)
...
(in the <callback> func) remote.callRemote('myfunc', ...)
It all works and does the stuff I need.
But the trouble is that I see the connection still active ("ESTABLISHED") if I check it bynetstat -a
.
Since I'm doing this between a client and a server that run indefinitely, I cannot just keep accumulating the active connections.
I can't stop the reactor either for the same reason.
So, is there a way to close the connection, short of going through creating one's own protocol?
I wanted to check first since it is all in working order except this one fact - If possible I'll just add the one needed thing rather than starting with protocol setup and all.
Thanks for your attention and any general advice would be appreciated.
Tony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
remote
是一个RemoteReference
。它有一个 Broker 属性,该属性是创建它的twisted.spread.pb.Broker 协议实例。与几乎所有协议一样,Broker
实例具有一个transport
属性,该属性引用表示协议正在运行的连接的对象。因此,
remote.broker.transport.loseConnection()
应该执行您想要的操作。还有其他选择。您可以在工厂捕获
Broker
实例:现在您在工厂上有一个
proto
属性(但只有在实际建立连接之后,并且没有任何东西可以清理它,所以连接丢失后它仍然存在 - 但你可以处理这个)。remote
is aRemoteReference
. It has abroker
attribute which is thetwisted.spread.pb.Broker
protocol instance that created it. Like almost all protocols, theBroker
instance has atransport
attribute which refers to the object representing the connection the protocol is running over.Therefore,
remote.broker.transport.loseConnection()
should do what you want.There are other options, too. You could capture the
Broker
instance at the factory:Now you have a
proto
attribute on the factory (but only after the connection actually gets made, and nothing will clean it up so it will still be there after the connection is lost - but you could take care of that).