简单的 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()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己想出来了。
来自 http://twistedmatrix.com/documents/current/core/howto/threading .html:
无论如何,谢谢大家!
I figured it out myself.
From http://twistedmatrix.com/documents/current/core/howto/threading.html:
Thanks anyways folks!