【发布时间】:2014-04-28 15:01:49
【问题描述】:
我开始学习 Twisted 并尝试完成我的第一个任务。我有下一个服务器代码:
class TextProtocol(Protocol):
def connectionMade(self):
text = self.factory.text
self.transport.write(text)
self.transport.loseConnection()
class TextServerFactory(ServerFactory):
protocol = TextProtocol
def __init__(self, text):
self.text = text
from twisted.internet import reactor
reactor.listenTCP(4444, TextServerFactory("Twisted"))
reactor.run()
此代码立即向客户端发送文本。但实际上我想通过信件发送数据:如果我们有很多客户端,服务器应该逐个信件发送。我认为 SERVER 日志应该是这样的:
Client #1 connected
Sending "T" to client #1
Sending "w" to client #1
Client #2 connected
Sending "T" to client #2
Sending "i" to client #1
Sending "w" to client #2
Sending "s" to client #1
Sending "i" to client #2
.....
如果我将在协议中创建循环,它将阻塞操作。 你能帮我解决这个问题吗?
【问题讨论】:
标签: python asynchronous twisted