Skip to content

Commit 33c30c1

Browse files
committed
feat: demonstrate python grpc
Ported from https://github.com/chrisirhc/precompiled-grpc-in-bazel-python
1 parent cb688bf commit 33c30c1

File tree

12 files changed

+236
-23
lines changed

12 files changed

+236
-23
lines changed

examples/MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ bazel_dep(name = "platforms", version = "0.0.11")
77
bazel_dep(name = "protobuf", version = "29.3")
88
bazel_dep(name = "rules_java", version = "8.6.3")
99
bazel_dep(name = "rules_proto", version = "7.1.0")
10+
bazel_dep(name = "rules_proto_grpc", version = "5.0.1")
11+
bazel_dep(name = "rules_proto_grpc_python", version = "5.0.1")
1012
bazel_dep(name = "rules_python", version = "1.2.0-rc0")
1113
bazel_dep(name = "rules_rust", version = "0.59.1")
1214
bazel_dep(name = "rules_rust_prost", version = "0.59.1")

examples/proto/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@rules_go//proto:def.bzl", "go_proto_library")
33
load("@rules_proto//proto:defs.bzl", "proto_library")
44
load("@rules_rust_prost//:defs.bzl", "rust_prost_library")
55
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
6+
load("//python:python_grpc_compile.bzl", "python_grpc_compile")
67

78
package(default_visibility = ["//visibility:public"])
89

@@ -17,6 +18,10 @@ py_proto_library(
1718
deps = [":greeter_proto"],
1819
)
1920

21+
python_grpc_compile(
22+
name = "greeter_py_grpc",
23+
protos = [":greeter_proto"],
24+
)
2025
# Broken by https://github.com/protocolbuffers/protobuf/pull/19679
2126
# which causes building C++ code from source.
2227
# TODO: re-enable once protobuf honors the toolchain

examples/python/BUILD

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
load("@rules_python//python:defs.bzl", "py_test")
2-
3-
py_test(
4-
name = "message_test",
5-
srcs = ["message_test.py"],
6-
deps = ["//proto:greeter_py_proto"],
7-
)

examples/python/message_test.py

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Generated definition of python_grpc_compile.
3+
Modified from https://github.com/rules-proto-grpc/rules_proto_grpc/blob/d17b5b16c8b12143c6f1b78dabd6bbc228e89b58/modules/python/python_grpc_compile.bzl
4+
"""
5+
6+
load(
7+
"@rules_proto_grpc//:defs.bzl",
8+
"ProtoPluginInfo",
9+
"proto_compile_attrs",
10+
"proto_compile_impl",
11+
"proto_compile_toolchains",
12+
)
13+
14+
# Create compile rule
15+
python_grpc_compile = rule(
16+
implementation = proto_compile_impl,
17+
attrs = dict(
18+
proto_compile_attrs,
19+
_plugins = attr.label_list(
20+
providers = [ProtoPluginInfo],
21+
default = [
22+
Label("//tools/toolchains:proto_plugin"),
23+
Label("//tools/toolchains:grpc_plugin"),
24+
],
25+
cfg = "exec",
26+
doc = "List of protoc plugins to apply",
27+
),
28+
),
29+
toolchains = proto_compile_toolchains,
30+
)

examples/python/server/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@aspect_rules_py//py:defs.bzl", "py_binary")
2+
3+
py_binary(
4+
name = "server",
5+
srcs = ["__main__.py"],
6+
main = "__main__.py",
7+
deps = [
8+
"//proto:greeter_py_grpc",
9+
"@pypi//grpcio",
10+
],
11+
)

examples/python/server/__main__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from concurrent import futures
2+
import logging
3+
import math
4+
import time
5+
6+
import grpc
7+
from proto import greeter_pb2
8+
9+
class Greeter(greeter_pb2.GreeterStub):
10+
def SayHello(self, request, context):
11+
return greeter_pb2.HelloReply(message=f'Hello {request.name}')
12+
13+
def serve():
14+
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
15+
greeter_pb2.add_Greeter_to_server(Greeter(), server)
16+
server.add_insecure_port("[::1]:5042")
17+
server.start()
18+
server.wait_for_termination()
19+
20+
if __name__ == "__main__":
21+
logging.basicConfig()
22+
serve()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
3+
py_binary(
4+
name = "protoc",
5+
srcs = ["__main__.py"],
6+
main = "__main__.py",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"@pypi//grpcio_tools",
10+
],
11+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import runpy
2+
3+
runpy.run_module('grpc_tools.protoc', run_name='__main__')

examples/tools/requirements.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
protobuf==5.28.0
2+
grpcio
3+
grpcio-tools

0 commit comments

Comments
 (0)