def consume_request_iterator(): for request in request_iterator: serialized_request = _common.serialize(request, request_serializer) with state.condition: if state.code is None and not state.cancelled: if serialized_request is None: call.cancel() details = 'Exception serializing request!' _abort(state, grpc.StatusCode.INTERNAL, details) return else: operations = (cygrpc.operation_send_message( serialized_request, _EMPTY_FLAGS), ) call.start_client_batch(cygrpc.Operations(operations), event_handler) state.due.add(cygrpc.OperationType.send_message) while True: state.condition.wait() if state.code is None: if cygrpc.OperationType.send_message not in state.due: break else: return else: return with state.condition: if state.code is None: operations = ( cygrpc.operation_send_close_from_client(_EMPTY_FLAGS), ) call.start_client_batch(cygrpc.Operations(operations), event_handler) state.due.add(cygrpc.OperationType.send_close_from_client)
def consume_request_iterator(): for request in request_iterator: serialized_request = _common.serialize(request, request_serializer) with state.condition: if state.code is None and not state.cancelled: if serialized_request is None: call.cancel() details = 'Exception serializing request!' _abort(state, grpc.StatusCode.INTERNAL, details) return else: operations = ( cygrpc.operation_send_message( serialized_request, _EMPTY_FLAGS), ) call.start_client_batch(cygrpc.Operations(operations), event_handler) state.due.add(cygrpc.OperationType.send_message) while True: state.condition.wait() if state.code is None: if cygrpc.OperationType.send_message not in state.due: break else: return else: return with state.condition: if state.code is None: operations = ( cygrpc.operation_send_close_from_client(_EMPTY_FLAGS), ) call.start_client_batch(cygrpc.Operations(operations), event_handler) state.due.add(cygrpc.OperationType.send_close_from_client)
async def __call__(self, request, timeout=None, metadata=None, credentials=None, wait_for_ready=None, compression=None): if timeout: raise NotImplementedError("TODO: timeout not implemented yet") if metadata: raise NotImplementedError("TODO: metadata not implemented yet") if credentials: raise NotImplementedError("TODO: credentials not implemented yet") if wait_for_ready: raise NotImplementedError( "TODO: wait_for_ready not implemented yet") if compression: raise NotImplementedError("TODO: compression not implemented yet") response = await self._channel.unary_unary( self._method, _common.serialize(request, self._request_serializer)) return _common.deserialize(response, self._response_deserializer)
def consume_request_iterator(): # pylint: disable=too-many-branches while True: return_from_user_request_generator_invoked = False try: # The thread may die in user-code. Do not block fork for this. cygrpc.enter_user_request_generator() request = next(request_iterator) except StopIteration: break except Exception: # pylint: disable=broad-except cygrpc.return_from_user_request_generator() return_from_user_request_generator_invoked = True code = grpc.StatusCode.UNKNOWN details = 'Exception iterating requests!' _LOGGER.exception(details) call.cancel(_common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code], details) _abort(state, code, details) return finally: if not return_from_user_request_generator_invoked: cygrpc.return_from_user_request_generator() serialized_request = _common.serialize(request, request_serializer) with state.condition: if state.code is None and not state.cancelled: if serialized_request is None: code = grpc.StatusCode.INTERNAL details = 'Exception serializing request!' call.cancel( _common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code], details) _abort(state, code, details) return else: operations = (cygrpc.SendMessageOperation( serialized_request, _EMPTY_FLAGS), ) operating = call.operate(operations, event_handler) if operating: state.due.add(cygrpc.OperationType.send_message) else: return while True: state.condition.wait(condition_wait_timeout) cygrpc.block_if_fork_in_progress(state) if state.code is None: if cygrpc.OperationType.send_message not in state.due: break else: return else: return with state.condition: if state.code is None: operations = ( cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS), ) operating = call.operate(operations, event_handler) if operating: state.due.add(cygrpc.OperationType.send_close_from_client)
def consume_request_iterator(): # pylint: disable=too-many-branches while True: return_from_user_request_generator_invoked = False try: # The thread may die in user-code. Do not block fork for this. cygrpc.enter_user_request_generator() request = next(request_iterator) except StopIteration: break except Exception: # pylint: disable=broad-except cygrpc.return_from_user_request_generator() return_from_user_request_generator_invoked = True code = grpc.StatusCode.UNKNOWN details = 'Exception iterating requests!' _LOGGER.exception(details) call.cancel(_common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code], details) _abort(state, code, details) return finally: if not return_from_user_request_generator_invoked: cygrpc.return_from_user_request_generator() serialized_request = _common.serialize(request, request_serializer) with state.condition: if state.code is None and not state.cancelled: if serialized_request is None: code = grpc.StatusCode.INTERNAL details = 'Exception serializing request!' call.cancel( _common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code], details) _abort(state, code, details) return else: operations = (cygrpc.SendMessageOperation( serialized_request, _EMPTY_FLAGS),) operating = call.operate(operations, event_handler) if operating: state.due.add(cygrpc.OperationType.send_message) else: return while True: state.condition.wait(condition_wait_timeout) cygrpc.block_if_fork_in_progress(state) if state.code is None: if cygrpc.OperationType.send_message not in state.due: break else: return else: return with state.condition: if state.code is None: operations = ( cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),) operating = call.operate(operations, event_handler) if operating: state.due.add(cygrpc.OperationType.send_close_from_client)
def _serialize_response(rpc_event, state, response, response_serializer): serialized_response = _common.serialize(response, response_serializer) if serialized_response is None: with state.condition: _abort(state, rpc_event.operation_call, cygrpc.StatusCode.internal, b'Failed to serialize response!') return None else: return serialized_response
async def _send_unary_request(self) -> ResponseType: serialized_request = _common.serialize(self._request, self._request_serializer) try: await self._cython_call.initiate_unary_stream( serialized_request, self._metadata) except asyncio.CancelledError: if not self.cancelled(): self.cancel() raise
def _start_unary_request(request, timeout, request_serializer): deadline = _deadline(timeout) serialized_request = _common.serialize(request, request_serializer) if serialized_request is None: state = _RPCState((), (), (), grpc.StatusCode.INTERNAL, 'Exception serializing request!') rendezvous = _Rendezvous(state, None, None, deadline) return deadline, None, rendezvous else: return deadline, serialized_request, None
def __call__(self, request, *, timeout=None, metadata=None, credentials=None, wait_for_ready=None, compression=None) -> Call: """Asynchronously invokes the underlying RPC. Args: request: The request value for the RPC. timeout: An optional duration of time in seconds to allow for the RPC. metadata: Optional :term:`metadata` to be transmitted to the service-side of the RPC. credentials: An optional CallCredentials for the RPC. Only valid for secure Channel. wait_for_ready: This is an EXPERIMENTAL argument. An optional flag to enable wait for ready mechanism compression: An element of grpc.compression, e.g. grpc.compression.Gzip. This is an EXPERIMENTAL option. Returns: A Call object instance which is an awaitable object. Raises: RpcError: Indicating that the RPC terminated with non-OK status. The raised RpcError will also be a Call for the RPC affording the RPC's metadata, status code, and details. """ if metadata: raise NotImplementedError("TODO: metadata not implemented yet") if credentials: raise NotImplementedError("TODO: credentials not implemented yet") if wait_for_ready: raise NotImplementedError( "TODO: wait_for_ready not implemented yet") if compression: raise NotImplementedError("TODO: compression not implemented yet") serialized_request = _common.serialize(request, self._request_serializer) timeout = self._timeout_to_deadline(timeout) aio_cancel_status = cygrpc.AioCancelStatus() aio_call = asyncio.ensure_future( self._channel.unary_unary(self._method, serialized_request, timeout, aio_cancel_status), loop=self._loop) return Call(aio_call, self._response_deserializer, aio_cancel_status)
async def _send_unary_request(self) -> ResponseType: serialized_request = _common.serialize(self._request, self._request_serializer) try: await self._cython_call.unary_stream(serialized_request, self._set_initial_metadata, self._set_status) except asyncio.CancelledError: if self._code != grpc.StatusCode.CANCELLED: self.cancel() raise
def _start_unary_request(request, timeout, request_serializer): deadline, deadline_timespec = _deadline(timeout) serialized_request = _common.serialize(request, request_serializer) if serialized_request is None: state = _RPCState( (), _EMPTY_METADATA, _EMPTY_METADATA, grpc.StatusCode.INTERNAL, "Exception serializing request!" ) rendezvous = _Rendezvous(state, None, None, deadline) return deadline, deadline_timespec, None, rendezvous else: return deadline, deadline_timespec, serialized_request, None
async def _invoke(self) -> ResponseType: serialized_request = _common.serialize(self._request, self._request_serializer) self._bytes_aiter = await self._channel.unary_stream( self._method, serialized_request, self._deadline, self._cancellation, self._set_initial_metadata, self._set_status, )
async def __call__(self, request, timeout=None, metadata=None, credentials=None, wait_for_ready=None, compression=None): """Asynchronously invokes the underlying RPC. Args: request: The request value for the RPC. timeout: An optional duration of time in seconds to allow for the RPC. metadata: Optional :term:`metadata` to be transmitted to the service-side of the RPC. credentials: An optional CallCredentials for the RPC. Only valid for secure Channel. wait_for_ready: This is an EXPERIMENTAL argument. An optional flag to enable wait for ready mechanism compression: An element of grpc.compression, e.g. grpc.compression.Gzip. This is an EXPERIMENTAL option. Returns: The response value for the RPC. Raises: RpcError: Indicating that the RPC terminated with non-OK status. The raised RpcError will also be a Call for the RPC affording the RPC's metadata, status code, and details. """ if timeout: raise NotImplementedError("TODO: timeout not implemented yet") if metadata: raise NotImplementedError("TODO: metadata not implemented yet") if credentials: raise NotImplementedError("TODO: credentials not implemented yet") if wait_for_ready: raise NotImplementedError( "TODO: wait_for_ready not implemented yet") if compression: raise NotImplementedError("TODO: compression not implemented yet") response = await self._channel.unary_unary( self._method, _common.serialize(request, self._request_serializer)) return _common.deserialize(response, self._response_deserializer)
def consume_request_iterator(): # pylint: disable=too-many-branches while True: try: request = next(request_iterator) except StopIteration: break except Exception: # pylint: disable=broad-except code = grpc.StatusCode.UNKNOWN details = 'Exception iterating requests!' logging.exception(details) call.cancel(_common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code], details) _abort(state, code, details) return serialized_request = _common.serialize(request, request_serializer) with state.condition: if state.code is None and not state.cancelled: if serialized_request is None: code = grpc.StatusCode.INTERNAL # pylint: disable=redefined-variable-type details = 'Exception serializing request!' call.cancel( _common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code], details) _abort(state, code, details) return else: operations = (cygrpc.SendMessageOperation( serialized_request, _EMPTY_FLAGS), ) operating = call.operate(operations, event_handler) if operating: state.due.add(cygrpc.OperationType.send_message) else: return while True: state.condition.wait() if state.code is None: if cygrpc.OperationType.send_message not in state.due: break else: return else: return with state.condition: if state.code is None: operations = ( cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS), ) operating = call.operate(operations, event_handler) if operating: state.due.add(cygrpc.OperationType.send_close_from_client)
async def write(self, request: RequestType) -> None: if self._cython_call.done(): raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS) if self._done_writing: raise asyncio.InvalidStateError(_RPC_HALF_CLOSED_DETAILS) if not self._metadata_sent.is_set(): await self._metadata_sent.wait() serialized_request = _common.serialize(request, self._request_serializer) try: await self._cython_call.send_serialized_message(serialized_request) except asyncio.CancelledError: if not self.cancelled(): self.cancel() await self._raise_for_status()
async def _invoke(self) -> ResponseType: serialized_request = _common.serialize(self._request, self._request_serializer) # NOTE(lidiz) asyncio.CancelledError is not a good transport for status, # because the asyncio.Task class do not cache the exception object. # https://github.com/python/cpython/blob/edad4d89e357c92f70c0324b937845d652b20afd/Lib/asyncio/tasks.py#L785 try: serialized_response = await self._cython_call.unary_unary( serialized_request, self._metadata) except asyncio.CancelledError: if not self.cancelled(): self.cancel() if self._cython_call.is_ok(): return _common.deserialize(serialized_response, self._response_deserializer) else: return cygrpc.EOF
def consume_request_iterator(): while True: try: request = next(request_iterator) except StopIteration: break except Exception: # pylint: disable=broad-except logging.exception("Exception iterating requests!") call.cancel() _abort(state, grpc.StatusCode.UNKNOWN, "Exception iterating requests!") return serialized_request = _common.serialize(request, request_serializer) with state.condition: if state.code is None and not state.cancelled: if serialized_request is None: call.cancel() details = 'Exception serializing request!' _abort(state, grpc.StatusCode.INTERNAL, details) return else: operations = (cygrpc.operation_send_message( serialized_request, _EMPTY_FLAGS),) call.start_client_batch( cygrpc.Operations(operations), event_handler) state.due.add(cygrpc.OperationType.send_message) while True: state.condition.wait() if state.code is None: if cygrpc.OperationType.send_message not in state.due: break else: return else: return with state.condition: if state.code is None: operations = ( cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),) call.start_client_batch( cygrpc.Operations(operations), event_handler) state.due.add(cygrpc.OperationType.send_close_from_client)
async def _invoke(self) -> ResponseType: serialized_request = _common.serialize(self._request, self._request_serializer) # NOTE(lidiz) asyncio.CancelledError is not a good transport for # status, since the Task class do not cache the exact # asyncio.CancelledError object. So, the solution is catching the error # in Cython layer, then cancel the RPC and update the status, finally # re-raise the CancelledError. serialized_response = await self._channel.unary_unary( self._method, serialized_request, self._deadline, self._cancellation, self._set_initial_metadata, self._set_status, ) await self._raise_rpc_error_if_not_ok() return _common.deserialize(serialized_response, self._response_deserializer)
async def _invoke(self) -> ResponseType: serialized_request = _common.serialize(self._request, self._request_serializer) # NOTE(lidiz) asyncio.CancelledError is not a good transport for status, # because the asyncio.Task class do not cache the exception object. # https://github.com/python/cpython/blob/edad4d89e357c92f70c0324b937845d652b20afd/Lib/asyncio/tasks.py#L785 try: serialized_response = await self._cython_call.unary_unary( serialized_request, self._set_initial_metadata, self._set_status, ) except asyncio.CancelledError: if self._code != grpc.StatusCode.CANCELLED: self.cancel() # Raises here if RPC failed or cancelled await self._raise_for_status() return _common.deserialize(serialized_response, self._response_deserializer)