async def _list_all_events(self, request): start_time = request.start_time start_version = request.start_version end_version = request.end_version timeout_seconds = request.timeout_seconds if 0 == timeout_seconds: event_models = self._query_all_events(start_time, start_version, end_version) event_proto_list = event_list_to_proto(event_models) return notification_service_pb2.ListEventsResponse( return_code=notification_service_pb2.ReturnStatus.SUCCESS, return_msg='', events=event_proto_list) else: start = time.time() event_models = self._query_all_events(start_time, start_version, end_version) async with self.write_condition: while time.time() - start < timeout_seconds and len( event_models) == 0: try: await asyncio.wait_for( self.write_condition.wait(), timeout_seconds - time.time() + start) event_models = self._query_all_events( start_time, start_version, end_version) except asyncio.TimeoutError: pass event_proto_list = event_list_to_proto(event_models) return notification_service_pb2.ListEventsResponse( return_code=notification_service_pb2.ReturnStatus.SUCCESS, return_msg='', events=event_proto_list)
async def _list_events(self, request): keys = request.keys event_type = request.event_type start_time = request.start_time start_version = request.start_version namespace = None if request.namespace == '' else request.namespace sender = None if request.sender == '' else request.sender timeout_seconds = request.timeout_seconds if timeout_seconds == 0: event_models = self._query_events(keys, event_type, start_time, start_version, namespace, sender) event_proto_list = event_list_to_proto(event_models) return notification_service_pb2.ListEventsResponse( return_code=notification_service_pb2.ReturnStatus.SUCCESS, return_msg='', events=event_proto_list) else: start = time.time() # Lock conditions dict for get/check/update of key await self.lock.acquire() for key in keys: if self.notification_conditions.get(key) is None: self.notification_conditions.update({ (key, asyncio.Condition()) }) # Release lock after check/update key of notification conditions dict self.lock.release() event_models = [] if len(keys) == 1: key = keys[0] condition = self.notification_conditions.get(key) else: condition = self.write_condition async with condition: while time.time() - start < timeout_seconds and len( event_models) == 0: try: await asyncio.wait_for( condition.wait(), timeout_seconds - time.time() + start) event_models = self._query_events( keys, event_type, start_time, start_version, namespace, sender) except asyncio.TimeoutError: pass if len(event_models) == 0: event_models = self._query_events(keys, event_type, start_time, start_version, namespace, sender) event_proto_list = event_list_to_proto(event_models) return notification_service_pb2.ListEventsResponse( return_code=notification_service_pb2.ReturnStatus.SUCCESS, return_msg='', events=event_proto_list)