python 每隔1秒循环发10条数据 为什么是10条一起发出去的?
python 每隔1秒循环发10条数据 为什么是10条一起发送的?
我用的是twisted的框架, 我想没隔一秒就发送一条出去 而不是等10条一起发送,怎么解决?
这个就有点像nagle算法了 但是python中不知道怎么关闭
[解决办法]
#
import time
from twisted.internet import protocol, reactor, defer,threads
class Echo(protocol.Protocol):
def connectionMade(self):
#1. writeSomeData
def writeSomeData():
i=10
while i:
self.transport.writeSomeData(time.strftime('%H:%M:%S',time.localtime(time.time())))
self.transport.writeSomeData('\r\n')
time.sleep(2)
i-=1
writeSomeData()
#2, threads.deferToThread
def write():
i=10
while i:
self.transport.write(time.strftime('%H:%M:%S',time.localtime(time.time())))
self.transport.write('\r\n')
time.sleep(2)
i-=1
threads.deferToThread(write)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
reactor.listenTCP(8000, EchoFactory())
reactor.run()
def connectionMade(self):
logging.info('Got connection from :' + repr(self.transport.client))
boss_id = 0
while boss_id < self.count:
boss_id = boss_id + 1
msg = "{"method":"getSimInfo","id":%d"%boss_id + ","params":{"simBankAddress":1,"bladeLogicalAddress":1,"simAddress":%d}}"%boss_id
json_data = struct.pack('>cH{msg_len}s'.format(msg_len = len(msg)), '\x00', len(msg), msg)
reactor.callLater(9 + boss_id, self.transport.write, json_data)
#self.transport.write(json_data)
self.idSendList.append(boss_id)
logging.info('send :' + repr(json_data))
#time.sleep(5)