Simple Twisted服务器不会使用Timer写入

时间:2010-08-05 03:20:22

标签: python timer twisted

我只是在学习Python和Twisted,我无法弄清楚为什么这个简单的服务器无法工作。从计时器调用时,self.transport.write不起作用。我一点也没有错。任何帮助赞赏。非常感谢你!

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 个答案:

答案 0 :(得分:2)

我自己想出来了。

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

  

Twisted中的大多数代码都不是线程安全的。例如,从协议向传输写入数据不是线程安全的。

非常感谢大家!