我使用的是python的扭曲框架,在创建了客户端和服务器端之后,它可以工作。但是消息是从另一个实例产生的,我如何通过调用其实例来编写协议类之外的发送消息块
对于使用扭曲的客户端:
from twisted.internet import reactor, protocol
class clientProtocol(protocol.Protocol):
def __init__(self):
pass
def send(self, data):
self.transport.write(data.encode('utf-8'))
xxxxxx other codes XXXXXXX
class clientFactory(protocal.ClientFactory):
def buildProtocol(self, addr):
retrn clientProtocol()
reactor.connectTCP('0.0.0.0',1234, clientFactory())
reactor.run()
现在我还有另一个将生成发送消息的类:
class myclass:
def __init__(self, clientProtocol_instance):
self.clientProtocol_instance = clientProtocol_instance
#and the reactor has running otherwhere
def continuous_produding_message:
for i in range(1000000):
if i %5 == 0:
**self.clientProtocol_instance.send(i)**
def onReceived(self):
** if clientProtocol_instance.dataRecieved, callback at here**
我想要myClass作为主要入口(或线程),而不是react.run(), 就像在第一次调用Reactor.run()时一样,我觉得下面的其他代码被阻止了,
if __name__ == '__main__':
reactor.connectTCP('0.0.0.0',1234, clientFactory())
reactor.run()
myclass(protocol_instance_above) #will never called
我该如何设计代码