{"id":317995,"date":"2021-02-05T05:33:45","date_gmt":"2021-02-04T21:33:45","guid":{"rendered":"http:\/\/4563.org\/?p=317995"},"modified":"2021-02-05T05:33:45","modified_gmt":"2021-02-04T21:33:45","slug":"python-async-await-%e7%9a%84%e5%8d%8f%e7%a8%8b-%e6%80%8e%e4%b9%88%e5%8f%96%e5%88%b0%e8%bf%94%e5%9b%9e%e5%80%bc%e5%91%a2%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=317995","title":{"rendered":"Python async\/await \u7684\u534f\u7a0b \u600e\u4e48\u53d6\u5230\u8fd4\u56de\u503c\u5462\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python async\/await \u7684\u534f\u7a0b \u600e\u4e48\u53d6\u5230\u8fd4\u56de\u503c\u5462\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : PPTX <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>async def SendHttpReq(url, method, params=None, data=None,                 headers=None,verify=None, cert=None, json_data=None):      async with httpx.AsyncClient() as client:         if (method == \"GET\"):             response = await client.get(url)     return response <\/code><\/pre>\n<p>\u6bd4\u5982\u6211\u5916\u90e8\u51fd\u6570\u60f3\u8c03\u7528\u8fd9\u4e2a SendHttpReq\uff0c\u53d6\u5230 response \u3002\u8fd9\u4e2a\u4e0d\u80fd\u76f4\u63a5\u50cf\u51fd\u6570\u4e00\u6837 resp = SendHttpReq()\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5093457\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5916\u90e8\u4e5f\u9700\u8981 resp =await SendHttpReq()                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5093458\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : towry <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`py<br \/>import asyncio<\/p>\n<p>resp = asyncio.ensure_future(SendHttpReq(&#8216;url&#8217;, &#8216;GET&#8217;))<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5093459\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sss495088732 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             0.0 \u9898\u5916\u8bdd,httpx,aiohttp \u8fd9\u79cd\u5f02\u6b65 http \u5e93\u90fd\u4f1a\u6709\u5957\u63a5\u5b57\u6ea2\u51fa\u7684\u95ee\u9898.<br \/>\u8bf7\u89c1 requests \u6587\u6863\u4ecb\u7ecd:<br \/>Requests \u552f\u4e00\u7684\u4e00\u4e2a\u975e\u8f6c\u57fa\u56e0\u7684 Python HTTP \u5e93\uff0c\u4eba\u7c7b\u53ef\u4ee5\u5b89\u5168\u4eab\u7528\u3002<\/p>\n<p>\u8b66\u544a\uff1a\u975e\u4e13\u4e1a\u4f7f\u7528\u5176\u4ed6 HTTP \u5e93\u4f1a\u5bfc\u81f4\u5371\u9669\u7684\u526f\u4f5c\u7528\uff0c\u5305\u62ec\uff1a\u5b89\u5168\u7f3a\u9677\u75c7\u3001\u5197\u4f59\u4ee3\u7801\u75c7\u3001\u91cd\u65b0\u53d1\u660e\u8f6e\u5b50\u75c7\u3001\u5543\u6587\u6863\u75c7\u3001\u6291\u90c1\u3001\u5934\u75bc\u3001\u751a\u81f3\u6b7b\u4ea1\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5093460\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : julyclyde <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0a\u9762 await \u548c ensure_future \u7684\u533a\u522b\u5728\u54ea\u65b9\u9762\u5462                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5093461\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : frostming <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @julyclyde ensure_future \u5e76\u4e0d\u80fd\u62ff\u5230\u7ed3\u679c\uff0c\u8fd8\u662f\u5f97 await \u4e00\u4e0b\uff0c\u6240\u4ee5\u6309 1 \u7684\u76f4\u63a5 await \u5c31\u597d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python async\/awai&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317995"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317995"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317995\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317995"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}