[Python] When implementing grpc communication with python, it was found that the client did not call the server, and the server had the problem of periodically using the cpu?
- Dominant language
- C++
- Stars
- 45.3k
- Forks
- 11.4k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 47
Description
The grpc version I'm using is 1.66.1, as follows:
Name: grpcio
Version: 1.66.1
Summary: HTTP/2-based RPC framework
Home-page: https://grpc.io
Author: The gRPC Authors
Author-email: grpc-io@googlegroups.com
License: Apache License 2.0
Location: /usr/share/kylin-ai-python-env/python-env/lib/python3.8/site-packages
Requires:
Required-by: grpcio-tools
It was the same when I changed the grpc version to 1.73 and 1.51.1. However, when I implemented grpc(1.16.1) using c++, this problem did not occur.
This has led to a problem of functional wear and tear for us, so we want to know the answer. Below, I will use the demo and the monitoring method
Is used here to three files calculator. Proto, client. Py, server. Py
The demo is as follows:
=======================server.py=============================
```
from concurrent import futures
import grpc
import calculator_pb2
import calculator_pb2_grpc
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):
def Add(self, request, context):
result = request.a + request.b
return calculator_pb2.AddResponse(result=result)
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
calculator_pb2_grpc.add_CalculatorServicer_to_server(CalculatorServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
print("Server started, listening on port 50051")
server.wait_for_termination()
if __name__ == '__main__':
serve()
```
=======================client.py=============================
```
import grpc
import calculator_pb2
import calculator_pb2_grpc
def run():
channel = grpc.insecure_channel('localhost:50051')
stub = calculator_pb2_grpc.CalculatorStub(channel)
# 简单调用
response = stub.Add(calculator_pb2.AddRequest(a=10, b=20))
print(f"10 + 20 = {response.result}")
# 交互式调用
while True:
try:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
response = stub.Add(calculator_pb2.AddRequest(a=a, b=b))
print(f"Result: {response.result}")
except KeyboardInterrupt:
print("\nExiting...")
break
if __name__ == '__main__':
run()
```
=======================calculator.proto=============================
```
syntax = "proto3";
package calculator;
service Calculator {
rpc Add (AddRequest) returns (AddResponse) {}
}
message AddRequest {
int32 a = 1;
int32 b = 2;
}
message AddResponse {
int32 result = 1;
}
```
Monitoring method:
```
pidstat -u-p 1 3600
================================================================
(base) ➜ grpc-demo pidstat -u -p 4018633 1 3600
Linux 5.4.18-142-generic (kylin-pc) 2025年07月08日 _x86_64_ (8 CPU)
15时21分10秒 UID PID %usr %system %guest %wait %CPU CPU Command
15时21分11秒 0 4018633 0.00 0.00 0.00 0.00 0.00 5 python
15时21分12秒 0 4018633 0.00 0.00 0.00 0.00 0.00 3 python
15时21分13秒 0 4018633 0.00 0.00 0.00 0.00 0.00 4 python
15时21分14秒 0 4018633 0.00 0.00 0.00 0.00 0.00 3 python
15时21分15秒 0 4018633 0.00 0.00 0.00 0.00 0.00 4 python
15时21分16秒 0 4018633 0.00 0.00 0.00 0.00 0.00 1 python
15时21分17秒 0 4018633 0.00 0.00 0.00 0.00 0.00 7 python
15时21分18秒 0 4018633 0.00 0.00 0.00 0.00 0.00 7 python
15时21分19秒 0 4018633 0.00 1.00 0.00 0.00 1.00 2 python
15时21分20秒 0 4018633 0.00 0.00 0.00 0.00 0.00 2 python
15时21分21秒 0 4018633 1.00 0.00 0.00 0.00 1.00 3 python
15时21分22秒 0 4018633 0.00 0.00 0.00 0.00 0.00 4 python
15时21分23秒 0 4018633 0.00 0.00 0.00 0.00 0.00 4 python
15时21分24秒 0 4018633 0.00 0.00 0.00 0.00 0.00 3 python
15时21分25秒 0 4018633 0.00 0.00 0.00 0.00 0.00 4 python
15时21分26秒 0 4018633 0.00 0.00 0.00 0.00 0.00 5 python
15时21分27秒 0 4018633 0.00 0.00 0.00 0.00 0.00 3 python
15时21分28秒 0 4018633 0.00 0.00 0.00 0.00 0.00 6 python
15时21分29秒 0 4018633 0.00 0.00 0.00 0.00 0.00 4 python
15时21分30秒 0 4018633 0.00 0.00 0.00 0.00 0.00 5 python
15时21分30秒 UID PID %usr %system %guest %wait %CPU CPU Command
15时21分31秒 0 4018633 0.00 0.00 0.00 0.00 0.00 5 python
15时21分32秒 0 4018633 1.00 0.00 0.00 0.00 1.00 5 python
15时21分33秒 0 4018633 0.00 0.00 0.00 0.00 0.00 7 python
15时21分34秒 0 4018633 0.00 0.00 0.00 0.00 0.00 0 python
15时21分35秒 0 4018633 0.00 0.00 0.00 0.00 0.00 0 python
15时21分36秒 0 4018633 0.00 0.00 0.00 0.00 0.00 1 python
15时21分37秒 0 4018633 0.00 0.00 0.00 0.00 0.00 0 python
15时21分38秒 0 4018633 0.00 0.00 0.00 0.00 0.00 2 python
15时21分39秒 0 4018633 0.00 0.00 0.00 0.00 0.00 0 python
15时21分40秒 0 4018633 0.00 0.00 0.00 0.00 0.00 4 python
15时21分41秒 0 4018633 0.00 0.00 0.00 0.00 0.00 6 python
15时21分42秒 0 4018633 0.00 0.00 0.00 0.00 0.00 5 python
15时21分43秒 0 4018633 0.00 0.00 0.00 0.00 0.00 5 python
15时21分44秒 0 4018633 0.00 0.00 0.00 0.00 0.00 5 python
15时21分45秒 0 4018633 1.00 0.00 0.00 0.00 1.00 7 python
15时21分46秒 0 4018633 0.00 0.00 0.00 0.00 0.00 7 python
15时21分47秒 0 4018633 0.00 0.00 0.00 0.00 0.00 1 python
15时21分48秒 0 4018633 0.00 0.00 0.00 0.00 0.00 1 python
15时21分49秒 0 4018633 0.00 0.00 0.00 0.00 0.00 7 python
15时21分50秒 0 4018633 0.00 0.00 0.00 0.00 0.00 2 python
```
Contributor guide
Assessment
This issue has not been assessed yet.