Skip to content

Instantly share code, notes, and snippets.

@ivankra
Last active July 22, 2024 02:00
Show Gist options
  • Save ivankra/04f11961f7953549a45a82fc13ef3866 to your computer and use it in GitHub Desktop.
Save ivankra/04f11961f7953549a45a82fc13ef3866 to your computer and use it in GitHub Desktop.
Minimal example of a python grpc server built with bazel.
$ bazel build ...
$ bazel run :greeter_server
Listening on :50051
$ bazel run :greeter_client
Greeter client received: Hello, you!
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@com_github_grpc_grpc//bazel:python_rules.bzl", "py_grpc_library", "py_proto_library")
proto_library(
name = "helloworld_proto",
srcs = ["helloworld.proto"],
)
py_proto_library(
name = "helloworld_py_proto",
deps = [":helloworld_proto"],
)
py_grpc_library(
name = "helloworld_py_grpc",
srcs = [":helloworld_proto"],
deps = [":helloworld_py_proto"],
)
py_binary(
name = "greeter_server",
srcs = ["greeter_server.py"],
deps = [":helloworld_py_grpc"],
)
py_binary(
name = "greeter_client",
srcs = ["greeter_client.py"],
deps = [":helloworld_py_grpc"],
)
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Python implementation of the GRPC helloworld.Greeter client."""
from __future__ import print_function
import logging
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
def run():
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
# used in circumstances in which the with statement does not fit the needs
# of the code.
with grpc.insecure_channel('localhost:50051') as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print('Greeter client received: ' + response.message)
if __name__ == '__main__':
logging.basicConfig()
run()
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Python implementation of the GRPC helloworld.Greeter server."""
from concurrent import futures
import logging
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
print('Request from %s' % request.name)
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051')
server.start()
print('Listening on :50051')
server.wait_for_termination()
if __name__ == '__main__':
logging.basicConfig()
serve()
syntax = "proto3";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
workspace(name = "hello")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
http_archive(
name = "com_github_grpc_grpc",
sha256 = "51403542b19e9ed5d3b6551ce4a828e17883a1593d4ca408b098f04b0767d202",
strip_prefix = "grpc-1.36.2",
urls = ["https://github.com/grpc/grpc/archive/v1.36.2.tar.gz"],
)
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
grpc_extra_deps()
@peggyxia-alj
Copy link

I failed to build this using bazel 6.3.1 on mac. Got this error: Error: 'apple_common' value has no field or method 'objc_proto_aspect'

I added the build_bazel_rules_apple, fail with new error like:
external/com_google_absl/absl/meta/type_traits.h:293:36: error: builtin __has_trivial_destructor is deprecated; use __is_trivially_destructible instead [-Werror,-Wdeprecated-builtins]
: std::integral_constant<bool, __has_trivial_destructor(T) &&

Can you provide more information about which platform and which bazel version this can build successfully?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment