{"id":204315,"date":"2020-11-11T09:30:00","date_gmt":"2020-11-11T01:30:00","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=204315"},"modified":"2020-11-01T17:44:05","modified_gmt":"2020-11-01T09:44:05","slug":"python-grpc-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-grpc-linux.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdPython grpc\u8d85\u65f6\u673a\u5236"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Python grpc\u8d85\u65f6\u673a\u5236\u4ee3\u7801\u793a\u4f8b,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5de5\u4f5c\u4e2d\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u4e0a\u6e38\u670d\u52a1\u901a\u8fc7grpc\u8c03\u7528\u4e0b\u6e38\u670d\u52a1\uff0c\u4f46\u662f\u7531\u4e8e\u4e0b\u6e38\u670d\u52a1\u8d1f\u8f7d\u592a\u9ad8\u5bfc\u81f4\u4e0a\u6e38\u670d\u52a1\u7684\u8c03\u7528\u4f1a\u968f\u673a\u51fa\u73b0\u8d85\u65f6\u7684\u60c5\u51b5\uff0c\u4f46\u662f\u6709\u4e00\u70b9\u4e0d\u592a\u660e\u786e\uff1a\u8d85\u65f6\u4e4b\u540e\uff0c\u4e0b\u6e38\u670d\u52a1\u8fd8\u4f1a\u7ee7\u7eed\u8fdb\u884c\u8ba1\u7b97\u4e48\uff1f<\/p>\n

\u4e8e\u662f\u81ea\u5df1\u5199\u4e86\u4e00\u4e2adamon\u8bd5\u4e86\u4e00\u4e0b\uff1a<\/p>\n

client\uff1a<\/strong><\/span><\/div>\n
\r\n# Copyright 2015 gRPC authors.\r\n#\r\n# Licensed under the Apache License, Version 2.0 (the \"License\");\r\n# you may not use this file except in compliance with the License.\r\n# You may obtain a copy of the License at\r\n#\r\n#   http:\/\/www.apache.org\/licenses\/LICENSE-2.0\r\n#\r\n# Unless required by applicable law or agreed to in writing, software\r\n# distributed under the License is distributed on an \"AS IS\" BASIS,\r\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n# See the License for the specific language governing permissions and\r\n# limitations under the License.\r\n\"\"\"The Python implementation of the GRPC helloworld.Greeter client.\"\"\"\r\n \r\nfrom __future__ import print_function\r\nimport logging\r\n \r\nimport grpc\r\n \r\nimport helloworld_pb2\r\nimport helloworld_pb2_grpc\r\n \r\n \r\ndef run():\r\n  # NOTE(gRPC Python Team): .close() is possible on a channel and should be\r\n  # used in circumstances in which the with statement does not fit the needs\r\n  # of the code.\r\n  with grpc.insecure_channel('localhost:50051') as channel:\r\n    stub = helloworld_pb2_grpc.GreeterStub(channel)\r\n    response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), timeout=30)\r\n  print(\"Greeter client received: \" + response.message)\r\n \r\n \r\nif __name__ == '__main__':\r\n  logging.basicConfig()\r\n  run()<\/pre>\n
server\uff1a<\/strong><\/span><\/div>\n
\r\n# Copyright 2015 gRPC authors.\r\n#\r\n# Licensed under the Apache License, Version 2.0 (the \"License\");\r\n# you may not use this file except in compliance with the License.\r\n# You may obtain a copy of the License at\r\n#\r\n#   http:\/\/www.apache.org\/licenses\/LICENSE-2.0\r\n#\r\n# Unless required by applicable law or agreed to in writing, software\r\n# distributed under the License is distributed on an \"AS IS\" BASIS,\r\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n# See the License for the specific language governing permissions and\r\n# limitations under the License.\r\n\"\"\"The Python implementation of the GRPC helloworld.Greeter server.\"\"\"\r\n \r\nfrom concurrent import futures\r\nimport time\r\nimport logging\r\n \r\nimport grpc\r\n \r\nimport helloworld_pb2\r\nimport helloworld_pb2_grpc\r\n \r\n_ONE_DAY_IN_SECONDS = 60 * 60 * 24\r\n \r\n \r\nclass Greeter(helloworld_pb2_grpc.GreeterServicer):\r\n \r\n  def SayHello(self, request, context):\r\n  count = 0\r\n  while count < 10:\r\n    print('time:%s' % (time.time()))\r\n    time.sleep(5)\r\n    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)\r\n \r\n \r\ndef serve():\r\n  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))\r\n  helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)\r\n  server.add_insecure_port('[::]:50051')\r\n  server.start()\r\n  try:\r\n    while True:\r\n      time.sleep(_ONE_DAY_IN_SECONDS)\r\n  except KeyboardInterrupt:\r\n    server.stop(0)\r\n \r\n \r\nif __name__ == '__main__':\r\n  logging.basicConfig()\r\n  serve()<\/pre>\n

\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u5c31\u662f\u5728grpc\u5b98\u65b9\u63d0\u4f9b\u7684python\u4f8b\u5b50\u4e0a\u505a\u4e86\u4e00\u4e0b\u5c0f\u7684\u6539\u52a8\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f\uff1a\u5f53client\u8d85\u65f6\u62a5\u9519\u9000\u51fa\u4e4b\u540e\uff0cserver\u8fd8\u662f\u4f1a\u7ee7\u7eed\u8fdb\u884c\u8ba1\u7b97\uff0c\u76f4\u5230\u7ed3\u675f\uff0c\u90a3\u5982\u679c\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u8d85\u65f6\u7684\u673a\u5236\u5bf9\u4e8eserver\u6765\u8bf4\u662f\u6ca1\u6709\u4f5c\u7528\u7684\uff0c\u5373\u4f7fclient\u5df2\u7ecf\u4e0d\u518d\u7b49\u5f85\u8fd9\u4e2a\u7ed3\u679c\u4e86\uff0c\u4f46\u662fserver\u8fd8\u662f\u4f1a\u7ee7\u7eed\u8ba1\u7b97\uff0c\u6d6a\u8d39server\u7684\u8d44\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5de5\u4f5c\u4e2d\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u4e0a\u6e38\u670d\u52a1\u901a\u8fc7grpc\u8c03\u7528\u4e0b\u6e38\u670d\u52a1\uff0c\u4f46\u662f\u7531\u4e8e\u4e0b\u6e38\u670d\u52a1\u8d1f\u8f7d\u592a\u9ad8\u5bfc\u81f4\u4e0a\u6e38\u670d\u52a1\u7684\u8c03\u7528\u4f1a\u968f\u673a\u51fa\u73b0\u8d85\u65f6 […]<\/p>\n","protected":false},"author":1482,"featured_media":198116,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-204315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/204315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=204315"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/204315\/revisions"}],"predecessor-version":[{"id":204335,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/204315\/revisions\/204335"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/198116"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=204315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=204315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=204315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}