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

发布于 2024-09-12 13:33:09 字数 541 浏览 2 评论 0原文

我刚刚学习 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()

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2024-09-19 13:33:09

我自己想出来了。

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

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

无论如何,谢谢大家!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文