1 year ago

#320783

test-img

jsofri

how exactly does gRPC for python handles unix domain sockets

After trying a few things, I got to a point where I can run client-server gRPC connection that is UDS based on the same host. Since I didn't find any detailed documentation for this setup, my main concern is that I don't know if I use gRPC API properly.
Any reference/resource/explanation would be warmly welcomed

sources I looked at:
gRPC python API
gRPC python docs
gRPC server in Python with Unix domain socket (SO)

gRPC connection:

server
import grpc
import my_pb2_grpc
from concurrent import futures

server = None
#...
class Listener(my_pb2_grpc.MyServiceServicer):
  #...

def create_connection():
    if server is None:
        server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
        my_pb2_grpc.add_MyServiceServicer_to_server(Listener(), server)
        server.add_insecure_port("unix:///path/to/uds.sock")
        server.start()

def stop_connection():
    if server:
        server.stop(0)
        server = None
client
import grpc
import my_pb2_grpc

channel = None
stub = None
#...

def create_connection():
    if channel is None:
        channel = grpc.insecure_channel("unix:///path/to/uds.sock")
        stub = my_pb2_grpc.MyServiceStub(channel)

def stop_connection():
    if channel:
        channel.close()
        channel = None
        stub = None

python

sockets

grpc

unix-socket

grpc-python

0 Answers

Your Answer

Accepted video resources