What&#39;s the larger problem you&#39;re trying to solve?<div><br></div><div>Jason<br><br><div class="gmail_quote">On Thu, Feb 17, 2011 at 4:38 PM,  <span dir="ltr">&lt;<a href="mailto:Andy.Henshaw@gtri.gatech.edu" target="_blank">Andy.Henshaw@gtri.gatech.edu</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div><p class="MsoNormal">I apologize in advance – another question about Twisted and threading.  I just seem to have trouble with this and I’m hoping my example is simple enough that a good explanation will be forthcoming.</p>

<p class="MsoNormal"> </p><p class="MsoNormal">I’m trying to start and stop Twisted in its own thread, over and over.  I’ve seen some notes that say this is a bad idea and that programs that do so should be restructured.  But, it would really work better for this application if I could do it somehow.</p>

<p class="MsoNormal"> </p><p class="MsoNormal">My controlling thread just wants to start Twisted, let it do its thing, and then let the thread stop.  Later on, I’d like to do it again.  In the example code below, the thread runs and stops fine in the first iteration.  In the second iteration, it starts, seems to run okay, but it never stops.  Is this an unreasonable request?</p>

<p class="MsoNormal"> </p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">from threading import Thread</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">from twisted.internet import reactor</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">from twisted.internet import protocol</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;"> </span></p><p class="MsoNormal">

<span style="font-family:&quot;Courier New&quot;">PORT = 9999</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;"> </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">class AgentManager(Thread):</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">    def run(self):</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        print &#39;\nstarting agentmanager&#39;, self</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        reactor.connectTCP(&#39;localhost&#39;, PORT, AgentClientFactory())</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        reactor.listenTCP(PORT, AgentServerFactory())</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        </span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        reactor.callLater(5, reactor.stop)</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        reactor.run(installSignalHandlers=0)</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">                        </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;"> </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">class Producer(protocol.Protocol):</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">    def connectionMade(self):</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        print &#39;connection made&#39;</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        self.transport.write(&#39;Hello&#39;)</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        </span></p><p class="MsoNormal">

<span style="font-family:&quot;Courier New&quot;">    def connectionLost(self, reason):</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        print &#39;connection lost&#39;</span></p><p class="MsoNormal">

<span style="font-family:&quot;Courier New&quot;"> </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;"> </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">class Consumer(protocol.Protocol):</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">    def dataReceived(self, data):</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        print data</span></p><p class="MsoNormal">

<span style="font-family:&quot;Courier New&quot;"> </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">class AgentServerFactory(protocol.ServerFactory):</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">    protocol = Producer</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">class AgentClientFactory(protocol.ClientFactory):</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">    protocol = Consumer</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;"> </span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;"> </span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">if __name__ == &#39;__main__&#39;:</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">    for i in range(3):</span></p>
<p class="MsoNormal">
<span style="font-family:&quot;Courier New&quot;">        mgr = AgentManager()</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        mgr.start()</span></p><p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">        mgr.join()</span></p>

</div></div><br>_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>Jason Rennie<br>Research Scientist, ITA Software<br>617-714-2645<br><a href="http://www.itasoftware.com/" target="_blank">http://www.itasoftware.com/</a><br><br>
</div>