Skip to content

feat: demonstrate python grpc #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "protobuf", version = "29.3")
bazel_dep(name = "rules_java", version = "8.6.3")
bazel_dep(name = "rules_proto", version = "7.1.0")
bazel_dep(name = "rules_proto_grpc", version = "5.0.1")
bazel_dep(name = "rules_proto_grpc_python", version = "5.0.1")
bazel_dep(name = "rules_python", version = "1.2.0-rc0")
bazel_dep(name = "rules_rust", version = "0.59.1")
bazel_dep(name = "rules_rust_prost", version = "0.59.1")
Expand Down
5 changes: 5 additions & 0 deletions examples/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_rust_prost//:defs.bzl", "rust_prost_library")
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
load("//python:python_grpc_compile.bzl", "python_grpc_compile")

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

Expand All @@ -17,6 +18,10 @@ py_proto_library(
deps = [":greeter_proto"],
)

python_grpc_compile(
name = "greeter_py_grpc",
protos = [":greeter_proto"],
)
# Broken by https://github.com/protocolbuffers/protobuf/pull/19679
# which causes building C++ code from source.
# TODO: re-enable once protobuf honors the toolchain
Expand Down
7 changes: 0 additions & 7 deletions examples/python/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
load("@rules_python//python:defs.bzl", "py_test")

py_test(
name = "message_test",
srcs = ["message_test.py"],
deps = ["//proto:greeter_py_proto"],
)
15 changes: 0 additions & 15 deletions examples/python/message_test.py

This file was deleted.

