![](/img/trans.png)
[英]Why is my python socket client not sending events to my node.js server?
[英]Why does my Node.js gRPC client take 3 seconds to send a request to my Python gRPC server?
让我们首先承认我是一个 gRPC 菜鸟。 如果我问了一个愚蠢的问题,请提前 go 让我知道,但只有在解释了为什么它很愚蠢之后。 谢谢: :)
我正在开发一个处理图像的应用程序。 影响处理结果的变量可由用户更改。 我想在保持跨平台兼容性的同时提供一个好看的 GUI。 因此,我决定在 Python 中实现我的图像处理,并为我的 GUI 使用 Node.js 和 Electron。
需要一种向 Python 后端发送和接收数据的方法,我决定使用 gRPC。 因此,我有一个Node.js gRPC Client与一个Python gRPC Server配对。
在阅读了许多教程并了解了一些关于协议缓冲区的知识后,我能够成功地在两个程序之间传输数据。
Node.js Electron 应用程序接收用户的输入,并向 Python 后端发送请求。 请求的大小是一个小object:
// From My Protocol Buffer
message DetectionSettings {
float lowerThreshold = 1;
float upperThreshold = 2;
float smallestObject = 3;
float largestObject = 4;
float blurAmount = 5;
int64 frameNumber = 6;
string streamSource = 7;
}
收到此请求后,Python 应用程序从指定的streamSource
中读取一个帧并进行处理。 然后将此处理后的帧转换为 JPEG 格式并通过 gRPC 作为Image
返回到 Node.js 应用程序:
// From My Protocol Buffer
message Image {
bytes image_data = 1;
int32 height = 2;
int32 width = 3;
int64 frame = 4;
}
我注意到在发出请求和接收到图像之间存在可变延迟。 在分析 Python 代码之后,这个时间从几毫秒到近 3 秒不等。 我已经确定处理时间可以忽略不计,此外,通过 gRPC 返回Image
所需的时间也可以忽略不计。
因此,在 RPC 执行和 Python 应用程序接收调用之间存在一个有趣的延迟。 以下是一些带有时间的日志,可以更好地解释正在发生的事情(括号中的数字以秒为单位):
/* Node.js */
[160408.072] "Sending Request..."
[160408.072] "Executing RPC"
[160408.072] "RPC Executed"
/* Python */
[160411.032] [ py-backend ] Getting frame
/* Node.js */
[160411.617] "Got Data"
You can see that in this case the time from Node.js RPC execution to the Python method being called was around 3 seconds, while the time from the execution of the Python method to the Node.js application receiving the Image
is less than 1 second 0.o
Python gRPC 服务器
# Create the server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
# Add our service to the server
processor_pb2_grpc.add_ProcessorServicer_to_server(
ProcessorServicer(), server
)
server.add_insecure_port('[::1]:50053')
server.start()
server.wait_for_termination()
Node.js 客户端:
const grpc = require('grpc')
const PROTO_PATH = '../processor.proto'
const serviceConfig = {
"loadBalancingConfig": [ {"round_robin": {} } ]
}
const options = {
'grpc.service_config': JSON.stringify(serviceConfig)
}
const ProcessorService = grpc.load(PROTO_PATH).Processor
const client = new ProcessorService ('[::1]:50053',
grpc.credentials.createInsecure(), options);
module.exports = client
协议缓冲区:
syntax = "proto3";
message Number {
float value = 1;
}
message Image {
bytes image_data = 1;
int32 height = 2;
int32 width = 3;
int64 frame = 4;
}
message DetectionSettings {
float lowerThreshold = 1;
float upperThreshold = 2;
float smallestObject = 3;
float largestObject = 4;
float blurAmount = 5;
int64 frameNumber = 6;
string streamSource = 7;
}
service Processor{
rpc GetFrame(DetectionSettings) returns (Image) {};
}
Node.js gRPC 调用:
function GetFrameBytes(detectionSettings, streamSource, frameNumber, callback)
{
detectionSettings["streamSource"] = streamSource;
detectionSettings["frameNumber"] = frameNumber;
client.GetFrame(detectionSettings, (error, response) =>
{
if(!error){
callback(response)
}
else
{
console.error(error);
}
});
}
为什么我的 Node.js 客户端请求需要这么长时间才能触发我的 Python 代码?
有几个因素可能会影响您观看的时间。
首先,客户端发出的第一个请求总是需要更长的时间,因为它必须进行一些连接设置工作。 一般的期望是单个客户端将发出许多请求,并且设置时间将分摊到所有这些请求中。
其次,您提到您正在使用 Electron。 在 Electron 渲染过程中使用时,有一些关于 gRPC for Node 性能不佳的报告。 在主进程或常规 Node 进程中运行代码时,您可能会看到不同的结果。
如果您尝试 package @grpc/grpc-js
,您可能也会有不同的运气,这是对 Node gRPC 库的完整重新实现。 您当前用于加载 .proto 包的.proto
在该库中不存在,但使用@grpc/proto-loader
的替代方案适用于两种实现,并且其他 API 相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.