class SdkHarness(object): def __init__(self, control_channel): self._control_channel = control_channel self._data_channel_factory = GrpcClientDataChannelFactory() def run(self): contol_stub = beam_fn_api_pb2.BeamFnControlStub(self._control_channel) # TODO(robertwb): Wire up to new state api. state_stub = None self.worker = SdkWorker(state_stub, self._data_channel_factory) responses = queue.Queue() no_more_work = object() def get_responses(): while True: response = responses.get() if response is no_more_work: return yield response def process_requests(): for work_request in contol_stub.Control(get_responses()): logging.info('Got work %s', work_request.instruction_id) try: response = self.worker.do_instruction(work_request) except Exception: # pylint: disable=broad-except logging.error( 'Error processing instruction %s', work_request.instruction_id, exc_info=True) response = beam_fn_api_pb2.InstructionResponse( instruction_id=work_request.instruction_id, error=traceback.format_exc()) responses.put(response) t = threading.Thread(target=process_requests) t.start() t.join() # get_responses may be blocked on responses.get(), but we need to return # control to its caller. responses.put(no_more_work) self._data_channel_factory.close() logging.info('Done consuming work.')
class SdkHarness(object): def __init__(self, control_channel): self._control_channel = control_channel self._data_channel_factory = GrpcClientDataChannelFactory() def run(self): contol_stub = beam_fn_api_pb2.BeamFnControlStub(self._control_channel) # TODO(robertwb): Wire up to new state api. state_stub = None self.worker = SdkWorker(state_stub, self._data_channel_factory) responses = queue.Queue() no_more_work = object() def get_responses(): while True: response = responses.get() if response is no_more_work: return yield response def process_requests(): for work_request in contol_stub.Control(get_responses()): logging.info('Got work %s', work_request.instruction_id) try: response = self.worker.do_instruction(work_request) except Exception: # pylint: disable=broad-except logging.error('Error processing instruction %s', work_request.instruction_id, exc_info=True) response = beam_fn_api_pb2.InstructionResponse( instruction_id=work_request.instruction_id, error=traceback.format_exc()) responses.put(response) t = threading.Thread(target=process_requests) t.start() t.join() # get_responses may be blocked on responses.get(), but we need to return # control to its caller. responses.put(no_more_work) self._data_channel_factory.close() logging.info('Done consuming work.')
def __init__(self, control_channel): self._control_channel = control_channel self._data_channel_factory = GrpcClientDataChannelFactory()