Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查gRPC流是否为空或是否为';t流数据_Python_Python 3.x_Protocol Buffers_Grpc - Fatal编程技术网

Python 检查gRPC流是否为空或是否为';t流数据

Python 检查gRPC流是否为空或是否为';t流数据,python,python-3.x,protocol-buffers,grpc,Python,Python 3.x,Protocol Buffers,Grpc,我正在创建一个存根,该存根连接到一个服务器,该服务器以特定的间隔输出数据,然后将其上载到TSDB。我已经实现了批处理来优化上传,但是如果在一个时间间隔内流出来的数据量与批大小不一致,那么在下一个时间间隔之前,一些数据将不会被上传,这是我不想要的。gRPC存根上是否有方法检查流是否为空 class DialInClient(object): def __init__(self, host, port, timeout=100000000, user='root', password='la

我正在创建一个存根,该存根连接到一个服务器,该服务器以特定的间隔输出数据,然后将其上载到TSDB。我已经实现了批处理来优化上传,但是如果在一个时间间隔内流出来的数据量与批大小不一致,那么在下一个时间间隔之前,一些数据将不会被上传,这是我不想要的。gRPC存根上是否有方法检查流是否为空

class DialInClient(object):
    def __init__(self, host, port, timeout=100000000, user='root', password='lablab'):
        self._host = host
        self._port = port
        self._timeout = float(timeout)
        self._channel = None
        self._cisco_ems_stub = None
        self._connected = False
        self._metadata = [('username', user), ('password', password)]

    def subscribe(self, sub_id):
        sub_args = CreateSubsArgs(ReqId=1, encode=3, subidstr=sub_id)
        stream = self._cisco_ems_stub.CreateSubs(sub_args, timeout=self._timeout, metadata=self._metadata)
        for segment in stream:
            yield segment 

    def connect(self):
        self._channel = grpc.insecure_channel(':'.join([self._host,self._port]))
        try:
            grpc.channel_ready_future(self._channel).result(timeout=10)
            self._connected = True
        except grpc.FutureTimeoutError as e:
            raise DeviceFailedToConnect from e
        else:
            self._cisco_ems_stub = gRPCConfigOperStub(self._channel)

如果我设置了一个较低的超时,则整个通道将断开连接,我想在for循环中为流媒体添加某种超时,以查看我是否在1秒内没有获得另一个片段。yield
None
告诉我的另一部分,这就是结束,并在没有完整批量的情况下上载。

GRPC中本机不存在这种机制,但是
线程
库应该允许您在批处理满之前发送批处理。我已经包括了的一个修改版本,让您了解如何做到这一点

from\uuuuu future\uuuuu导入打印功能
进口grpc
导入helloworld_pb2
导入helloworld\u pb2\u grpc
导入线程
从6.0移动导入队列
导入时间
#10第二批
批次周期=10.0
def收集响应(响应队列,完成):
使用grpc.Unsecure_通道(“本地主机:50051”)作为通道:
存根=helloworld\u pb2\u grpc.问候存根(频道)
对于i,枚举中的响应(stub.SayHello(helloworld\u pb2.HelloRequest(name='you',num_greetings=“100”)):
resp_queue.put(响应)
已完成。set()
def是批处理结束(批处理开始):
返回时间.time()-批处理开始时间<批处理期间
def获取剩余时间(开始时间):
返回时间(开始时间+批处理时间段)-time.time()
def批处理响应(响应队列,完成):
批次数量=0
尽管如此:
批次响应=[]
批处理\u开始=时间。时间()
剩余时间=获取剩余时间(批处理启动)
剩余时间>0.0且未完成时。是否设置为()
尝试:
批处理响应追加(响应队列获取())
队列除外。空:
通过
最后:
剩余时间=获取剩余时间(批处理启动)
打印(“批处理{}({}):”。格式(批处理数+1,len(批处理响应)))
对于批量_resp中的resp:
打印(“{}”格式(响应消息))
批次数量+=1
def run():
resp_queue=queue.queue()
finished=threading.Event()
客户端线程=线程。线程(目标=收集响应,参数=(响应队列,完成))
client_thread.start()
批处理响应(响应队列,完成)
client_thread.join()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
运行()