issue with twisted and reactor. Can't stop reactor

G

Gabriel

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()

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()

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:

..../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.

Any idea why this is happening?
King regards.
 
T

Tim Harig

Subject: issue with twisted and reactor. Can't stop reactor

Not having written anything using twisted I cannot help you much with your
code; but, I cannot resist commenting about your subject line:

I suspect that if are having an issue with your reactor after being hit by
a twister[d], then you probably have more at issue then your networking.
My suggestion would be to run or drive away quickly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top