<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><blockquote type="cite"><div>Indeed, a Deferred should help. &nbsp;You can have one which is fired with the<br>SSHConnection once it is available. &nbsp;Then, callbacks on this Deferred will<br>be able to start any commands they want. &nbsp;One way to do this would be to<br>pass a Deferred all the way down into the SSHConnection and fire it with<br>`self´ in `serviceStarted´. &nbsp;This basically turns control flow inside-out,<br>allowing arbitrary code which is not part of the SSHConnection class to use<br>the connection.</div></blockquote><br></div><div>Hi Jean-Paul,</div><div>Thanks for that advice - as you suggested I'm now passing a Deferred down into the SSHConnection to receive a reference to the connection object, which works really well.</div><div><br></div><div>I now have a follow-on question. This is my situation: I'd like to set up an SSHConnection and associate it with a Machine object at program startup, then execute arbitrary commands over this SSHConnection at run-time.</div><div><br></div><div>The problem I'm facing is that after instantiating the Twisted objects, calling reactor.run() doesn't return, so I can't create a standalone SSHConnection and attach it as a utility to a Machine object.</div><div><br></div><div>For example:</div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span style="color: #0000ff">class</span>&nbsp;Machine(object):</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; <span style="color: #0000ff">def</span> __init__(self, address):</div><span class="Apple-style-span" style="font-family: Monaco; font-size: 11px; ">&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;self.conn = MachineConnection(self, address,&nbsp;<span style="color: rgb(0, 170, 0); ">'user'</span>)</span><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span style="color: #0000ff">class</span> MachineConnection(object):</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; <span style="color: #0000ff">def</span> __init__(self, address, user):</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; self.address = address</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; self.port = <span style="color: #800000">22</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; self.user = user</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; d=defer.Deferred()</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; d.addCallback(self.__conn_ready)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; d.addErrback(self.__conn_err)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; factory =&nbsp;protocol.ClientFactory(self.user, d) # I'm actually using a subclass</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; reactor.connectTCP(self.address, self.port, factory)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; reactor.run()<br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="font-size: 12px;">Here, I've left out definitions of my conch subclasses, and methods on Machine and MachineConnection, but I don't think they're relevant.</span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="font-size: 12px;"><br></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="font-size: 12px;">The issue is that reactor.run() in the last line of MachineConnection.__init__ starts Twisted up, creates the connection and so on, but doesn't return control to the original flow of code. I've tried starting separate threads for the connections, but Twisted seems to rely on running in the main thread.</span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="font-size: 12px;"><br></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="font-size: 12px;">Are the any suggestions on how to set up backgrounded Twisted connections that can be interacted with as daemonic threads?</span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="font-size: 12px;"><br></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="font-size: 12px;">Thank you,</span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="font-size: 12px;">James</span></font></div></p></div></body></html>