我刚刚学习 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()
最佳答案
我自己解决了。
来自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.
无论如何,谢谢大家!
关于python - 简单的 Twisted 服务器不会使用计时器写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3411511/