python grpcio 双向stream 问题

安宇雨 发布于 2017/02/14 13:58
阅读 204
收藏 0

现在是有这样的场境

server 端 会监听 subscribeSystemEvents

基于session_key=XXX返回到事件信息到调用的client端

 

client 端 发送 Empyt包+metadata(session_key=XXX) 到 server 端

        stub = WeworkService_pb2.WeworkServiceStub(channel=self.channel)
        request_iterator = self.generate_messages()
        response = stub.subscribeSystemEvents(
            request_iterator=request_iterator,
            # timeout=self._TIMEOUT_SECONDS,
            metadata=self.metadata)
        print("WeworkService.subscribeSystemEvents...: ")

        for one_of_system_event in response:
            # TODO 处理返回的event
       print('get a grpc event\nid:%s\nsender:%s\ntimeTag:%s' % (str(one_of_system_event.id), one_of_system_event.sender, one_of_system_event.timeTag))

问题就在于iterator完之后,程序会退出

希望在python中能实现以下效果

function subscribeSystemEvents() {
		var client = new weworkServices.WeworkServiceClient(channel:5000,
			grpc.credentials.createInsecure());
			
		var request = new securityMessages.Empty();
		var myMetadata = new grpc.Metadata();
		myMetadata.add('session_key' , mSessionId);
		
		var call = client.subscribeSystemEvents(myMetadata , request);
		//--------------------------------------------------GRPC回调函数3个-----------------
		call.on('data', function(mySystemEvent) {//---------grpc有事件返回时的回调---------------
			console.log(
				'get a grpc event!' ,
				'\nid:' , mySystemEvent.getId() ,
				'\nsender:' , mySystemEvent.getSender() ,
				'\ntimeTag:' , mySystemEvent.getTimetag() 
			);
			//--------------------------------------------------分情况返回事件块-----------------

也就是说,希望在client端连接server端时,方法subscribeSystemEvents 服务端没有说传输完成时,client端不会退出程序,一直监听服务端返回的stream

加载中
返回顶部
顶部