質問した経緯
gRPCの公式ドキュメントに載っているPythonのプログラムについてエラーが発生しました。
いろいろ調べたのですが解決策がわからず、ご教授お願い致します。
前提条件(環境)
- Windows10
- VisualStudioCode
- Python3.8.8
- Anaconda(仮想環境)
エラーが発生する流れ
gRPCの公式ドキュメントの流れに沿って実施
- grpcioのインストール
- gRPCツールのインストール
- サンプルプログラムをGitからダウンロード
- VisualStudioCodeのターミナルからgRPCアプリケーション(サーバー)を実行
python greeter_server.py
- VisualStudioCodeから別のターミナルを立ち上げてgRPCアプリケーション(クライアント)を実行
python greeter_client.py
5.の実行結果として、下記のエラーとなってしまいます。
console
1(py38) PS C:\Users\testuser.conda\virtualenv\grpc\examples\python\helloworld>python .\greeter_client.py 2Traceback (most recent call last): 3 File ".\greeter_client.py", line 38, in <module> 4 run() 5 File ".\greeter_client.py", line 32, in run 6 response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) 7 File "C:\Users\testuser\Anaconda3\envs\py38\lib\site-packages\grpc\_channel.py", line 923, in __call__ 8 return _end_unary_response_blocking(state, call, False, None) 9 File "C:\Users\testuser\Anaconda3\envs\py38\lib\site-packages\grpc\_channel.py", line 826, in _end_unary_response_blocking 10 raise _InactiveRpcError(state) 11grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: 12 status = StatusCode.UNAVAILABLE 13 details = "failed to connect to all addresses" 14 debug_error_string = "{"created":"@1617865971.134000000","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc"t_channel.cc","file_line":5391,"referenced_errors":[{"created":"@1617865971.134000000","description":"failed to connect to all addresses","file":"src/core/eient_channel/,"file_line":5391,"referenced_errors":[{"created":"@1617865971.134000000","description":"failxt/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":398,"grpc_status":14}]}"
調べたこと
- failed to connect to all addressesと出ているため、pythonコードで開けたポート(自身の端末)に対して、疎通確認
Test-NetConnection localhost -Port 50051
- 結果
console
1ComputerName : localhost 2RemoteAddress : ::1 3RemotePort : 50051 4InterfaceAlias : Loopback Pseudo-Interface 1 5SourceAddress : ::1 6TcpTestSucceeded : True
TcpTestSucceeded の結果がTrueになっているため、通信はできると思ってます。
ソースコード
- Gitからダウンロードしたコードそのまま
- greeter_server.py
python:
1# Copyright 2015 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""The Python implementation of the GRPC helloworld.Greeter server.""" 15 16from concurrent import futures 17import logging 18 19import grpc 20 21import helloworld_pb2 22import helloworld_pb2_grpc 23 24 25class Greeter(helloworld_pb2_grpc.GreeterServicer): 26 27 def SayHello(self, request, context): 28 return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) 29 30 31def serve(): 32 server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) 33 helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) 34 server.add_insecure_port('[::]:50051') 35 server.start() 36 server.wait_for_termination() 37 38 39if __name__ == '__main__': 40 logging.basicConfig() 41 serve()
- greeter_client.py
python:
1# Copyright 2015 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""The Python implementation of the GRPC helloworld.Greeter client.""" 15 16from __future__ import print_function 17import logging 18 19import grpc 20 21 22import helloworld_pb2 23import helloworld_pb2_grpc 24 25 26def run(): 27 # NOTE(gRPC Python Team): .close() is possible on a channel and should be 28 # used in circumstances in which the with statement does not fit the needs 29 # of the code. 30 with grpc.insecure_channel('localhost:50051') as channel: 31 stub = helloworld_pb2_grpc.GreeterStub(channel) 32 response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) 33 print("Greeter client received: " + response.message) 34 35 36if __name__ == '__main__': 37 logging.basicConfig() 38 run()
あなたの回答
tips
プレビュー