issue with twisted and reactor. Can't stop reactor

Gabriel gabriel at opensuse.org
Mon May 11 16:40:57 EDT 2009


Hello all!,

I'm trying to implement a simple one way communication using twisted.

Sender:
> send message
> close connection

Receiver:
> receive
> do something
> wait for other message

I'm testing with this simple examples:
Sender:
[code]
class SenderClient(protocol.Protocol):

    def __init__(self, data):
        self.data = data

    def connectionMade(self):
        self.transport.write(self.data)

    def dataReceived(self, data):
        self.transport.loseConnection()

    def connectionLost(self, reason):
        pass

class SenderFactory(protocol.ClientFactory):

    def __init__(self, data):
        self.data = data

    def buildProtocol(self, addr):
        return SenderClient(self.data)

    def clientConnectionFailed(self, connector, reason):
        logger.error("Connection failed. Reason %s" % reason)
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        reactor.stop()

class Sender(object):

    def __init__(self, host='localhost', port=61610):
        self.host = host
        self.port = port

    def send(self, data):
        reactor.connectTCP(self.host, self.port, SenderFactory(data))
        reactor.run()
[/code]

Receiver:
[code]
from twisted.internet import reactor, protocol

class Receiver(protocol.Protocol):

    def dataReceived(self, data):
        self.transport.write("success")
        print data

def main():
    factory = protocol.ServerFactory()
    factory.protocol = Receiver
    reactor.listenTCP(61610,factory)
    reactor.run()

if __name__ == '__main__':
    main()
[/code]

When I call send the first time it works fine, when I call send a
second time the sender hangs.
If I hit ctrl+c it throws:

[quote]
.../twisted/internet/base.py", line 527, in stop
    "Can't stop reactor that isn't running.")
twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running.
[/quote]

Any idea why this is happening?
King regards.



More information about the Python-list mailing list