All pastes #39129 Raw Edit

Untitled

public text v1 · immutable
#39129 ·published 2006-01-30 15:29 UTC
rendered paste body
from twisted.python import threadable
threadable.init()

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.callInThread(factory.test.callIn)
print 'done'

time.sleep(1)