Skip to content
No description, website, or topics provided.
Go Dockerfile Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
client
proto
server
.gitignore
Dockerfile
README.md

README.md

This repo demonstrates how to write a gRPC server in Go and a client in Python.

Canonical Development Environment

A secondary motivation of this project is to show how to create a canonical development environment for Go and Python programmers using Docker.

Building in Container

We build a Docker image that contains development tools:

  1. The Python interpreter
  2. The Go compiler
  3. The protobuf compiler
  4. The protobuf to Go compiler extension
  5. The protobuf to Python compiler extension

Editing on Host

When we use this Docker image for daily development work, the source code relies on the host computer instead of the container. The source code includes this repo and all its dependencies, for example, the Go package google.golang.org/grpc. Code-on-the-host allows us to run our favorite editors (Emacs, VIM, Eclipse, and more) on the host. Please free to rely on editors add-ons to analyze the source code for auto-completion.

How to Build

Because this repo contains Go code, please make sure that you have the directory structure required by Go. On my laptop computer, I have

export GOPATH=$HOME/go

You could have your $GOPATH pointing to any directory you like.

Given $GOPATH$ set, we could git clone the source code of our project and all its dependencies, including google.golang.org/grpc, by running:

go get github.com/wangkuiyi/canonicalize-go-python-grpc-dev-env

To build this demo, we need the protobuf compiler, Go compiler, Python interpreter, gRPC extension to the protobuf compiler. To ease the installation and configuration of these tools, I provide a Dockerfile to install them into a Docker image. To build the Docker image:

cd $GOPATH/src/github.com/wangkuiyi/canonicalize-go-python-grpc-dev-env
docker build -t grpc .

To run the container, we need to map the $GOPATH directory on the host into the /go directory in the container, because the Dockerfile configures /go as the $GOPATH in the container:

docker run --rm -it \
    -v $PWD:/go/src/github.com/wangkuiyi/canonicalize-go-python-grpc-dev-env \
    -w /go/src/github.com/wangkuiyi/canonicalize-go-python-grpc-dev-env \
    grpc bash

Now, in the container, we can compile the sqlflow.proto in this repo into the Go source code:

protoc -I proto proto/sqlflow.proto --go_out=plugin=grpc:proto

Similarly, we can compile it into Python:

python -m grpc_tools.protoc -I proto --python_out=client --grpc_python_out=client sqlflow.proto

Please be aware that the Go toolchain requires that the generated Go source files in the same directory as the .proto file, which is a separate directory than the server source code, whereas the Python convention is to put generated files with the client source code.

To build the Go server:

cd server
go get -u ./...
go generate
go install
go test

where the go get -u ./... retrieves and updates Go dependencies of our server, go generate invokes the protoc command to translate proto/sqlflow.proto into proto/sqlflow.pb.go, go install builds the server into $GOPATH/bin/server, and go test builds and run unit tests, which runs the gRPC server in a goroutine and the client in another goroutine.

To run the Go server:

$GOPATH/bin/server &

To run the Python client:

cd ../client
python client_test.py
You can’t perform that action at this time.