开发者

Simple Twisted server won't write with Timer

开发者 https://www.devze.com 2023-01-10 12:37 出处:网络
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

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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号