views:

27

answers:

1

I'm just learning Python and Twisted and I can't figure out for the life of me why this simple server won't work. The self.transport.write doesn't work when called from a timer. I get no error at all. Any help appreciated. Thank you very much!

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
from threading import Timer

class proto(Protocol):

    def saySomething(self):
        self.transport.write('hello there\r\n')

    def connectionMade(self):
        Timer(5, self.saySomething).start()

class theFactory(Factory):

    protocol = proto

reactor.listenTCP(8007, theFactory())
reactor.run()
+1  A: 

I figured it out myself.

From http://twistedmatrix.com/documents/current/core/howto/threading.html:

Most code in Twisted is not thread-safe. For example, writing data to a transport from a protocol is not thread-safe.

Thanks anyways folks!

PythonNewb