def testUnary(self): request = b'\x00' * 100 # Client -> server compressed through default client channel compression # settings. Server -> client compressed via server-side metadata setting. # TODO(https://github.com/grpc/grpc/issues/4078): replace the "1" integer # literal with proper use of the public API. compressed_channel = grpc.insecure_channel( 'localhost:%d' % self._port, options=[('grpc.default_compression_algorithm', 1)]) multi_callable = compressed_channel.unary_unary(_UNARY_UNARY) response = multi_callable(request) self.assertEqual(request, response) # Client -> server compressed through client metadata setting. Server -> # client compressed via server-side metadata setting. # TODO(https://github.com/grpc/grpc/issues/4078): replace the "0" integer # literal with proper use of the public API. uncompressed_channel = grpc.insecure_channel( 'localhost:%d' % self._port, options=[('grpc.default_compression_algorithm', 0)]) multi_callable = compressed_channel.unary_unary(_UNARY_UNARY) response = multi_callable( request, metadata=[('grpc-internal-encoding-request', 'gzip')]) self.assertEqual(request, response)
def run_pipeline(self, pipeline): docker_image = ( pipeline.options.view_as(PortableOptions).harness_docker_image or self.default_docker_image()) job_endpoint = pipeline.options.view_as(PortableOptions).job_endpoint if not job_endpoint: raise ValueError( 'job_endpoint should be provided while creating runner.') proto_context = pipeline_context.PipelineContext( default_environment_url=docker_image) proto_pipeline = pipeline.to_runner_api(context=proto_context) if not self.is_embedded_fnapi_runner: # Java has different expectations about coders # (windowed in Fn API, but *un*windowed in runner API), whereas the # embedded FnApiRunner treats them consistently, so we must guard this # for now, until FnApiRunner is fixed. # See also BEAM-2717. for pcoll in proto_pipeline.components.pcollections.values(): if pcoll.coder_id not in proto_context.coders: # This is not really a coder id, but a pickled coder. coder = coders.registry.get_coder(pickler.loads(pcoll.coder_id)) pcoll.coder_id = proto_context.coders.get_id(coder) proto_context.coders.populate_map(proto_pipeline.components.coders) # Some runners won't detect the GroupByKey transform unless it has no # subtransforms. Remove all sub-transforms until BEAM-4605 is resolved. for _, transform_proto in list( proto_pipeline.components.transforms.items()): if transform_proto.spec.urn == common_urns.primitives.GROUP_BY_KEY.urn: for sub_transform in transform_proto.subtransforms: del proto_pipeline.components.transforms[sub_transform] del transform_proto.subtransforms[:] # TODO: Define URNs for options. options = {'beam:option:' + k + ':v1': v for k, v in pipeline._options.get_all_options().iteritems() if v is not None} job_service = beam_job_api_pb2_grpc.JobServiceStub( grpc.insecure_channel(job_endpoint)) prepare_response = job_service.Prepare( beam_job_api_pb2.PrepareJobRequest( job_name='job', pipeline=proto_pipeline, pipeline_options=job_utils.dict_to_struct(options))) if prepare_response.artifact_staging_endpoint.url: stager = portable_stager.PortableStager( grpc.insecure_channel(prepare_response.artifact_staging_endpoint.url), prepare_response.staging_session_token) retrieval_token, _ = stager.stage_job_resources( pipeline._options, staging_location='') else: retrieval_token = None run_response = job_service.Run( beam_job_api_pb2.RunJobRequest( preparation_id=prepare_response.preparation_id, retrieval_token=retrieval_token)) return PipelineResult(job_service, run_response.job_id)
def __init__(self, remote, host, port_public, port_admin, wallet_dir): self.remote = remote self.host = host self.port_public = port_public self.port_admin = port_admin self.wallet_dir = os.path.abspath(wallet_dir) self.channel_public = grpc.insecure_channel(self.node_public_address) self.channel_admin = grpc.insecure_channel(self.node_admin_address)
def _start_local_runner_subprocess_job_service(cls): cls._maybe_kill_subprocess() # TODO(robertwb): Consider letting the subprocess pick one and # communicate it back... port = cls._pick_unused_port() logging.info('Starting server on port %d.', port) cls._subprocess = subprocess.Popen(cls._subprocess_command(port)) address = 'localhost:%d' % port job_service = beam_job_api_pb2_grpc.JobServiceStub( grpc.insecure_channel(address)) logging.info('Waiting for server to be ready...') start = time.time() timeout = 30 while True: time.sleep(0.1) if cls._subprocess.poll() is not None: raise RuntimeError( 'Subprocess terminated unexpectedly with exit code %d.' % cls._subprocess.returncode) elif time.time() - start > timeout: raise RuntimeError( 'Pipeline timed out waiting for job service subprocess.') else: try: job_service.GetState( beam_job_api_pb2.GetJobStateRequest(job_id='[fake]')) break except grpc.RpcError as exn: if exn.code() != grpc.StatusCode.UNAVAILABLE: # We were able to contact the service for our fake state request. break logging.info('Server ready.') return address
def __init__(self, server, config, hist): # Create the stub if config.HasField('security_params'): creds = grpc.ssl_channel_credentials( resources.test_root_certificates()) channel = test_common.test_secure_channel( server, creds, config.security_params.server_host_override) else: channel = grpc.insecure_channel(server) # waits for the channel to be ready before we start sending messages grpc.channel_ready_future(channel).result() if config.payload_config.WhichOneof('payload') == 'simple_params': self._generic = False self._stub = services_pb2.BenchmarkServiceStub(channel) payload = messages_pb2.Payload( body='\0' * config.payload_config.simple_params.req_size) self._request = messages_pb2.SimpleRequest( payload=payload, response_size=config.payload_config.simple_params.resp_size) else: self._generic = True self._stub = GenericStub(channel) self._request = '\0' * config.payload_config.bytebuf_params.req_size self._hist = hist self._response_callbacks = []
def _connect_to_peer(self, host, port): try: channel = yield grpc.insecure_channel('{}:{}'.format(host, port)) log.info('grpc-channel-created-with-peer', peer=host) returnValue(channel) except Exception, e: log.exception('exception', e=e)
def make_insecure_stub(stub_class, host, port=None): """Makes an insecure stub for an RPC service. Uses / depends on gRPC. :type stub_class: type :param stub_class: A gRPC stub type for a given service. :type host: str :param host: The host for the service. May also include the port if ``port`` is unspecified. :type port: int :param port: (Optional) The port for the service. :rtype: object, instance of ``stub_class`` :returns: The stub object used to make gRPC requests to a given API. """ if port is None: target = host else: # NOTE: This assumes port != http_client.HTTPS_PORT: target = '%s:%d' % (host, port) channel = grpc.insecure_channel(target) return stub_class(channel)
def main(): # Connect with the gRPC server server_address = "127.0.0.1:50051" request_timeout = 5.0 channel = grpc.insecure_channel(server_address) stub = predict_pb2.PredictionServiceStub(channel) # Make request data request = predict_pb2.PredictRequest() image = Image.open('../mnist_jpgs/4/pic_test1010.png') array = np.array(image)/(255*1.0) samples_features = array.reshape([-1,784]) # samples_features = np.array( # [[10, 10, 10, 8, 6, 1, 8, 9, 1], [10, 10, 10, 8, 6, 1, 8, 9, 1]]) samples_keys = np.array([1]) # Convert numpy to TensorProto request.inputs["features"].CopyFrom(tensor_util.make_tensor_proto( samples_features)) request.inputs["key"].CopyFrom(tensor_util.make_tensor_proto(samples_keys)) # Invoke gRPC request response = stub.Predict(request, request_timeout) # Convert TensorProto to numpy result = {} for k, v in response.outputs.items(): result[k] = tensor_util.MakeNdarray(v) print(result)
def do_inference(hostport, work_dir, concurrency, num_tests): """Tests PredictionService with concurrent requests. Args: hostport: Host:port address of the PredictionService. work_dir: The full path of working directory for test data set. concurrency: Maximum number of concurrent requests. num_tests: Number of test images to use. Returns: The classification error rate. Raises: IOError: An error occurred processing test data set. """ test_data_set = mnist_input_data.read_data_sets(work_dir).test channel = grpc.insecure_channel(hostport) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) result_counter = _ResultCounter(num_tests, concurrency) for _ in range(num_tests): request = predict_pb2.PredictRequest() request.model_spec.name = 'mnist' request.model_spec.signature_name = 'predict_images' image, label = test_data_set.next_batch(1) request.inputs['images'].CopyFrom( tf.contrib.util.make_tensor_proto(image[0], shape=[1, image[0].size])) result_counter.throttle() result_future = stub.Predict.future(request, 5.0) # 5 seconds result_future.add_done_callback( _create_rpc_callback(label[0], result_counter)) return result_counter.get_error_rate()
def __init__(self, policy_class=thread.Policy, **kwargs): # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get('PUBSUB_EMULATOR_HOST'): kwargs['channel'] = grpc.insecure_channel( target=os.environ.get('PUBSUB_EMULATOR_HOST'), ) # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if 'channel' not in kwargs: kwargs['channel'] = grpc_helpers.create_channel( credentials=kwargs.pop('credentials', None), target=self.target, scopes=subscriber_client.SubscriberClient._DEFAULT_SCOPES, options={ 'grpc.max_send_message_length': -1, 'grpc.max_receive_message_length': -1, 'grpc.keepalive_time_ms': 30000, }.items(), ) # Add the metrics headers, and instantiate the underlying GAPIC # client. self.api = subscriber_client.SubscriberClient(**kwargs) # The subcription class is responsible to retrieving and dispatching # messages. self._policy_class = policy_class
def _CreateService(): """Provides a servicer backend and a stub. Returns: A _Service with which to test RPCs. """ servicer_methods = _ServicerMethods() class Servicer(getattr(service_pb2_grpc, SERVICER_IDENTIFIER)): def UnaryCall(self, request, context): return servicer_methods.UnaryCall(request, context) def StreamingOutputCall(self, request, context): return servicer_methods.StreamingOutputCall(request, context) def StreamingInputCall(self, request_iter, context): return servicer_methods.StreamingInputCall(request_iter, context) def FullDuplexCall(self, request_iter, context): return servicer_methods.FullDuplexCall(request_iter, context) def HalfDuplexCall(self, request_iter, context): return servicer_methods.HalfDuplexCall(request_iter, context) server = test_common.test_server() getattr(service_pb2_grpc, ADD_SERVICER_TO_SERVER_IDENTIFIER)(Servicer(), server) port = server.add_insecure_port('[::]:0') server.start() channel = grpc.insecure_channel('localhost:{}'.format(port)) stub = getattr(service_pb2_grpc, STUB_IDENTIFIER)(channel) return _Service(servicer_methods, server, stub)
def main(unused_argv): """Main entry point for SDK Fn Harness.""" logging_service_descriptor = beam_fn_api_pb2.ApiServiceDescriptor() text_format.Merge(os.environ['LOGGING_API_SERVICE_DESCRIPTOR'], logging_service_descriptor) # Send all logs to the runner. fn_log_handler = FnApiLogRecordHandler(logging_service_descriptor) # TODO(vikasrk): This should be picked up from pipeline options. logging.getLogger().setLevel(logging.INFO) logging.getLogger().addHandler(fn_log_handler) try: logging.info('Python sdk harness started.') service_descriptor = beam_fn_api_pb2.ApiServiceDescriptor() text_format.Merge(os.environ['CONTROL_API_SERVICE_DESCRIPTOR'], service_descriptor) # TODO(robertwb): Support credentials. assert not service_descriptor.oauth2_client_credentials_grant.url channel = grpc.insecure_channel(service_descriptor.url) SdkHarness(channel).run() logging.info('Python sdk harness exiting.') except: # pylint: disable=broad-except logging.exception('Python sdk harness failed: ') raise finally: fn_log_handler.close()
def __init__(self, server, config, hist): # Create the stub if config.HasField('security_params'): creds = grpc.ssl_channel_credentials(resources.test_root_certificates()) channel = test_common.test_secure_channel( server, creds, config.security_params.server_host_override) else: channel = grpc.insecure_channel(server) connected_event = threading.Event() def wait_for_ready(connectivity): if connectivity == grpc.ChannelConnectivity.READY: connected_event.set() channel.subscribe(wait_for_ready, try_to_connect=True) connected_event.wait() if config.payload_config.WhichOneof('payload') == 'simple_params': self._generic = False self._stub = services_pb2.BenchmarkServiceStub(channel) payload = messages_pb2.Payload( body='\0' * config.payload_config.simple_params.req_size) self._request = messages_pb2.SimpleRequest( payload=payload, response_size=config.payload_config.simple_params.resp_size) else: self._generic = True self._stub = GenericStub(channel) self._request = '\0' * config.payload_config.bytebuf_params.req_size self._hist = hist self._response_callbacks = []
def __init__(self, batch_settings=(), **kwargs): # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get("PUBSUB_EMULATOR_HOST"): kwargs["channel"] = grpc.insecure_channel( target=os.environ.get("PUBSUB_EMULATOR_HOST") ) # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if "channel" not in kwargs: kwargs["channel"] = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, scopes=publisher_client.PublisherClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, "grpc.max_receive_message_length": -1, }.items(), ) # Add the metrics headers, and instantiate the underlying GAPIC # client. self.api = publisher_client.PublisherClient(**kwargs) self.batch_settings = types.BatchSettings(*batch_settings) # The batches on the publisher client are responsible for holding # messages. One batch exists for each topic. self._batch_lock = self._batch_class.make_lock() self._batches = {}
def main(): channel = grpc.insecure_channel('localhost:8080') # 使用 stub client = pi_pb2_grpc.PiCalculatorStub(channel) # 调用吧 for i in range(1, 1000): print "pi(%d) =" % i, client.Calc(pi_pb2.PiRequest(n=i)).value
def __init__(self, storage_client_settings=(), namespace="public", **kwargs): # init the storage client settings self.storage_client_settings =\ types.StorageClientSettings(*storage_client_settings) __logger__.info("Creating a storage client to cluster '%s'", self.storage_client_settings.service_uri) service_uri = ServiceURI(self.storage_client_settings.service_uri) assert service_uri.service_name.lower() == 'bk' # create channel self.__channel__ = grpc.insecure_channel( target=service_uri.service_location ) __logger__.info( "Successfully created a storage client to cluster '%s'", self.storage_client_settings.service_uri) # create the rpc stub self.__root_range__ = RootRangeServiceStub(channel=self.__channel__) # assign the namespace self.__namespace__ = namespace
def VerifyPredictRequest(self, model_server_address, expected_output, expected_version, model_name='default', specify_output=True, signature_name= signature_constants. DEFAULT_SERVING_SIGNATURE_DEF_KEY): """Send PredictionService.Predict request and verify output.""" print('Sending Predict request...') # Prepare request request = predict_pb2.PredictRequest() request.model_spec.name = model_name request.model_spec.signature_name = signature_name request.inputs['x'].dtype = types_pb2.DT_FLOAT request.inputs['x'].float_val.append(2.0) dim = request.inputs['x'].tensor_shape.dim.add() dim.size = 1 if specify_output: request.output_filter.append('y') # Send request channel = grpc.insecure_channel(model_server_address) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) result = stub.Predict(request, RPC_TIMEOUT) # 5 secs timeout # Verify response self.assertTrue('y' in result.outputs) self.assertIs(types_pb2.DT_FLOAT, result.outputs['y'].dtype) self.assertEqual(1, len(result.outputs['y'].float_val)) self.assertEqual(expected_output, result.outputs['y'].float_val[0]) self._VerifyModelSpec(result.model_spec, request.model_spec.name, signature_name, expected_version)
def testRegress(self): """Test PredictionService.Regress implementation.""" model_path = self._GetSavedModelBundlePath() model_server_address = TensorflowModelServerTest.RunServer( 'default', model_path)[1] print('Sending Regress request...') # Prepare request request = regression_pb2.RegressionRequest() request.model_spec.name = 'default' request.model_spec.signature_name = 'regress_x_to_y' example = request.input.example_list.examples.add() example.features.feature['x'].float_list.value.extend([2.0]) # Send request channel = grpc.insecure_channel(model_server_address) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) result = stub.Regress(request, RPC_TIMEOUT) # 5 secs timeout # Verify response self.assertEqual(1, len(result.result.regressions)) expected_output = 3.0 self.assertEqual(expected_output, result.result.regressions[0].value) self._VerifyModelSpec(result.model_spec, request.model_spec.name, request.model_spec.signature_name, self._GetModelVersion(model_path))
def __init__( self, project=None, credentials=None, read_only=False, admin=False, channel=None ): if read_only and admin: raise ValueError( "A read-only client cannot also perform" "administrative actions." ) # NOTE: We set the scopes **before** calling the parent constructor. # It **may** use those scopes in ``with_scopes_if_required``. self._read_only = bool(read_only) self._admin = bool(admin) self._emulator_host = os.getenv(BIGTABLE_EMULATOR) self._emulator_channel = None if self._emulator_host is not None: self._emulator_channel = grpc.insecure_channel(self._emulator_host) if channel is not None: warnings.warn( "'channel' is deprecated and no longer used.", DeprecationWarning, stacklevel=2, ) self._channel = channel self.SCOPE = self._get_scopes() super(Client, self).__init__(project=project, credentials=credentials)
def __init__(self, logger, args, env=None, env_vars=None): """Initialize the process with the specified logger, arguments, and environment.""" _process.Process.__init__(self, logger, args, env=env, env_vars=env_vars) self._id = None self._stub = self.jasper_pb2_grpc.JasperProcessManagerStub( grpc.insecure_channel(self.connection_str)) self._return_code = None
def __init__(self, **kwargs): # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get("PUBSUB_EMULATOR_HOST"): kwargs["channel"] = grpc.insecure_channel( target=os.environ.get("PUBSUB_EMULATOR_HOST") ) # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if "transport" not in kwargs: channel = kwargs.pop("channel", None) if channel is None: channel = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, scopes=subscriber_client.SubscriberClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, "grpc.max_receive_message_length": -1, "grpc.keepalive_time_ms": 30000, }.items(), ) # cannot pass both 'channel' and 'credentials' kwargs.pop("credentials", None) transport = subscriber_grpc_transport.SubscriberGrpcTransport( channel=channel ) kwargs["transport"] = transport # Add the metrics headers, and instantiate the underlying GAPIC # client. self._api = subscriber_client.SubscriberClient(**kwargs)
def __init__(self): self.state_handler = FnApiRunner.SimpleState() self.control_server = grpc.server( futures.ThreadPoolExecutor(max_workers=10)) self.control_port = self.control_server.add_insecure_port('[::]:0') self.data_server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) self.data_port = self.data_server.add_insecure_port('[::]:0') self.control_handler = streaming_rpc_handler( beam_fn_api_pb2.BeamFnControlServicer, 'Control') beam_fn_api_pb2.add_BeamFnControlServicer_to_server( self.control_handler, self.control_server) self.data_plane_handler = data_plane.GrpcServerDataChannel() beam_fn_api_pb2.add_BeamFnDataServicer_to_server( self.data_plane_handler, self.data_server) logging.info('starting control server on port %s', self.control_port) logging.info('starting data server on port %s', self.data_port) self.data_server.start() self.control_server.start() self.worker = sdk_worker.SdkHarness( grpc.insecure_channel('localhost:%s' % self.control_port)) self.worker_thread = threading.Thread(target=self.worker.run) logging.info('starting worker') self.worker_thread.start()
def setUp(self): self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY) self._server = grpc.server( self._server_pool, handlers=(_GenericHandler(),)) port = self._server.add_insecure_port('[::]:0') self._server.start() self._channel = grpc.insecure_channel('localhost:%d' % port)
def test_lonely_channel_connectivity(self): callback = _Callback() channel = grpc.insecure_channel('localhost:12345') channel.subscribe(callback.update, try_to_connect=False) first_connectivities = callback.block_until_connectivities_satisfy(bool) channel.subscribe(callback.update, try_to_connect=True) second_connectivities = callback.block_until_connectivities_satisfy( lambda connectivities: 2 <= len(connectivities)) # Wait for a connection that will never happen. time.sleep(test_constants.SHORT_TIMEOUT) third_connectivities = callback.connectivities() channel.unsubscribe(callback.update) fourth_connectivities = callback.connectivities() channel.unsubscribe(callback.update) fifth_connectivities = callback.connectivities() channel.close() self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE,), first_connectivities) self.assertNotIn(grpc.ChannelConnectivity.READY, second_connectivities) self.assertNotIn(grpc.ChannelConnectivity.READY, third_connectivities) self.assertNotIn(grpc.ChannelConnectivity.READY, fourth_connectivities) self.assertNotIn(grpc.ChannelConnectivity.READY, fifth_connectivities)
def main(): channel = grpc.insecure_channel("localhost:5050") stub = QuoterStub(channel) for i in range(3): r = stub.GetQuote(QuoteRequest(author=None)) print("{} ~ {}".format(r.quote, r.author))
def connect(self, grpc_url=DEF_GRPC_URL): if self.debug: print('Connecting to ' + grpc_url) if self.is_connected(): raise self.APIError('Already connected') while not self.is_connected(): if self.channel is None: self.status = None self.peer = grpc_url self.channel = grpc.insecure_channel(grpc_url) self.channel.subscribe(self._update_status, try_to_connect=True) self.stub = service_pb2.BESSControlStub(self.channel) elif self.status == self.CLOSING_CHANNEL: self.disconnect() elif self.status in [grpc.ChannelConnectivity.TRANSIENT_FAILURE, grpc.ChannelConnectivity.SHUTDOWN, self.BROKEN_CHANNEL]: self.disconnect() raise self.APIError( 'Connection to {} failed'.format(grpc_url)) time.sleep(0.1)
def run(): parser = argparse.ArgumentParser() parser.add_argument( '--log_payloads', action='store_true', help='log request/response objects to open-tracing spans') args = parser.parse_args() config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='trivial-client') tracer = config.initialize_tracer() tracer_interceptor = open_tracing_client_interceptor( tracer, log_payloads=args.log_payloads) channel = grpc.insecure_channel('localhost:50051') channel = intercept_channel(channel, tracer_interceptor) stub = command_line_pb2.CommandLineStub(channel) response = stub.Echo(command_line_pb2.CommandRequest(text='Hello, hello')) print(response.text) time.sleep(2) tracer.close() time.sleep(2)
def setUp(self): self._server = test_common.test_server() self._server.add_generic_rpc_handlers((_GenericHandler( weakref.proxy(self)),)) port = self._server.add_insecure_port('[::]:0') self._server.start() self._channel = grpc.insecure_channel('localhost:%d' % port)
def report(taskid, instanceid, status, token): global taskmgr master_port = env.getenv('BATCH_MASTER_PORT') channel = grpc.insecure_channel('%s:%s' % ('0.0.0.0', master_port)) stub = MasterStub(channel) response = stub.report(ReportMsg(taskmsgs=[TaskMsg(taskid=taskid, username='******', vnodeid=instanceid, subTaskStatus=status, token=token)]))
def run(): channel = grpc.insecure_channel('localhost:2020') stub = bosster_pb2_grpc.PosterStub(channel) target_fb = bosster_pb2.PostJob(type=bosster_pb2.FACEBOOK, # id группы social_id = "24234234", # токен админа, от его имени будет опубликогвана запись social_token = "", social_app_id = "", social_app_secret = "", ) target_vk = bosster_pb2.PostJob(type=bosster_pb2.VK, social_id = "39816168", social_token = "4722691fb64cd18d27bf2af4e4c355c26d902c32a0be0e3eb71932e403187566490406e05bc31437f3e3b", ) post = bosster_pb2.Post(message='hello from bosster',image_urls=['https://pp.userapi.com/c639320/v639320146/51c82/NLu2r3Q3sJw.jpg']); req = bosster_pb2.PostRequest(post=post, sync=True, targets=[target_vk]) response = stub.Post(req) print("client received: " + str(response.jobs[0].status)) print("{0}".format(response.jobs[0].status == bosster_pb2.ENQUEUED))
import grpc # import the generated classes import service.proto_spec.compo1_service_pb2_grpc as grpc_compo1_grpc import service.proto_spec.compo1_service_pb2 as grpc_compo1_pb2 from service import registry if __name__ == "__main__": # Call Compo1 Service try: # Ask endpoint and argument dflt_ep = "localhost:{}".format(registry["compo1_service"]["grpc"]) endpoint = input("Endpoint [default={}]: ".format(dflt_ep)) or dflt_ep argument = int(input("Argument [default=20]: ") or "20") # Open a gRPC channel channel = grpc.insecure_channel(endpoint) stub = grpc_compo1_grpc.Compo1Stub(channel) arguments = grpc_compo1_pb2.Arguments(argument=argument) # Carry out service response = stub.compo1(arguments) print(response.value) except Exception as e: print(e) exit(1)
def get_store(store_uri: str) -> esgrpc.EventstoreServiceStub: channel = grpc.insecure_channel(store_uri) return esgrpc.EventstoreServiceStub(channel=channel)
import grpc import greeter_pb2_grpc import greeter_pb2 import prometheus_client from prometheus_client import Counter from flask import Response, Flask, request, make_response from interceptor import header_adder_interceptor from log import init_log init_log() app = Flask(__name__) requests_total = Counter("request_count", "Total request cout of the host") channel = grpc.insecure_channel('test-greeter:50051') header_interceptor = header_adder_interceptor(request) intercept_channel = grpc.intercept_channel(channel, header_interceptor) greeter_client = greeter_pb2_grpc.GreeterStub(intercept_channel) retry_count = {} @app.route("/metrics") def requests_count(): return Response(prometheus_client.generate_latest(requests_total), mimetype="text/plain")
logging.fatal(error_message) exit(1) check_not_empty(worker_id, "No id provided.") check_not_empty(logging_endpoint, "No logging endpoint provided.") check_not_empty(artifact_endpoint, "No artifact endpoint provided.") check_not_empty(provision_endpoint, "No provision endpoint provided.") check_not_empty(control_endpoint, "No control endpoint provided.") logging.info("Initializing python harness: %s" % " ".join(sys.argv)) metadata = [("worker_id", worker_id)] # read job information from provision stub with grpc.insecure_channel(provision_endpoint) as channel: client = ProvisionServiceStub(channel=channel) info = client.GetProvisionInfo(GetProvisionInfoRequest(), metadata=metadata).info options = json_format.MessageToJson(info.pipeline_options) staged_dir = os.path.join(semi_persist_dir, "staged") # download files with grpc.insecure_channel(artifact_endpoint) as channel: client = ArtifactRetrievalServiceStub(channel=channel) # get file list via retrieval token response = client.GetManifest( GetManifestRequest(retrieval_token=info.retrieval_token), metadata=metadata) artifacts = response.manifest.artifact
import os from flask import Flask, render_template import grpc from recommendations_pb2 import BookCategory, RecommendationRequest from recommendations_pb2_grpc import RecommendationsStub app = Flask(__name__) recommendations_host = os.getenv('RECOMMENDATIONS_HOST', 'localhost') recommendation_channel = grpc.insecure_channel(f"{recommendations_host}:50051") recommendations_client = RecommendationsStub(recommendation_channel) @app.route("/") def render_homepage(): recommendations_request = RecommendationRequest( user_id=1, category=BookCategory.MYSTERY, max_results=3) recommendation_response = recommendations_client.Recommend( recommendations_request) return render_template( "homepage.html", recommendations=recommendation_response.recommendations, )
def __init__(self, address): channel = grpc.insecure_channel(address) self.stub = detection_pb2_grpc.DetectionStub(channel)
def run(): channel = grpc.insecure_channel('localhost:50087') stub = force_pb2_grpc.ForceStub(channel) fwdForce = force_pb2.GeneralizedForce( x = 5000.0, y = 0.0, z = 0.0, k = 0.0, m = 0.0, n = 0.0 ) leftForce = force_pb2.GeneralizedForce( x = 0.0, y = -5000.0, z = 0.0, k = 0.0, m = 0.0, n = 0.0 ) bckwdForce = force_pb2.GeneralizedForce( x = -5000.0, y = 0.0, z = 0.0, k = 0.0, m = 0.0, n = 0.0 ) rightForce = force_pb2.GeneralizedForce( x = 0.0, y = 5000.0, z = 0.0, k = 0.0, m = 0.0, n = 0.0 ) upForce = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = -5000.0, k = 0.0, m = 0.0, n = 0.0 ) downForce = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = 5000.0, k = 0.0, m = 0.0, n = 0.0 ) leftTorque = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = 0.0, k = -5000.0, m = 0.0, n = 0.0 ) rightTorque = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = 0.0, k = 5000.0, m = 0.0, n = 0.0 ) upTorque = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = 0.0, k = 0.0, m = -5000.0, n = 0.0 ) downTorque = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = 0.0, k = 0.0, m = 5000.0, n = 0.0 ) posNTorque = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = 0.0, k = 0.0, m = 0.0, n = 5000.0 ) negNTorque = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = 0.0, k = 0.0, m = 0.0, n = -5000.0 ) noForce = force_pb2.GeneralizedForce( x = 0.0, y = 0.0, z = 0.0, k = 0.0, m = 0.0, n = 0.0 ) command = "" while True: command = input() if command == "w": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = fwdForce)) elif command == "s": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = bckwdForce)) elif command == "d": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = rightForce)) elif command == "a": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = leftForce)) elif command == "r": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = upForce)) elif command == "f": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = downForce)) elif command == "j": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = leftTorque)) elif command == "l": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = rightTorque)) elif command == "i": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = upTorque)) elif command == "k": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = downTorque)) elif command == "y": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = posNTorque)) elif command == "h": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = negNTorque)) elif command == " ": success = stub.ApplyForce(force_pb2.ForceRequest(vesselId = "Ferry", generalizedForce = noForce))
def createStub(self): self.stub = bank_pb2_grpc.BankingStub( grpc.insecure_channel('localhost:' + str(PORT + self.id))) return self.stub
args = vars(parser.parse_args()) address = "{}:{}".format(args['grpc_address'], args['grpc_port']) channel = None if args.get('tls'): server_ca_cert, client_key, client_cert = prepare_certs( server_cert=args['server_cert'], client_key=args['client_key'], client_ca=args['client_cert']) creds = grpc.ssl_channel_credentials(root_certificates=server_ca_cert, private_key=client_key, certificate_chain=client_cert) channel = grpc.secure_channel(address, creds) else: channel = grpc.insecure_channel(address) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) files = os.listdir(args['input_images_dir']) batch_size = args['batch_size'] print(files) imgs = np.zeros((0, 3, args['height'], args['width']), np.dtype('<f')) for i in files: img = load_image(os.path.join(args['input_images_dir'], i)) imgs = np.append(imgs, img, axis=0) # contains all imported images print('Start processing {} iterations with batch size {}'.format( len(files) // batch_size, batch_size))
import jarvis_nlp_pb2 as jnlp import jarvis_nlp_pb2_grpc as jnlp_srv def get_args(): parser = argparse.ArgumentParser(description="Jarvis Question Answering client sample") parser.add_argument("--jarvis-uri", default="localhost:50052", type=str, help="URI to access Jarvis server") parser.add_argument("--iterations", default=10, type=int, help="number of queries to make") return parser.parse_args() parser = get_args() grpc_server = parser.jarvis_uri channel = grpc.insecure_channel(grpc_server) jarvis_nlp = jnlp_srv.JarvisNLPStub(channel) ok_counter = fastcounter.Counter() bad_counter = fastcounter.Counter() def process_response(call_future): # print(call_future.exception()) # print(call_future.result()) if call_future.exception(): bad_counter.increment() else: ok_counter.increment() def run(iterations): req = jnlp.NaturalQueryRequest()
def setUpClass(cls): cls.conn = grpc.insecure_channel(_HOST + ':' + _PORT) cls.client = data_pb2_grpc.FormatDataStub(channel=cls.conn)
def __init__(self): channel = grpc.insecure_channel('127.0.0.1:50051') self.stub = user_pb2_grpc.UserStub(channel)
def start_server(): conn = grpc.insecure_channel(_HOST + ':' + _PORT) client = sigmob_jion_pb2_grpc.RtbStub(channel=conn) response = client.GetMobileAd(generate_search_log()) print(response)
import grpc import AddNumbers_pb2 import AddNumbers_pb2_grpc channel = grpc.insecure_channel('localhost:50052') stub = AddNumbers_pb2_grpc.AddNumbersStub(channel) input = AddNumbers_pb2.input(a=5, b=6) response = stub.AddNumbers(input) print(response.value)
parser.add_argument('--host', default='127.0.0.1', help='ip address of the node') parser.add_argument('--port', default='19009', help='port of the node') parser.add_argument('--show_request', action='store_true', default=False, help='display outgoing query message') parser.add_argument( 'object', help='what to query, such as [block number], [txhash], [Q-address], stats') args = parser.parse_args() address = '{}:{}'.format(args.host, args.port) channel = grpc.insecure_channel(address, options=[('grpc.max_receive_message_length', 4194304 * 2)]) qrl_client = qrl_pb2_grpc.PublicAPIStub(channel) logging.info('Connected to {}'.format(address)) # Determine if we are querying something sensible if len(args.object) == 79 and args.object.startswith('Q'): logging.info('Query of address detected') query_addr(qrl_client, args.object) elif len(args.object) == 64: logging.info('Query of tx detected') query_txn(qrl_client, args.object) elif args.object.isnumeric(): logging.info('Query of block number detected') query_block(qrl_client, int(args.object)) elif args.object == 'stats':
def test_get_batch_features(self, mock_client, mocker): mock_client._serving_service_stub = Serving.ServingServiceStub( grpc.insecure_channel("")) mock_client._core_service_stub = Core.CoreServiceStub( grpc.insecure_channel("")) mocker.patch.object( mock_client._core_service_stub, "GetFeatureSet", return_value=GetFeatureSetResponse(feature_set=FeatureSetSpec( name="customer_fs", version=1, entities=[ EntitySpec(name="customer", value_type=ValueProto.ValueType.INT64), EntitySpec(name="transaction", value_type=ValueProto.ValueType.INT64), ], features=[ FeatureSpec( name="customer_feature_1", value_type=ValueProto.ValueType.FLOAT, ), FeatureSpec( name="customer_feature_2", value_type=ValueProto.ValueType.STRING, ), ], )), ) expected_dataframe = pd.DataFrame({ "datetime": [datetime.utcnow() for _ in range(3)], "customer": [1001, 1002, 1003], "transaction": [1001, 1002, 1003], "customer_fs:1:customer_feature_1": [1001, 1002, 1003], "customer_fs:1:customer_feature_2": [1001, 1002, 1003], }) final_results = tempfile.mktemp() to_avro(file_path_or_buffer=final_results, df=expected_dataframe) mocker.patch.object( mock_client._serving_service_stub, "GetBatchFeatures", return_value=GetBatchFeaturesResponse(job=BatchFeaturesJob( id="123", type=JobType.JOB_TYPE_DOWNLOAD, status=JobStatus.JOB_STATUS_DONE, file_uris=[f"file://{final_results}"], data_format=DataFormat.DATA_FORMAT_AVRO, )), ) mocker.patch.object( mock_client._serving_service_stub, "GetJob", return_value=GetJobResponse(job=BatchFeaturesJob( id="123", type=JobType.JOB_TYPE_DOWNLOAD, status=JobStatus.JOB_STATUS_DONE, file_uris=[f"file://{final_results}"], data_format=DataFormat.DATA_FORMAT_AVRO, )), ) mocker.patch.object( mock_client._serving_service_stub, "GetFeastServingInfo", return_value=GetFeastServingInfoResponse( job_staging_location=f"file://{tempfile.mkdtemp()}/", type=FeastServingType.FEAST_SERVING_TYPE_BATCH, ), ) response = mock_client.get_batch_features( entity_rows=pd.DataFrame({ "datetime": [ pd.datetime.now(tz=timezone("Asia/Singapore")) for _ in range(3) ], "customer": [1001, 1002, 1003], "transaction": [1001, 1002, 1003], }), feature_ids=[ "customer_fs:1:customer_feature_1", "customer_fs:1:customer_feature_2", ], ) # type: Job assert response.id == "123" and response.status == JobStatus.JOB_STATUS_DONE actual_dataframe = response.to_dataframe() assert actual_dataframe[[ "customer_fs:1:customer_feature_1", "customer_fs:1:customer_feature_2" ]].equals(expected_dataframe[[ "customer_fs:1:customer_feature_1", "customer_fs:1:customer_feature_2" ]])
def _init_rpc(self): self.rpc_connection = grpc.insecure_channel( current_app.config["IMPORTER_GRPC"]) self.load_stub: ImportStub = ImportStub(self.rpc_connection)
def _get_auth_insecure_channel(self, opts=None): channel = grpc.insecure_channel(self.endpoint, opts) return grpc.intercept_channel(channel, TokenAuthentication(self.token))
def __init__(self): pb2_module, addr = GetgRPCService('content') channel = grpc.insecure_channel('%s:%d' % addr) self._stub = pb2_module.ContentInfoStub(channel) self._pb2_module = pb2_module
def __init__(self, host='localhost', port=2379): self.channel = grpc.insecure_channel('{host}:{port}'.format(host=host, port=port)) self.kvstub = etcdrpc.KVStub(self.channel) self.transactions = Transactions()
def __init__(self, control_address): self._control_channel = grpc.insecure_channel(control_address) self._data_channel_factory = data_plane.GrpcClientDataChannelFactory()
str(ipaddress.ip_address(result.Prefix)), result.PrefixLen, result.ErrStatus.Status )) os._exit(0) # # Setup the GRPC channel with the server, and issue RPCs # if __name__ == '__main__': server_ip, server_port = get_server_ip_port() print("Using GRPC Server IP(%s) Port(%s)" %(server_ip, server_port)) # Create the channel for gRPC. channel = grpc.insecure_channel(str(server_ip) + ":" + str(server_port)) # Spawn a thread to Initialize the client and listen on notifications # The thread will run in the background global_init(channel) # Send an RPC for VRF registrations vrf_operation(channel, sl_common_types_pb2.SL_REGOP_REGISTER) # RPC EOF to cleanup any previous stale routes vrf_operation(channel, sl_common_types_pb2.SL_REGOP_EOF) # RPC route operations # for add: sl_common_types_pb2.SL_OBJOP_ADD # for update: sl_common_types_pb2.SL_OBJOP_UPDATE # for delete: sl_common_types_pb2.SL_OBJOP_DELETE
import time import grpc from dapr.proto.common.v1 import common_pb2 as commonv1pb from dapr.proto.dapr.v1 import dapr_pb2 as dapr_messages from dapr.proto.dapr.v1 import dapr_pb2_grpc as dapr_services from dapr.proto.daprclient.v1 import daprclient_pb2 as daprclient_messages from dapr.proto.daprclient.v1 import daprclient_pb2_grpc as daprclient_services import proto.response_pb2 as response_messages from google.protobuf.any_pb2 import Any # Start a gRPC client port = os.getenv('DAPR_GRPC_PORT') channel = grpc.insecure_channel(f"localhost:{port}") client = dapr_services.DaprStub(channel) print(f"Started gRPC client on DAPR_GRPC_PORT: {port}") # Our server methods class DaprClientServicer(daprclient_services.DaprClientServicer): def OnInvoke(self, request, context): data = None content_type = "" if request.method == 'my_method': custom_response = response_messages.CustomResponse( isSuccess=True, code=200, message="Hello World - Success!") data = Any() data.Pack(custom_response) else:
import grpc import data_pb2_grpc import data_pb2 channel = grpc.insecure_channel("localhost:6066") stub = data_pb2_grpc.DataHashStub(channel) md5 = data_pb2.Text(data="hello") res = stub.greeting(md5) print(res.data)
def handle(self, intent, mic): """ Once the brain detected the keywords above, it trigger this part """ # Naomi 3.0+ # Check for control inputs try: COLOR = intent['matches']['LEDColorKeyword'][0] except KeyError: COLOR = None try: OPERATION = intent['matches']['LEDOperationKeyword'][0] except KeyError: OPERATION = None try: ACTION = intent['matches']['LEDActionKeyword'][0] except KeyError: ACTION = None # # # # actCrtl = self.NOERROR # Set no error if ACTION is None or ACTION == "NAOMI": # Handle no ACTION input ACTION = 'TURN' if ACTION == 'BLINK': # Set control parameters for blink actCrtl = self.BLINK if COLOR == 'GREEN': colr = self.GREEN elif COLOR == 'RED': colr = self.RED else: # Blink both color LED off-on-off colr = self.BOTH elif ACTION == 'TURN' or ACTION == 'SET': # Set control parameters for change LED state if COLOR == 'GREEN': colr = self.GREEN if OPERATION == "ON" or OPERATION is None: actCrtl = self.ON else: actCrtl = self.OFF elif COLOR == 'RED': colr = self.RED if OPERATION == "ON" or OPERATION is None: actCrtl = self.ON else: actCrtl = self.OFF else: # No colour defined in input colr = self.BOTH if OPERATION == "ON": actCrtl = self.ON elif OPERATION == "OFF": actCrtl = self.OFF else: # Can't detect action default to on actCrtl = self.ON else: actCrtl = self.ERROR # Set error if actCrtl == self.ERROR: mic.say(self.gettext("Error: could not determine action to take!")) else: # print("LED controller: %d, %d" %(actCrtl,colr)) # Debug code # Send LED change control message to server ans_text = "RPi LED Controller" channel = grpc.insecure_channel(serverIP) stub = led_ctlr_pb2_grpc.RPi_CtlrStub(channel) response = stub.SendMsg( led_ctlr_pb2.RPiRequest(name=ans_text, colour=colr, operation=actCrtl)) # Deal with response from server print("LED controller client received: " + response.message) fmt = self.gettext(response.message) mic.say(fmt)
def __init__(self, user_id): self.user_id = int(user_id) # Open a communication channel with UsersService self.channel = grpc.insecure_channel(os.getenv('USER_SERVICE_HOST')) # Creating stub to get data self.stub = user_data_pb2_grpc.GetUserDataStub(self.channel)
def sacct(): conn = grpc.insecure_channel("{}:{}".format(_HOST, _PORT)) client = slurm_pb2_grpc.SlurmctldServiceStub(channel=conn) request = slurm_pb2.CMDRequest(cmd="sacct", type=CMD.SACCT.value) result = client.cmd(request).result print(result)
def main(): channel = grpc.insecure_channel('localhost:8080') client = pi_pb2_grpc.PiCalculatorStub(channel) for i in range(1, 1000): print "pi(%d) = " % i, client.Calc(pi_pb2.PiRequest(n=i)).value
def setUp(self): """ Test setup. """ self.channel = grpc.insecure_channel('localhost:50052') self.stub = SSE.ConnectorStub(self.channel)
response = stub.addTask(task_pb2.TaskDesc(description=desc)) tasks[response.id] = desc return tasks # Test that will be used to grade listTask def test_list(stub, tasks) -> None: response = stub.listTasks(task_pb2.Empty()) for t in response.tasks: # Is the proper task desc is returned for this id? #assert t.desc == tasks[t.id] pass # Test that will be used to grade delTask def test_del(stub, task_ids) -> None: for i in task_ids: stub.delTask(task_pb2.Id(id=i)) if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) with grpc.insecure_channel("localhost:50051") as channel: stub = task_pb2_grpc.TaskapiStub(channel) tasks = test_add(stub, 10) logging.info(f"added tasks {tasks}") test_list(stub, tasks) test_del(stub, tasks.keys())