我刚开始使用gRPC,在阅读了入门指南之后,我尝试实现一个简单的python服务。但是当我调用我的客户机调用时,python要求提供一个上下文参数。为什么我的代码需要在示例中不需要上下文对象时提供它?在
另外,我开始尝试创建自己的具体上下文子类,但不确定它应该如何实现。我已经添加了我的开始,但如果可能的话,我会非常感谢你的一个例子
谢谢!在
原型文件
syntax = "proto2";
package parsefile;
service ParseFile {
rpc SendFile (File) returns (Empty) {}
}
message File {
message MetaData {
optional string file_name = 1;
optional string file_path = 2 [default = '.'];
optional string mime_type = 3 [default = 'application/pdf'];
}
message Source {
optional string title = 1;
optional int32 id = 2;
}
optional MetaData document = 1;
optional Source supplier = 2;
}
message Empty {
}
服务器
^{pr2}$客户
import grpc
import parsefile_pb2_grpc
import parsefile_pb2
def get_file_info():
return parsefile_pb2.File(
document = parsefile_pb2.File.MetaData(
file_name = 'example.txt'
),
supplier = parsefile_pb2.File.Source(
title = 'Example Supplier'
)
)
def run():
channel = grpc.insecure_channel('localhost:50051')
stub = parsefile_pb2_grpc.ParseFileStub(channel)
context = RequestContext()
print('object created')
response = stub.SendFile(get_file_info())
print('File info sent to server')
if __name__ == '__main__':
run()
错误跟踪
Traceback (most recent call last): File "parse_client.py", line 60, in <module>
run() File "parse_client.py", line 56, in run
response = stub.SendFile(get_file_info(), 2) File "/Users/davidbowe/.virtualenvs/post/lib/python3.6/site-packages/grpc/_channel.py", line 507, in __call__
return _end_unary_response_blocking(state, call, False, deadline) File "/Users/davidbowe/.virtualenvs/post/lib/python3.6/site-packages/grpc/_channel.py", line 455, in _end_unary_response_bl ocking
raise _Rendezvous(state, None, None, deadline) grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNKNOWN, Exception calling application: SendF ile() missing 1 required positional argument: 'context')>
您不需要创建
context
参数,它是由grpc自动创建的。在在服务器代码中,缺少用于设置服务程序对象的括号。你写了。。。在
它应该是:
^{pr2}$相关问题 更多 >
编程相关推荐