{"id":960868,"date":"2024-12-27T03:53:30","date_gmt":"2024-12-26T19:53:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960868.html"},"modified":"2024-12-27T03:53:32","modified_gmt":"2024-12-26T19:53:32","slug":"python%e5%bc%82%e6%ad%a5%e5%a6%82%e4%bd%95%e6%94%af%e6%8c%81rpc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/960868.html","title":{"rendered":"python\u5f02\u6b65\u5982\u4f55\u652f\u6301rpc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103239\/7373c4a8-ebf5-413f-9fb1-5194adb005f7.webp\" alt=\"python\u5f02\u6b65\u5982\u4f55\u652f\u6301rpc\" \/><\/p>\n<p><p> <strong>Python\u5f02\u6b65\u652f\u6301RPC\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528\u5f02\u6b65\u5e93\u5982asyncio\u3001\u7ed3\u5408\u6846\u67b6\u5982gRPC\u3001\u5229\u7528\u6d88\u606f\u961f\u5217\u5982RabbitMQ\u3001\u5b9e\u73b0\u81ea\u5b9a\u4e49\u534f\u8bae\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528gRPC\u7ed3\u5408Python\u7684\u5f02\u6b65\u529f\u80fd\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002gRPC\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff08RPC\uff09\u6846\u67b6\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u5e76\u4e14\u5185\u7f6e\u4e86\u5bf9\u6d41\u5f0f\u4f20\u8f93\u548c\u5f02\u6b65\u8c03\u7528\u7684\u652f\u6301\u3002\u901a\u8fc7gRPC\u7684\u5f02\u6b65\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u63a5\u53e3\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u65b9\u4fbf\u5730\u5b9e\u73b0\u5f02\u6b65RPC\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u7cfb\u7edf\u7684\u5e76\u53d1\u80fd\u529b\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u63cf\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u7ed3\u5408gRPC\u548cPython\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\u6765\u5b9e\u73b0\u9ad8\u6548\u7684RPC\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001ASYNCIO\u4e0eRPC\u7ed3\u5408<\/p>\n<\/p>\n<p><p>asyncio\u662fPython\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\u7684\u6807\u51c6\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u4efb\u52a1\u7b49\u6982\u5ff5\uff0c\u7528\u4e8e\u6784\u5efa\u5e76\u53d1\u7a0b\u5e8f\u3002\u5728\u4f7f\u7528RPC\u65f6\uff0casyncio\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u5f02\u6b65\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e8b\u4ef6\u5faa\u73af\u4e0e\u534f\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u5728asyncio\u4e2d\uff0c\u4e8b\u4ef6\u5faa\u73af\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u6838\u5fc3\u3002\u5b83\u8d1f\u8d23\u8c03\u5ea6\u534f\u7a0b\uff0c\u5e76\u5728\u5b83\u4eec\u7b49\u5f85IO\u64cd\u4f5c\u65f6\u5c06\u63a7\u5236\u6743\u4ea4\u8fd8\u3002\u534f\u7a0b\u662f\u53ef\u4ee5\u6682\u505c\u548c\u6062\u590d\u7684\u51fd\u6570\uff0c\u901a\u5e38\u7528<code>async def<\/code>\u5b9a\u4e49\u3002\u5728RPC\u4e2d\uff0c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u53ef\u4ee5\u5229\u7528\u534f\u7a0b\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u7b80\u5355\u7684RPC\u5ba2\u6237\u7aef\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u534f\u7a0b\u6765\u53d1\u9001\u8bf7\u6c42\u5e76\u7b49\u5f85\u54cd\u5e94\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def rpc_call():<\/p>\n<p>    # \u53d1\u9001\u8bf7\u6c42<\/p>\n<p>    print(&quot;Sending request&quot;)<\/p>\n<p>    aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t asyncio.sleep(1)  # \u6a21\u62df\u7f51\u7edc\u5ef6\u8fdf<\/p>\n<p>    # \u63a5\u6536\u54cd\u5e94<\/p>\n<p>    print(&quot;Received response&quot;)<\/p>\n<p>asyncio.run(rpc_call())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4efb\u52a1\u4e0e\u5e76\u53d1<\/strong><\/p>\n<\/p>\n<p><p>\u4efb\u52a1\u662f\u534f\u7a0b\u7684\u5305\u88c5\u5668\uff0c\u7528\u4e8e\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u8c03\u5ea6\u534f\u7a0b\u3002\u901a\u8fc7\u5c06\u591a\u4e2a\u4efb\u52a1\u63d0\u4ea4\u7ed9\u4e8b\u4ef6\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5e76\u53d1\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u5728RPC\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u63d0\u9ad8\u541e\u5410\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def handle_request(request_id):<\/p>\n<p>    print(f&quot;Handling request {request_id}&quot;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(f&quot;Finished request {request_id}&quot;)<\/p>\n<p>async def main():<\/p>\n<p>    tasks = [asyncio.create_task(handle_request(i)) for i in range(5)]<\/p>\n<p>    await asyncio.gather(*tasks)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001GRPC\u4e0ePYTHON\u5f02\u6b65\u7f16\u7a0b<\/p>\n<\/p>\n<p><p>gRPC\u662f\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684RPC\u6846\u67b6\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u5e76\u4e14\u5185\u7f6e\u4e86\u5bf9\u5f02\u6b65\u7f16\u7a0b\u7684\u652f\u6301\u3002Python\u7684gRPC\u5e93\u63d0\u4f9b\u4e86\u5f02\u6b65\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u63a5\u53e3\uff0c\u53ef\u4ee5\u4e0easyncio\u65e0\u7f1d\u96c6\u6210\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u670d\u52a1\u548c\u6d88\u606f<\/strong><\/p>\n<\/p>\n<p><p>gRPC\u4f7f\u7528Protocol Buffers\u5b9a\u4e49\u670d\u52a1\u548c\u6d88\u606f\u683c\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u9ad8\u6548\uff0c\u8fd8\u80fd\u81ea\u52a8\u751f\u6210\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-proto\">syntax = &quot;proto3&quot;;<\/p>\n<p>service Greeter {<\/p>\n<p>    rpc SayHello (HelloRequest) returns (HelloResponse);<\/p>\n<p>}<\/p>\n<p>message HelloRequest {<\/p>\n<p>    string name = 1;<\/p>\n<p>}<\/p>\n<p>message HelloResponse {<\/p>\n<p>    string message = 1;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u5f02\u6b65\u670d\u52a1\u7aef<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528gRPC\u751f\u6210Python\u4ee3\u7801\u540e\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u5f02\u6b65\u670d\u52a1\u7aef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>from concurrent import futures<\/p>\n<p>import grpc<\/p>\n<p>import greeter_pb2<\/p>\n<p>import greeter_pb2_grpc<\/p>\n<p>class GreeterServicer(greeter_pb2_grpc.GreeterServicer):<\/p>\n<p>    async def SayHello(self, request, context):<\/p>\n<p>        response = greeter_pb2.HelloResponse()<\/p>\n<p>        response.message = f&quot;Hello, {request.name}&quot;<\/p>\n<p>        return response<\/p>\n<p>async def serve():<\/p>\n<p>    server = grpc.aio.server()<\/p>\n<p>    greeter_pb2_grpc.add_GreeterServicer_to_server(GreeterServicer(), server)<\/p>\n<p>    server.add_insecure_port(&#39;[::]:50051&#39;)<\/p>\n<p>    await server.start()<\/p>\n<p>    await server.wait_for_termination()<\/p>\n<p>asyncio.run(serve())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u5f02\u6b65\u5ba2\u6237\u7aef<\/strong><\/p>\n<\/p>\n<p><p>\u5ba2\u6237\u7aef\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u63a5\u53e3\u53d1\u9001\u8bf7\u6c42\u548c\u63a5\u6536\u54cd\u5e94\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import grpc<\/p>\n<p>import greeter_pb2<\/p>\n<p>import greeter_pb2_grpc<\/p>\n<p>async def run():<\/p>\n<p>    async with grpc.aio.insecure_channel(&#39;localhost:50051&#39;) as channel:<\/p>\n<p>        stub = greeter_pb2_grpc.GreeterStub(channel)<\/p>\n<p>        response = await stub.SayHello(greeter_pb2.HelloRequest(name=&#39;World&#39;))<\/p>\n<p>        print(f&quot;Greeter client received: {response.message}&quot;)<\/p>\n<p>asyncio.run(run())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5229\u7528\u6d88\u606f\u961f\u5217\u5982RABBITMQ\u5b9e\u73b0\u5f02\u6b65RPC<\/p>\n<\/p>\n<p><p>\u6d88\u606f\u961f\u5217\u662f\u53e6\u4e00\u79cd\u5b9e\u73b0\u5f02\u6b65RPC\u7684\u65b9\u5f0f\uff0c\u9002\u5408\u9700\u8981\u53ef\u9760\u6d88\u606f\u4f20\u9012\u548c\u590d\u6742\u6d88\u606f\u8def\u7531\u7684\u573a\u666f\u3002RabbitMQ\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6d88\u606f\u961f\u5217\u7cfb\u7edf\uff0c\u652f\u6301\u591a\u79cd\u6d88\u606f\u4f20\u9012\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6d88\u606f\u6a21\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u5728RabbitMQ\u4e2d\uff0c\u6d88\u606f\u901a\u8fc7\u4ea4\u6362\u673a\u8def\u7531\u5230\u961f\u5217\u3002\u5ba2\u6237\u7aef\u53ef\u4ee5\u5411\u961f\u5217\u53d1\u9001\u6d88\u606f\uff0c\u670d\u52a1\u7aef\u4ece\u961f\u5217\u4e2d\u6d88\u8d39\u6d88\u606f\u3002\u5bf9\u4e8eRPC\u6a21\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528\u4e00\u4e2a\u8bf7\u6c42\u961f\u5217\u548c\u591a\u4e2a\u54cd\u5e94\u961f\u5217\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\u4e2d\uff0c\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u6d88\u606f\u5230\u8bf7\u6c42\u961f\u5217\uff0c\u5e76\u6307\u5b9a\u4e00\u4e2a\u7528\u4e8e\u63a5\u6536\u54cd\u5e94\u7684\u4e34\u65f6\u961f\u5217\u3002\u670d\u52a1\u7aef\u5904\u7406\u8bf7\u6c42\u5e76\u5c06\u54cd\u5e94\u53d1\u9001\u56de\u4e34\u65f6\u961f\u5217\u3002<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pika<\/code>\u5e93\u4e0eRabbitMQ\u8fdb\u884c\u4ea4\u4e92\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<p>import uuid<\/p>\n<p>class RpcClient:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.connection = pika.BlockingConnection(pika.ConnectionParameters(&#39;localhost&#39;))<\/p>\n<p>        self.channel = self.connection.channel()<\/p>\n<p>        self.callback_queue = self.channel.queue_declare(queue=&#39;&#39;, exclusive=True).method.queue<\/p>\n<p>        self.channel.basic_consume(queue=self.callback_queue, on_message_callback=self.on_response, auto_ack=True)<\/p>\n<p>        self.response = None<\/p>\n<p>        self.corr_id = None<\/p>\n<p>    def on_response(self, ch, method, props, body):<\/p>\n<p>        if self.corr_id == props.correlation_id:<\/p>\n<p>            self.response = body<\/p>\n<p>    def call(self, n):<\/p>\n<p>        self.corr_id = str(uuid.uuid4())<\/p>\n<p>        self.channel.basic_publish(<\/p>\n<p>            exchange=&#39;&#39;,<\/p>\n<p>            routing_key=&#39;rpc_queue&#39;,<\/p>\n<p>            properties=pika.BasicProperties(reply_to=self.callback_queue, correlation_id=self.corr_id),<\/p>\n<p>            body=str(n)<\/p>\n<p>        )<\/p>\n<p>        while self.response is None:<\/p>\n<p>            self.connection.process_data_events()<\/p>\n<p>        return int(self.response)<\/p>\n<p>rpc_client = RpcClient()<\/p>\n<p>print(f&quot;Requesting fib(30)&quot;)<\/p>\n<p>response = rpc_client.call(30)<\/p>\n<p>print(f&quot;Got {response}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u670d\u52a1\u7aef\u5728\u5904\u7406\u8bf7\u6c42\u65f6\uff0c\u4f1a\u4ece\u8bf7\u6c42\u961f\u5217\u4e2d\u6d88\u8d39\u6d88\u606f\uff0c\u5e76\u5c06\u7ed3\u679c\u53d1\u9001\u5230\u5ba2\u6237\u7aef\u6307\u5b9a\u7684\u54cd\u5e94\u961f\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<p>def on_request(ch, method, props, body):<\/p>\n<p>    n = int(body)<\/p>\n<p>    print(f&quot;Calculating fib({n})&quot;)<\/p>\n<p>    response = fib(n)<\/p>\n<p>    ch.basic_publish(<\/p>\n<p>        exchange=&#39;&#39;,<\/p>\n<p>        routing_key=props.reply_to,<\/p>\n<p>        properties=pika.BasicProperties(correlation_id=props.correlation_id),<\/p>\n<p>        body=str(response)<\/p>\n<p>    )<\/p>\n<p>    ch.basic_ack(delivery_tag=method.delivery_tag)<\/p>\n<p>def fib(n):<\/p>\n<p>    if n == 0:<\/p>\n<p>        return 0<\/p>\n<p>    elif n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return fib(n - 1) + fib(n - 2)<\/p>\n<p>connection = pika.BlockingConnection(pika.ConnectionParameters(&#39;localhost&#39;))<\/p>\n<p>channel = connection.channel()<\/p>\n<p>channel.queue_declare(queue=&#39;rpc_queue&#39;)<\/p>\n<p>channel.basic_qos(prefetch_count=1)<\/p>\n<p>channel.basic_consume(queue=&#39;rpc_queue&#39;, on_message_callback=on_request)<\/p>\n<p>print(&quot;Awaiting RPC requests&quot;)<\/p>\n<p>channel.start_consuming()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408Python\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u4e0e\u4e0d\u540c\u7684RPC\u6846\u67b6\u6216\u673a\u5236\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u8fdc\u7a0b\u8c03\u7528\u3002\u4e0d\u540c\u7684\u65b9\u6848\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5f02\u6b65RPC\u5728Python\u4e2d\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u5f02\u6b65RPC\uff08\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff09\u5728Python\u4e2d\u63d0\u4f9b\u4e86\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\u3002\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u7b49\u5f85\u8fdc\u7a0b\u670d\u52a1\u5668\u54cd\u5e94\u65f6\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u8d44\u6e90\u5229\u7528\u7387\u548c\u54cd\u5e94\u901f\u5ea6\u3002\u8fd9\u79cd\u975e\u963b\u585e\u7279\u6027\u5bf9\u4e8e\u9ad8\u5e76\u53d1\u7684\u5e94\u7528\u573a\u666f\u5c24\u5176\u91cd\u8981\uff0c\u4f8b\u5982Web\u670d\u52a1\u548c\u5fae\u670d\u52a1\u67b6\u6784\u3002\u4f7f\u7528\u5f02\u6b65\u6846\u67b6\uff08\u5982<code>asyncio<\/code>\uff09\u53ef\u4ee5\u4f7f\u5f00\u53d1\u8005\u7f16\u5199\u66f4\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u4ee3\u7801\uff0c\u540c\u65f6\u51cf\u5c11\u4e86\u4f20\u7edf\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u590d\u6742\u6027\u548c\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5f02\u6b65RPC\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5b9e\u73b0\u5f02\u6b65RPC\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>aiohttp<\/code>\u6216<code>grpc.aio<\/code>\u7b49\u5e93\u3002<code>aiohttp<\/code>\u53ef\u7528\u4e8e\u6784\u5efa\u5f02\u6b65HTTP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\uff0c\u652f\u6301\u4f7f\u7528<code>async<\/code>\u548c<code>await<\/code>\u8bed\u6cd5\u8fdb\u884c\u5f02\u6b65\u8c03\u7528\u3002\u800c\u4f7f\u7528<code>grpc.aio<\/code>\u5219\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5b9e\u73b0\u57fa\u4e8egRPC\u7684\u5f02\u6b65\u901a\u4fe1\u3002\u901a\u8fc7\u5b9a\u4e49\u670d\u52a1\u534f\u8bae\u5e76\u751f\u6210\u76f8\u5e94\u7684Python\u4ee3\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u9ad8\u6548\u7684\u5f02\u6b65RPC\u670d\u52a1\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u5f02\u6b65RPC\u65f6\u5982\u4f55\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\uff1f<\/strong><br \/>\u5904\u7406\u5f02\u6b65RPC\u4e2d\u7684\u9519\u8bef\u548c\u5f02\u5e38\u662f\u786e\u4fdd\u5e94\u7528\u7a33\u5b9a\u8fd0\u884c\u7684\u91cd\u8981\u73af\u8282\u3002\u5bf9\u4e8e\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff0c\u4f8b\u5982\u8fde\u63a5\u8d85\u65f6\u6216\u670d\u52a1\u5668\u9519\u8bef\u3002\u5efa\u8bae\u5728\u6bcf\u4e2aRPC\u8c03\u7528\u4e2d\u5b9e\u73b0\u91cd\u8bd5\u673a\u5236\uff0c\u4ee5\u5e94\u5bf9\u4e34\u65f6\u7f51\u7edc\u95ee\u9898\u3002\u6b64\u5916\uff0c\u5408\u7406\u7684\u65e5\u5fd7\u8bb0\u5f55\u4e5f\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u8ffd\u8e2a\u548c\u5206\u6790\u9519\u8bef\uff0c\u53ca\u65f6\u505a\u51fa\u8c03\u6574\uff0c\u63d0\u5347\u7cfb\u7edf\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5f02\u6b65\u652f\u6301RPC\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528\u5f02\u6b65\u5e93\u5982asyncio\u3001\u7ed3\u5408\u6846\u67b6\u5982gRPC\u3001\u5229\u7528\u6d88\u606f\u961f\u5217\u5982Rabbi [&hellip;]","protected":false},"author":3,"featured_media":960872,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960868"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=960868"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960868\/revisions"}],"predecessor-version":[{"id":960873,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960868\/revisions\/960873"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960872"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}