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.
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.
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?
from threading import Thread
from twisted.internet import reactor
from twisted.internet import protocol
PORT = 9999
class AgentManager(Thread):
def run(self):
print '\nstarting agentmanager', self
reactor.connectTCP('localhost', PORT, AgentClientFactory())
reactor.listenTCP(PORT, AgentServerFactory())
reactor.callLater(5, reactor.stop)
reactor.run(installSignalHandlers=0)
class Producer(protocol.Protocol):
def connectionMade(self):
print 'connection made'
self.transport.write('Hello')
def connectionLost(self, reason):
print 'connection lost'
class Consumer(protocol.Protocol):
def dataReceived(self, data):
print data
class AgentServerFactory(protocol.ServerFactory):
protocol = Producer
class AgentClientFactory(protocol.ClientFactory):
protocol = Consumer
if __name__ == '__main__':
for i in range(3):
mgr = AgentManager()
mgr.start()
mgr.join()
_______________________________________________
Twisted-Python mailing list
[email protected]
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python