python - 简单的 Twisted 服务器不会使用计时器写入

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

最佳答案

我自己解决了。

来自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/

相关文章:

python - 返回扭曲的延迟后模拟无效

python - 根据列值将 2 列分组为类别

python - Unicode解码错误: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)

python - 使用逻辑表达式加速应用

android - 倒计时器跳过几毫秒

swift - 将所选时间与当前时间进行比较

java - 如何在swing中调用一段时间内的方法?

python - 关闭与扭曲的连接

python - 如何正确管理 connectionLost in twisted

python - 添加约束 CPLEX Python API