30 changes: 30 additions & 0 deletions examples/python/python_grpc_compile.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Generated definition of python_grpc_compile.
Modified from https://github.com/rules-proto-grpc/rules_proto_grpc/blob/d17b5b16c8b12143c6f1b78dabd6bbc228e89b58/modules/python/python_grpc_compile.bzl
"""

load(
"@rules_proto_grpc//:defs.bzl",
"ProtoPluginInfo",
"proto_compile_attrs",
"proto_compile_impl",
"proto_compile_toolchains",
)

# Create compile rule
python_grpc_compile = rule(
implementation = proto_compile_impl,
attrs = dict(
proto_compile_attrs,
_plugins = attr.label_list(
providers = [ProtoPluginInfo],
default = [
Label("//tools/toolchains:proto_plugin"),
Label("//tools/toolchains:grpc_plugin"),
],
cfg = "exec",
doc = "List of protoc plugins to apply",
),
),
toolchains = proto_compile_toolchains,
)
11 changes: 11 additions & 0 deletions examples/python/server/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@aspect_rules_py//py:defs.bzl", "py_binary")

py_binary(
name = "server",
srcs = ["__main__.py"],
main = "__main__.py",
deps = [
"//proto:greeter_py_grpc",
"@pypi//grpcio",
],
)
22 changes: 22 additions & 0 deletions examples/python/server/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from concurrent import futures
import logging
import math
import time

import grpc
from proto import greeter_pb2

class Greeter(greeter_pb2.GreeterStub):
def SayHello(self, request, context):
return greeter_pb2.HelloReply(message=f'Hello {request.name}')

def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
greeter_pb2.add_Greeter_to_server(Greeter(), server)
server.add_insecure_port("[::1]:5042")
server.start()
server.wait_for_termination()

if __name__ == "__main__":
logging.basicConfig()
serve()
11 changes: 11 additions & 0 deletions examples/tools/grpcio_tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
name = "protoc",
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//visibility:public"],
deps = [
"@pypi//grpcio_tools",
],
)
3 changes: 3 additions & 0 deletions examples/tools/grpcio_tools/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import runpy

runpy.run_module('grpc_tools.protoc', run_name='__main__')
2 changes: 2 additions & 0 deletions examples/tools/requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
protobuf==5.28.0
grpcio
grpcio-tools
116 changes: 115 additions & 1 deletion examples/tools/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,114 @@
# bazel run @@//tools:generate_requirements_txt
--index-url https://pypi.org/simple

grpcio==1.71.0 \
--hash=sha256:0ab8b2864396663a5b0b0d6d79495657ae85fa37dcb6498a2669d067c65c11ea \
--hash=sha256:0fa05ee31a20456b13ae49ad2e5d585265f71dd19fbd9ef983c28f926d45d0a7 \
--hash=sha256:0ff35c8d807c1c7531d3002be03221ff9ae15712b53ab46e2a0b4bb271f38537 \
--hash=sha256:1be857615e26a86d7363e8a163fade914595c81fec962b3d514a4b1e8760467b \
--hash=sha256:20e8f653abd5ec606be69540f57289274c9ca503ed38388481e98fa396ed0b41 \
--hash=sha256:22c3bc8d488c039a199f7a003a38cb7635db6656fa96437a8accde8322ce2366 \
--hash=sha256:24e867651fc67717b6f896d5f0cac0ec863a8b5fb7d6441c2ab428f52c651c6b \
--hash=sha256:2b85f7820475ad3edec209d3d89a7909ada16caab05d3f2e08a7e8ae3200a55c \
--hash=sha256:39983a9245d37394fd59de71e88c4b295eb510a3555e0a847d9965088cdbd033 \
--hash=sha256:3d081e859fb1ebe176de33fc3adb26c7d46b8812f906042705346b314bde32c3 \
--hash=sha256:469f42a0b410883185eab4689060a20488a1a0a00f8bbb3cbc1061197b4c5a79 \
--hash=sha256:47be9584729534660416f6d2a3108aaeac1122f6b5bdbf9fd823e11fe6fbaa29 \
--hash=sha256:4be74ddeeb92cc87190e0e376dbc8fc7736dbb6d3d454f2fa1f5be1dee26b9d7 \
--hash=sha256:4dd0dfbe4d5eb1fcfec9490ca13f82b089a309dc3678e2edabc144051270a66e \
--hash=sha256:5b08d03ace7aca7b2fadd4baf291139b4a5f058805a8327bfe9aece7253b6d67 \
--hash=sha256:63e41b91032f298b3e973b3fa4093cbbc620c875e2da7b93e249d4728b54559a \
--hash=sha256:652350609332de6dac4ece254e5d7e1ff834e203d6afb769601f286886f6f3a8 \
--hash=sha256:693bc706c031aeb848849b9d1c6b63ae6bcc64057984bb91a542332b75aa4c3d \
--hash=sha256:74258dce215cb1995083daa17b379a1a5a87d275387b7ffe137f1d5131e2cfbb \
--hash=sha256:789d5e2a3a15419374b7b45cd680b1e83bbc1e52b9086e49308e2c0b5bbae6e3 \
--hash=sha256:7c9c80ac6091c916db81131d50926a93ab162a7e97e4428ffc186b6e80d6dda4 \
--hash=sha256:7d6ac9481d9d0d129224f6d5934d5832c4b1cddb96b59e7eba8416868909786a \
--hash=sha256:85da336e3649a3d2171e82f696b5cad2c6231fdd5bad52616476235681bee5b3 \
--hash=sha256:8700a2a57771cc43ea295296330daaddc0d93c088f0a35cc969292b6db959bf3 \
--hash=sha256:8997d6785e93308f277884ee6899ba63baafa0dfb4729748200fcc537858a509 \
--hash=sha256:9182e0063112e55e74ee7584769ec5a0b4f18252c35787f48738627e23a62b97 \
--hash=sha256:9b91879d6da1605811ebc60d21ab6a7e4bae6c35f6b63a061d61eb818c8168f6 \
--hash=sha256:a2242d6950dc892afdf9e951ed7ff89473aaf744b7d5727ad56bdaace363722b \
--hash=sha256:a371e6b6a5379d3692cc4ea1cb92754d2a47bdddeee755d3203d1f84ae08e03e \
--hash=sha256:a76d39b5fafd79ed604c4be0a869ec3581a172a707e2a8d7a4858cb05a5a7637 \
--hash=sha256:ad9f30838550695b5eb302add33f21f7301b882937460dd24f24b3cc5a95067a \
--hash=sha256:b2266862c5ad664a380fbbcdbdb8289d71464c42a8c29053820ee78ba0119e5d \
--hash=sha256:b78a99cd1ece4be92ab7c07765a0b038194ded2e0a26fd654591ee136088d8d7 \
--hash=sha256:c200cb6f2393468142eb50ab19613229dcc7829b5ccee8b658a36005f6669fdd \
--hash=sha256:c30f393f9d5ff00a71bb56de4aa75b8fe91b161aeb61d39528db6b768d7eac69 \
--hash=sha256:c6a0a28450c16809f94e0b5bfe52cabff63e7e4b97b44123ebf77f448534d07d \
--hash=sha256:cebc1b34ba40a312ab480ccdb396ff3c529377a2fce72c45a741f7215bfe8379 \
--hash=sha256:d2c170247315f2d7e5798a22358e982ad6eeb68fa20cf7a820bb74c11f0736e7 \
--hash=sha256:d35a95f05a8a2cbe8e02be137740138b3b2ea5f80bd004444e4f9a1ffc511e32 \
--hash=sha256:d5170929109450a2c031cfe87d6716f2fae39695ad5335d9106ae88cc32dc84c \
--hash=sha256:d6aa986318c36508dc1d5001a3ff169a15b99b9f96ef5e98e13522c506b37eef \
--hash=sha256:d6de81c9c00c8a23047136b11794b3584cdc1460ed7cbc10eada50614baa1444 \
--hash=sha256:dc1a1231ed23caac1de9f943d031f1bc38d0f69d2a3b243ea0d664fc1fbd7fec \
--hash=sha256:e6beeea5566092c5e3c4896c6d1d307fb46b1d4bdf3e70c8340b190a69198594 \
--hash=sha256:e6d8de076528f7c43a2f576bc311799f89d795aa6c9b637377cc2b1616473804 \
--hash=sha256:e6f83a583ed0a5b08c5bc7a3fe860bb3c2eac1f03f1f63e0bc2091325605d2b7 \
--hash=sha256:f250ff44843d9a0615e350c77f890082102a0318d66a99540f54769c8766ab73 \
--hash=sha256:f71574afdf944e6652203cd1badcda195b2a27d9c83e6d88dc1ce3cfb73b31a5 \
--hash=sha256:f903017db76bf9cc2b2d8bdd37bf04b505bbccad6be8a81e1542206875d0e9db \
--hash=sha256:f9a412f55bb6e8f3bb000e020dbc1e709627dcb3a56f6431fa7076b4c1aab0db \
--hash=sha256:f9c30c464cb2ddfbc2ddf9400287701270fdc0f14be5f08a1e3939f1e749b455
# via
# -r tools/requirements.in
# grpcio-tools
grpcio-tools==1.71.0 \
--hash=sha256:072b2a5805ac97e4623b3aa8f7818275f3fb087f4aa131b0fce00471065f6eaa \
--hash=sha256:0ccfb837152b7b858b9f26bb110b3ae8c46675d56130f6c2f03605c4f129be13 \
--hash=sha256:0e647794bd7138b8c215e86277a9711a95cf6a03ff6f9e555d54fdf7378b9f9d \
--hash=sha256:1291a6136c07a86c3bb09f6c33f5cf227cc14956edd1b85cb572327a36e0aef8 \
--hash=sha256:1331e726e08b7bdcbf2075fcf4b47dff07842b04845e6e220a08a4663e232d7f \
--hash=sha256:145985c0bf12131f0a1503e65763e0f060473f7f3928ed1ff3fb0e8aad5bc8ac \
--hash=sha256:192808cf553cedca73f0479cc61d5684ad61f24db7a5f3c4dfe1500342425866 \
--hash=sha256:1f19b16b49afa5d21473f49c0966dd430c88d089cd52ac02404d8cef67134efb \
--hash=sha256:28784f39921d061d2164a9dcda5164a69d07bf29f91f0ea50b505958292312c9 \
--hash=sha256:2a1712f12102b60c8d92779b89d0504e0d6f3a59f2b933e5622b8583f5c02992 \
--hash=sha256:3059c14035e5dc03d462f261e5900b9a077fd1a36976c3865b8507474520bad4 \
--hash=sha256:38dba8e0d5e0fb23a034e09644fdc6ed862be2371887eee54901999e8f6792a8 \
--hash=sha256:41878cb7a75477e62fdd45e7e9155b3af1b7a5332844021e2511deaf99ac9e6c \
--hash=sha256:459c8f5e00e390aecd5b89de67deb3ec7188a274bc6cb50e43cef35ab3a3f45d \
--hash=sha256:48debc879570972d28bfe98e4970eff25bb26da3f383e0e49829b2d2cd35ad87 \
--hash=sha256:541a756276c8a55dec991f6c0106ae20c8c8f5ce8d0bdbfcb01e2338d1a8192b \
--hash=sha256:56ecd6cc89b5e5eed1de5eb9cafce86c9c9043ee3840888cc464d16200290b53 \
--hash=sha256:57e3e2544c306b60ef2d76570bac4e977be1ad548641c9eec130c3bc47e80141 \
--hash=sha256:580ac88141c9815557e63c9c04f5b1cdb19b4db8d0cb792b573354bde1ee8b12 \
--hash=sha256:61c0409d5bdac57a7bd0ce0ab01c1c916728fe4c8a03d77a25135ad481eb505c \
--hash=sha256:64bdb291df61cf570b5256777ad5fe2b1db6d67bc46e55dc56a0a862722ae329 \
--hash=sha256:65aa082f4435571d65d5ce07fc444f23c3eff4f3e34abef599ef8c9e1f6f360f \
--hash=sha256:6693a7d3ba138b0e693b3d1f687cdd9db9e68976c3fa2b951c17a072fea8b583 \
--hash=sha256:682e958b476049ccc14c71bedf3f979bced01f6e0c04852efc5887841a32ad6b \
--hash=sha256:6ae5f2efa9e644c10bf1021600bfc099dfbd8e02b184d2d25dc31fcd6c2bc59e \
--hash=sha256:6d11ed3ff7b6023b5c72a8654975324bb98c1092426ba5b481af406ff559df00 \
--hash=sha256:753270e2d06d37e6d7af8967d1d059ec635ad215882041a36294f4e2fd502b2e \
--hash=sha256:77fe6db1334e0ce318b2cb4e70afa94e0c173ed1a533d37aea69ad9f61ae8ea9 \
--hash=sha256:82c430edd939bb863550ee0fecf067d78feff828908a1b529bbe33cc57f2419c \
--hash=sha256:834959b6eceb85de5217a411aba1643b5f782798680c122202d6a06177226644 \
--hash=sha256:83e90724e3f02415c628e4ead1d6ffe063820aaaa078d9a39176793df958cd5a \
--hash=sha256:870c0097700d13c403e5517cb7750ab5b4a791ce3e71791c411a38c5468b64bd \
--hash=sha256:8b93b9f6adc7491d4c10144c0643409db298e5e63c997106a804f6f0248dbaf4 \
--hash=sha256:8dd9795e982d77a4b496f7278b943c2563d9afde2069cdee78c111a40cc4d675 \
--hash=sha256:8e6cdbba4dae7b37b0d25d074614be9936fb720144420f03d9f142a80be69ba2 \
--hash=sha256:8f987d0053351217954543b174b0bddbf51d45b3cfcf8d6de97b0a43d264d753 \
--hash=sha256:989ee9da61098230d3d4c8f8f8e27c2de796f1ff21b1c90110e636d9acd9432b \
--hash=sha256:9a78d07d6c301a25ef5ede962920a522556a1dfee1ccc05795994ceb867f766c \
--hash=sha256:abd57f615e88bf93c3c6fd31f923106e3beb12f8cd2df95b0d256fa07a7a0a57 \
--hash=sha256:af39e245fa56f7f5c2fe86b7d6c1b78f395c07e54d5613cbdbb3c24769a92b6e \
--hash=sha256:bfe3888c3bbe16a5aa39409bc38744a31c0c3d2daa2b0095978c56e106c85b42 \
--hash=sha256:c1b5860c41a36b26fec4f52998f1a451d0525a5c9a4fb06b6ea3e9211abdb925 \
--hash=sha256:d3adc8b229e60c77bab5a5d62b415667133bd5ced7d59b5f71d6317c9143631e \
--hash=sha256:e3ae9556e2a1cd70e7d7b0e0459c35af71d51a7dae4cf36075068011a69f13ec \
--hash=sha256:e52a041afc20ab2431d756b6295d727bd7adee813b21b06a3483f4a7a15ea15f \
--hash=sha256:edab7e6518de01196be37f96cb1e138c3819986bf5e2a6c9e1519b4d716b2f5a \
--hash=sha256:f360981b215b1d5aff9235b37e7e1826246e35bbac32a53e41d4e990a37b8f4c \
--hash=sha256:f4ad7f0d756546902597053d70b3af2606fbd70d7972876cd75c1e241d22ae00 \
--hash=sha256:f68334d28a267fabec6e70cb5986e9999cfbfd14db654094ddf9aedd804a293a \
--hash=sha256:f7c678e68ece0ae908ecae1c4314a0c2c7f83e26e281738b9609860cc2c82d96 \
--hash=sha256:ffff9bc5eacb34dd26b487194f7d44a3e64e752fc2cf049d798021bf25053b87
# via -r tools/requirements.in
protobuf==5.28.0 \
--hash=sha256:018db9056b9d75eb93d12a9d35120f97a84d9a919bcab11ed56ad2d399d6e8dd \
--hash=sha256:510ed78cd0980f6d3218099e874714cdf0d8a95582e7b059b06cabad855ed0a0 \
Expand All @@ -14,4 +122,10 @@ protobuf==5.28.0 \
--hash=sha256:dde74af0fa774fa98892209992295adbfb91da3fa98c8f67a88afe8f5a349add \
--hash=sha256:dde9fcaa24e7a9654f4baf2a55250b13a5ea701493d904c54069776b99a8216b \
--hash=sha256:eef7a8a2f4318e2cb2dee8666d26e58eaf437c14788f3a2911d0c3da40405ae8
# via -r tools/requirements.in
# via
# -r tools/requirements.in
# grpcio-tools
setuptools==76.1.0 \
--hash=sha256:34750dcb17d046929f545dec9b8349fe42bf4ba13ddffee78428aec422dbfb73 \
--hash=sha256:4959b9ad482ada2ba2320c8f1a8d8481d4d8d668908a7a1b84d987375cd7f5bd
# via grpcio-tools
35 changes: 35 additions & 0 deletions examples/tools/toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain")
load("@rules_rust_prost//:defs.bzl", "rust_prost_toolchain")
load("@rules_proto//proto:proto_toolchain.bzl", "proto_toolchain")
load("@rules_proto_grpc//:defs.bzl", "proto_plugin")

# Configure protoc to have the right arguments for generating Python stubs.
# NB: the protobuf team intends to remove --python_out and instead use a protoc plugin for Python stub emit.
Expand Down Expand Up @@ -34,3 +36,36 @@ toolchain(
toolchain = "prost_toolchain_impl",
toolchain_type = "@rules_rust_prost//:toolchain_type",
)

proto_plugin(
name = "proto_plugin",
exclusions = [
"google/protobuf",
],
outputs = ["{protopath|python}_pb2.py"],
protoc_plugin_name = "python",
visibility = ["//visibility:public"],
)

proto_plugin(
name = "grpc_plugin",
exclusions = [
"google/protobuf",
],
outputs = ["{protopath|python_grpc}_pb2_grpc.py"],
protoc_plugin_name = "grpc_python",
visibility = ["//visibility:public"],
)

proto_toolchain(
name = "prebuilt_protoc_toolchain_with_grpc_plugin",
proto_compiler = "//tools/grpcio_tools:protoc",
)

# https://github.com/aspect-build/toolchains_protoc/blob/e6ce32673fce191c6a0dd4a7752d0fcce73788f7/protoc/private/protoc_toolchains.bzl#L42
toolchain(
name = "prebuilt_protoc_toolchain_with_grpc_plugin_proto_toolchain",
# Note _toolchain suffix
toolchain = ":prebuilt_protoc_toolchain_with_grpc_plugin",
toolchain_type = "@rules_proto//proto:toolchain_type",
)