質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
gRPC

gRPCは、グーグル社が開発した通信プロトコルの一つ。Protocol Buffersを用いてシリアライズしバイナリに変換させるため、高速なRPCを実現することができます。また、プログラマは意識せずにHTTP/2を利用できることも特徴です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

3757閲覧

Pythonを使ったgRPCにおけるエラーについて

buru7052

総合スコア5

gRPC

gRPCは、グーグル社が開発した通信プロトコルの一つ。Protocol Buffersを用いてシリアライズしバイナリに変換させるため、高速なRPCを実現することができます。また、プログラマは意識せずにHTTP/2を利用できることも特徴です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

2クリップ

投稿2021/04/08 08:26

編集2021/04/08 08:28

質問した経緯

gRPCの公式ドキュメントに載っているPythonのプログラムについてエラーが発生しました。
いろいろ調べたのですが解決策がわからず、ご教授お願い致します。

前提条件(環境)

  • Windows10
  • VisualStudioCode
  • Python3.8.8
  • Anaconda(仮想環境)

エラーが発生する流れ

gRPCの公式ドキュメントの流れに沿って実施

  1. grpcioのインストール
  2. gRPCツールのインストール
  3. サンプルプログラムをGitからダウンロード
  4. VisualStudioCodeのターミナルからgRPCアプリケーション(サーバー)を実行
    python greeter_server.py
  5. 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()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問