grpc python support multithreading on client and server

Viewed 8175

I want to know Is grpc python support multithreading? please share some reference. I have written the code in the single thread but don't know how to write in multithread.

1 Answers

gRPC Python does support multithreading on both client and server. As for server, you will create the server with a thread pool, so it is multithreading in default. As for client, you can create a channel and pass it to multiple Python thread and then create a stub for each thread. Also, since the channel is managed in C instead of Python, create multiple channels to the same server in the same process won't give you any performance advantage.

import threading
import grpc

def worker(channel):
    stub = your_pb2_grpc.YourStub(channel)
    ...YOUR WORK WITH STUB

channel = grpc.insecure_channel(<YOUR PORT>)
your_thread = threading.Thread(target=worker, args=(channel,))
your_thread.start()

Also, if you intent to use multiprocessing instead of multithreading, you can reference here https://github.com/grpc/grpc/issues/16001 and https://github.com/grpc/grpc/blob/master/doc/fork_support.md.

Related