from twisted.internet import protocol, reactor from twisted.protocols import basic import threading, time class TestProto(protocol.Protocol): def connectionMade(self): print 'Connected' self.transport.write('Connection test!\r\n') def dataReceived(self, data): print data, def callIn(self): print 'callIn' self.transport.write('callInThread test!\r\n') class TestFactory(protocol.ClientFactory): protocol = TestProto def __init__(self): self.test = TestFactory.protocol() def buildProtocol(self, addr = None): return self.test def main(): reactor.run(installSignalHandlers=0) factory = TestFactory() reactor.connectTCP('localhost', 2005, factory) th1 = threading.Thread(target = main) th1.setDaemon(True) th1.start() while not factory.test.connected: time.sleep(0.1) reactor.callFromThread(factory.test.callIn) print 'done' reactor.stop() time.sleep(1)