Giter Site home page Giter Site logo

divergent-protos's Introduction

Divergent proto builds

This is just to validate that python is fine with importing generated gRPC clients from divergent proto directories with overlapping package naming.

  • I used python3 here.
  • I haven't used python in any form, for many many many years, so forgive me for having lost all my pythonic manners.

Setup

Validate that clients can be built for 2 separate and independent proto directories, with overlapping package names. In this case:

  • proto-a
  • proto-b
# ensure we're tidy.
for X in a b; do
    Y="proto-${X}"
    buf lint ${Y}
    buf format -w ${Y}
    tree ${Y}
done;

Build Clients

I haven't written python in a long time, so I'm following the venv instructions here mostly.

virtualenv venv
source venv/bin/activate
python -m pip install --upgrade pip
python -m pip install grpcio
python -m pip install grpcio-tools
python -m pip install ipython

for X in a b; do
    Y="proto-${X}"
    Z="${X}${X}${X}${X}"
    python3 -m grpc_tools.protoc -I${Y} --python_out=. --pyi_out=. --grpc_python_out=. ${Y}/github/canardleteer/${Z}/v1alpha1/service.proto
done;
tree github

Use Clients

I used ipython to explore.

ipython

In another terminal, you can spin up a faux server:

nc -l localhost 50051
import grpc
import importlib.util

# It's been many many years since I've used python, so I assume this could be
# drastically reduced.
import github.canardleteer.aaaa.v1alpha1.service_pb2 as aaaa
spec=importlib.util.spec_from_file_location("github.canardleteer.aaaa.v1alpha1.service_pb2_grpc", "github/canardleteer/aaaa/v1alpha1/service_pb2_grpc.py")
aaaa_grpc = importlib.util.module_from_spec(spec)
spec.loader.exec_module(aaaa_grpc)

import github.canardleteer.bbbb.v1alpha1.service_pb2 as bbbb
spec=importlib.util.spec_from_file_location("github.canardleteer.bbbb.v1alpha1.service_pb2_grpc", "github/canardleteer/bbbb/v1alpha1/service_pb2_grpc.py")
bbbb_grpc = importlib.util.module_from_spec(spec)
spec.loader.exec_module(bbbb_grpc)

# Test Service A.
with grpc.insecure_channel('localhost:50051') as channel:
    stub = aaaa_grpc.SomeATypeServiceStub(channel)
    response = stub.SomethingUnimplemented(aaaa.SomethingUnimplementedRequest())

    # Confirm you see something in your `nc` window.

    # For me, it looks like:
    # -> nc -l localhost 50051
    # PRI * HTTP/2.0
    # 
    # SM
    # 
    # $���@@@?

    # Hit Ctrl-C in ipython, the request will never be responded to.

# Kill your nc process, and relaunch it the same way as before: nc -l localhost 50051

# Test Service B.
with grpc.insecure_channel('localhost:50051') as channel:
    stub = bbbb_grpc.SomeBTypeServiceStub(channel)
    response = stub.SomethingUnimplemented(bbbb.SomethingUnimplementedRequest())

    # Confirm you see something in your `nc` window.

    # For me, it looks like:
    # -> nc -l localhost 50051
    # PRI * HTTP/2.0
    # 
    # SM
    # 
    # $���@@@?

    # Hit Ctrl-C in ipython, the request will never be responded to.

Shrinking

  • I assume you can move all the importlib stuff into something much more convenient.
  • I assume you can also move generated python files after a build, into a flatter directory, if that's wanted.

divergent-protos's People

Contributors

canardleteer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.