加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
testStream.py 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
Your Name 提交于 2019-08-11 13:51 . ff
import threading
from tornado.locks import Event
from tornado.ioloop import IOLoop
from tornado import gen
from helpFunc import *
import time
class ts():
def __init__(self,ioloop):
self.ioloop = ioloop
self.rEvent = Event()
self.wEvent = Event()
self.writeLock = threading.Lock()
self.readLock = threading.Lock()
self.writeBuffer = b''
self.readBuffer = b''
IOLoop.current().add_callback(self.doRead)
IOLoop.current().add_callback(self.doWrite)
@gen.coroutine
def doRead(self):
co = 0
while True:
yield self.rEvent.wait()
self.rEvent.clear()
while True:
msg = TOUMsg()
self.readLock.acquire()
r,self.readBuffer = msg.unpack(self.readBuffer)
self.readLock.release()
if not r:
break
co += 1
if co%10==0:
print ('co1',co,getRunningTime())
@gen.coroutine
def doWrite(self):
co = 0
while True:
yield self.wEvent.wait()
self.wEvent.clear()
while True:
if len(self.writeBuffer)>con_streamBufferSize:
break
yield gen.sleep(random.randint(3,20)/10.0)
msg = TOUMsg({},b's'*random.randint(10,2000))
self.writeLock.acquire()
self.writeBuffer += msg.pack()
self.writeLock.release()
co += 1
if co%10==0:
print ('co2',co,getRunningTime())
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化