{"id":1650,"date":"2020-06-22T01:20:05","date_gmt":"2020-06-21T17:20:05","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1650"},"modified":"2020-06-22T01:20:05","modified_gmt":"2020-06-21T17:20:05","slug":"python-%e5%bc%82%e6%ad%a5-io","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1650","title":{"rendered":"Python \u5f02\u6b65 IO"},"content":{"rendered":"<div>\n<h1>\u5f02\u6b65IO<\/h1>\n<p>CPU\u7684\u901f\u5ea6\u8fdc\u8fdc\u5feb\u4e8e\u78c1\u76d8\u3001\u7f51\u7edc\u7b49IO\u3002\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\uff0cCPU\u6267\u884c\u4ee3\u7801\u7684\u901f\u5ea6\u6781\u5feb\uff0c\u7136\u800c\uff0c\u4e00\u65e6\u9047\u5230IO\u64cd\u4f5c\uff0c\u5982\u8bfb\u5199\u6587\u4ef6\u3001\u53d1\u9001\u7f51\u7edc\u6570\u636e\u65f6\uff0c\u5c31\u9700\u8981\u7b49\u5f85IO\u64cd\u4f5c\u5b8c\u6210\uff0c\u624d\u80fd\u7ee7\u7eed\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c\u3002\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u540c\u6b65IO\u3002<br \/>\n\u5728IO\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5f53\u524d\u7ebf\u7a0b\u88ab\u6302\u8d77\uff0c\u800c\u5176\u4ed6\u9700\u8981CPU\u6267\u884c\u7684\u4ee3\u7801\u5c31\u65e0\u6cd5\u88ab\u5f53\u524d\u7ebf\u7a0b\u6267\u884c\u4e86\u3002<br \/>\n\u56e0\u4e3a\u4e00\u4e2aIO\u64cd\u4f5c\u5c31\u963b\u585e\u4e86\u5f53\u524d\u7ebf\u7a0b\uff0c\u5bfc\u81f4\u5176\u4ed6\u4ee3\u7801\u65e0\u6cd5\u6267\u884c\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u8005\u591a\u8fdb\u7a0b\u6765\u5e76\u53d1\u6267\u884c\u4ee3\u7801\uff0c\u4e3a\u591a\u4e2a\u7528\u6237\u670d\u52a1\u3002\u6bcf\u4e2a\u7528\u6237\u90fd\u4f1a\u5206\u914d\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5982\u679c\u9047\u5230IO\u5bfc\u81f4\u7ebf\u7a0b\u88ab\u6302\u8d77\uff0c\u5176\u4ed6\u7528\u6237\u7684\u7ebf\u7a0b\u4e0d\u53d7\u5f71\u54cd\u3002<br \/>\n\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7684\u6a21\u578b\u867d\u7136\u89e3\u51b3\u4e86\u5e76\u53d1\u95ee\u9898\uff0c\u4f46\u662f\u7cfb\u7edf\u4e0d\u80fd\u65e0\u4e0a\u9650\u5730\u589e\u52a0\u7ebf\u7a0b\u3002\u7531\u4e8e\u7cfb\u7edf\u5207\u6362\u7ebf\u7a0b\u7684\u5f00\u9500\u4e5f\u5f88\u5927\uff0c\u6240\u4ee5\uff0c\u4e00\u65e6\u7ebf\u7a0b\u6570\u91cf\u8fc7\u591a\uff0cCPU\u7684\u65f6\u95f4\u5c31\u82b1\u5728\u7ebf\u7a0b\u5207\u6362\u4e0a\u4e86\uff0c\u771f\u6b63\u8fd0\u884c\u4ee3\u7801\u7684\u65f6\u95f4\u5c31\u5c11\u4e86\uff0c\u7ed3\u679c\u5bfc\u81f4\u6027\u80fd\u4e25\u91cd\u4e0b\u964d\u3002<br \/>\n\u7531\u4e8e\u6211\u4eec\u8981\u89e3\u51b3\u7684\u95ee\u9898\u662fCPU\u9ad8\u901f\u6267\u884c\u80fd\u529b\u548cIO\u8bbe\u5907\u7684\u9f9f\u901f\u4e25\u91cd\u4e0d\u5339\u914d\uff0c\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u53ea\u662f\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<br \/>\n\u53e6\u4e00\u79cd\u89e3\u51b3IO\u95ee\u9898\u7684\u65b9\u6cd5\u662f\u5f02\u6b65IO\u3002\u5f53\u4ee3\u7801\u9700\u8981\u6267\u884c\u4e00\u4e2a\u8017\u65f6\u7684IO\u64cd\u4f5c\u65f6\uff0c\u5b83\u53ea\u53d1\u51faIO\u6307\u4ee4\uff0c\u5e76\u4e0d\u7b49\u5f85IO\u7ed3\u679c\uff0c\u7136\u540e\u5c31\u53bb\u6267\u884c\u5176\u4ed6\u4ee3\u7801\u4e86\u3002\u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u5f53IO\u8fd4\u56de\u7ed3\u679c\u65f6\uff0c\u518d\u901a\u77e5CPU\u8fdb\u884c\u5904\u7406\u3002<br \/>\n\u53ef\u4ee5\u60f3\u8c61\u5982\u679c\u6309\u666e\u901a\u987a\u5e8f\u5199\u51fa\u7684\u4ee3\u7801\u5b9e\u9645\u4e0a\u662f\u6ca1\u6cd5\u5b8c\u6210\u5f02\u6b65IO\u7684\uff0c\u5f02\u6b65IO\u6a21\u578b\u9700\u8981\u4e00\u4e2a\u6d88\u606f\u5faa\u73af\uff0c\u5728\u6d88\u606f\u5faa\u73af\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u4e0d\u65ad\u5730\u91cd\u590d\u201c\u8bfb\u53d6\u6d88\u606f-\u5904\u7406\u6d88\u606f\u201d\u8fd9\u4e00\u8fc7\u7a0b\u3002<br \/>\n\u5728\u201c\u53d1\u51faIO\u8bf7\u6c42\u201d\u5230\u6536\u5230\u201cIO\u5b8c\u6210\u201d\u7684\u8fd9\u6bb5\u65f6\u95f4\u91cc\uff0c\u540c\u6b65IO\u6a21\u578b\u4e0b\uff0c\u4e3b\u7ebf\u7a0b\u53ea\u80fd\u6302\u8d77\uff0c\u4f46\u5f02\u6b65IO\u6a21\u578b\u4e0b\uff0c\u4e3b\u7ebf\u7a0b\u5e76\u6ca1\u6709\u4f11\u606f\uff0c\u800c\u662f\u5728\u6d88\u606f\u5faa\u73af\u4e2d\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u6d88\u606f\u3002\u8fd9\u6837\uff0c\u5728\u5f02\u6b65IO\u6a21\u578b\u4e0b\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2aIO\u8bf7\u6c42\uff0c\u5e76\u4e14\u6ca1\u6709\u5207\u6362\u7ebf\u7a0b\u7684\u64cd\u4f5c\u3002\u5bf9\u4e8e\u5927\u591a\u6570IO\u5bc6\u96c6\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u7528\u5f02\u6b65IO\u5c06\u5927\u5927\u63d0\u5347\u7cfb\u7edf\u7684\u591a\u4efb\u52a1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<h2>\u534f\u7a0b<\/h2>\n<p>\u534f\u7a0b\uff0c\u53c8\u79f0\u5fae\u7ebf\u7a0b\uff0c\u7ea4\u7a0b\u3002\u82f1\u6587\u540dCoroutine\u3002<br \/>\n\u5b50\u7a0b\u5e8f\uff0c\u6216\u8005\u79f0\u4e3a\u51fd\u6570\uff0c\u5728\u6240\u6709\u8bed\u8a00\u4e2d\u90fd\u662f\u5c42\u7ea7\u8c03\u7528\uff0c\u6bd4\u5982A\u8c03\u7528B\uff0cB\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u53c8\u8c03\u7528\u4e86C\uff0cC\u6267\u884c\u5b8c\u6bd5\u8fd4\u56de\uff0cB\u6267\u884c\u5b8c\u6bd5\u8fd4\u56de\uff0c\u6700\u540e\u662fA\u6267\u884c\u5b8c\u6bd5\u3002<br \/>\n\u6240\u4ee5\u5b50\u7a0b\u5e8f\u8c03\u7528\u662f\u901a\u8fc7\u6808\u5b9e\u73b0\u7684\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5c31\u662f\u6267\u884c\u4e00\u4e2a\u5b50\u7a0b\u5e8f\u3002<br \/>\n\u5b50\u7a0b\u5e8f\u8c03\u7528\u603b\u662f\u4e00\u4e2a\u5165\u53e3\uff0c\u4e00\u6b21\u8fd4\u56de\uff0c\u8c03\u7528\u987a\u5e8f\u662f\u660e\u786e\u7684\u3002\u800c\u534f\u7a0b\u7684\u8c03\u7528\u548c\u5b50\u7a0b\u5e8f\u4e0d\u540c\u3002<br \/>\n\u534f\u7a0b\u770b\u4e0a\u53bb\u4e5f\u662f\u5b50\u7a0b\u5e8f\uff0c\u4f46\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u5728\u5b50\u7a0b\u5e8f\u5185\u90e8\u53ef\u4e2d\u65ad\uff0c\u7136\u540e\u8f6c\u800c\u6267\u884c\u522b\u7684\u5b50\u7a0b\u5e8f\uff0c\u5728\u9002\u5f53\u7684\u65f6\u5019\u518d\u8fd4\u56de\u6765\u63a5\u7740\u6267\u884c\u3002<br \/>\n\u6ce8\u610f\uff0c\u5728\u4e00\u4e2a\u5b50\u7a0b\u5e8f\u4e2d\u4e2d\u65ad\uff0c\u53bb\u6267\u884c\u5176\u4ed6\u5b50\u7a0b\u5e8f\uff0c\u4e0d\u662f\u51fd\u6570\u8c03\u7528\uff0c\u6709\u70b9\u7c7b\u4f3cCPU\u7684\u4e2d\u65ad\u3002\u6bd4\u5982\u5b50\u7a0b\u5e8fA\u3001B\uff0c\u4f46\u662f\u5728A\u4e2d\u662f\u6ca1\u6709\u8c03\u7528B\u7684\uff0c\u6240\u4ee5\u534f\u7a0b\u7684\u8c03\u7528\u6bd4\u51fd\u6570\u8c03\u7528\u7406\u89e3\u8d77\u6765\u8981\u96be\u4e00\u4e9b\u3002<br \/>\n\u770b\u8d77\u6765A\u3001B\u7684\u6267\u884c\u6709\u70b9\u50cf\u591a\u7ebf\u7a0b\uff0c\u4f46\u534f\u7a0b\u7684\u7279\u70b9\u5728\u4e8e\u662f\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\uff0c\u90a3\u548c\u591a\u7ebf\u7a0b\u6bd4\uff0c\u534f\u7a0b\u6709\u4f55\u4f18\u52bf\uff1f<br \/>\n\u6700\u5927\u7684\u4f18\u52bf\u5c31\u662f\u534f\u7a0b\u6781\u9ad8\u7684\u6267\u884c\u6548\u7387\u3002\u56e0\u4e3a\u5b50\u7a0b\u5e8f\u5207\u6362\u4e0d\u662f\u7ebf\u7a0b\u5207\u6362\uff0c\u800c\u662f\u7531\u7a0b\u5e8f\u81ea\u8eab\u63a7\u5236\uff0c\u56e0\u6b64\uff0c\u6ca1\u6709\u7ebf\u7a0b\u5207\u6362\u7684\u5f00\u9500\uff0c\u548c\u591a\u7ebf\u7a0b\u6bd4\uff0c\u7ebf\u7a0b\u6570\u91cf\u8d8a\u591a\uff0c\u534f\u7a0b\u7684\u6027\u80fd\u4f18\u52bf\u5c31\u8d8a\u660e\u663e\u3002<br \/>\n\u7b2c\u4e8c\u5927\u4f18\u52bf\u5c31\u662f\u4e0d\u9700\u8981\u591a\u7ebf\u7a0b\u7684\u9501\u673a\u5236\uff0c\u56e0\u4e3a\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4e5f\u4e0d\u5b58\u5728\u540c\u65f6\u5199\u53d8\u91cf\u51b2\u7a81\uff0c\u5728\u534f\u7a0b\u4e2d\u63a7\u5236\u5171\u4eab\u8d44\u6e90\u4e0d\u52a0\u9501\uff0c\u53ea\u9700\u8981\u5224\u65ad\u72b6\u6001\u5c31\u597d\u4e86\uff0c\u6240\u4ee5\u6267\u884c\u6548\u7387\u6bd4\u591a\u7ebf\u7a0b\u9ad8\u5f88\u591a\u3002<br \/>\n\u56e0\u4e3a\u534f\u7a0b\u662f\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\uff0c\u90a3\u600e\u4e48\u5229\u7528\u591a\u6838CPU\u5462\uff1f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u591a\u8fdb\u7a0b+\u534f\u7a0b\uff0c\u65e2\u5145\u5206\u5229\u7528\u591a\u6838\uff0c\u53c8\u5145\u5206\u53d1\u6325\u534f\u7a0b\u7684\u9ad8\u6548\u7387\uff0c\u53ef\u83b7\u5f97\u6781\u9ad8\u7684\u6027\u80fd\u3002<br \/>\nPython\u5bf9\u534f\u7a0b\u7684\u652f\u6301\u662f\u901a\u8fc7generator\u5b9e\u73b0\u7684\u3002<br \/>\n\u751f\u4ea7\u8005\u751f\u4ea7\u6d88\u606f\u540e\uff0c\u76f4\u63a5\u901a\u8fc7yield\u8df3\u8f6c\u5230\u6d88\u8d39\u8005\u5f00\u59cb\u6267\u884c\uff0c\u5f85\u6d88\u8d39\u8005\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5207\u6362\u56de\u751f\u4ea7\u8005\u7ee7\u7eed\u751f\u4ea7\uff0c\u6548\u7387\u6781\u9ad8\uff1a<\/p>\n<div>\n<pre>def consumer():\n    r = \\'\\'\n    while True:\n        n = yield r\n        if not n:\n            return\n        print(\\'[CONSUMER] Consuming %s...\\' % n)\n        r = \\'200 OK\\'\ndef produce(c):\n    c.send(None)\n    n = 0\n    while n &lt; 5:\n        n = n + 1\n        print(\\'[PRODUCER] Producing %s...\\' % n)\n        r = c.send(n)\n        print(\\'[PRODUCER] Consumer return: %s\\' % r)\n    c.close()\nc = consumer()\nproduce(c)\n<\/pre>\n<\/div>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<div>\n<pre>[PRODUCER] Producing 1...\n[CONSUMER] Consuming 1...\n[PRODUCER] Consumer return: 200 OK\n[PRODUCER] Producing 2...\n[CONSUMER] Consuming 2...\n[PRODUCER] Consumer return: 200 OK\n[PRODUCER] Producing 3...\n[CONSUMER] Consuming 3...\n[PRODUCER] Consumer return: 200 OK\n[PRODUCER] Producing 4...\n[CONSUMER] Consuming 4...\n[PRODUCER] Consumer return: 200 OK\n[PRODUCER] Producing 5...\n[CONSUMER] Consuming 5...\n[PRODUCER] Consumer return: 200 OK\n<\/pre>\n<\/div>\n<p>\u6ce8\u610f\u5230consumer\u51fd\u6570\u662f\u4e00\u4e2agenerator\uff0c\u628a\u4e00\u4e2aconsumer\u4f20\u5165produce\u540e\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\u8c03\u7528c.send(None)\u542f\u52a8\u751f\u6210\u5668\uff1b<\/li>\n<li>\u7136\u540e\uff0c\u4e00\u65e6\u751f\u4ea7\u4e86\u4e1c\u897f\uff0c\u901a\u8fc7c.send(n)\u5207\u6362\u5230consumer\u6267\u884c\uff1b<\/li>\n<li>consumer\u901a\u8fc7yield\u62ff\u5230\u6d88\u606f\uff0c\u5904\u7406\uff0c\u53c8\u901a\u8fc7yield\u628a\u7ed3\u679c\u4f20\u56de\uff1b<\/li>\n<li>produce\u62ff\u5230consumer\u5904\u7406\u7684\u7ed3\u679c\uff0c\u7ee7\u7eed\u751f\u4ea7\u4e0b\u4e00\u6761\u6d88\u606f\uff1b<\/li>\n<li>produce\u51b3\u5b9a\u4e0d\u751f\u4ea7\u4e86\uff0c\u901a\u8fc7c.close()\u5173\u95edconsumer\uff0c\u6574\u4e2a\u8fc7\u7a0b\u7ed3\u675f\u3002<\/li>\n<\/ol>\n<p>\u6574\u4e2a\u6d41\u7a0b\u65e0\u9501\uff0c\u7531\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\uff0cproduce\u548cconsumer\u534f\u4f5c\u5b8c\u6210\u4efb\u52a1\uff0c\u6240\u4ee5\u79f0\u4e3a\u201c\u534f\u7a0b\u201d\uff0c\u800c\u975e\u7ebf\u7a0b\u7684\u62a2\u5360\u5f0f\u591a\u4efb\u52a1\u3002<\/p>\n<h2>asyncio<\/h2>\n<p>Python\u5b9e\u73b0\u5f02\u6b65IO\u975e\u5e38\u7b80\u5355\uff0casyncio\u662fPython 3.4\u7248\u672c\u5f15\u5165\u7684\u6807\u51c6\u5e93\uff0c\u76f4\u63a5\u5185\u7f6e\u4e86\u5bf9\u5f02\u6b65IO\u7684\u652f\u6301\u3002<br \/>\nasyncio\u7684\u7f16\u7a0b\u6a21\u578b\u5c31\u662f\u4e00\u4e2a\u6d88\u606f\u5faa\u73af\u3002\u6211\u4eec\u4eceasyncio\u6a21\u5757\u4e2d\u76f4\u63a5\u83b7\u53d6\u4e00\u4e2aEventLoop\u7684\u5f15\u7528\uff0c\u7136\u540e\u628a\u9700\u8981\u6267\u884c\u7684\u534f\u7a0b\u6254\u5230EventLoop\u4e2d\u6267\u884c\uff0c\u5c31\u5b9e\u73b0\u4e86\u5f02\u6b65IO\u3002<br \/>\n\u7528asyncio\u7684\u5f02\u6b65\u7f51\u7edc\u8fde\u63a5\u6765\u83b7\u53d6sina\u3001sohu\u548c163\u7684\u7f51\u7ad9\u9996\u9875\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div>\n<pre>import asyncio\n@asyncio.coroutine\ndef wget(host):\n    print(\\'wget %s...\\' % host)\n    connect = asyncio.open_connection(host, 80)\n    reader, writer = yield from connect\n    header = \\'GET \/ HTTP\/1.0\\\\r\\\\nHost: %s\\\\r\\\\n\\\\r\\\\n\\' % host\n    writer.write(header.encode(\\'utf-8\\'))\n    yield from writer.drain()\n    while True:\n        line = yield from reader.readline()\n        if line == b\\'\\\\r\\\\n\\':\n            break\n        print(\\'%s header &gt; %s\\' % (host, line.decode(\\'utf-8\\').rstrip()))\n    # Ignore the body, close the socket\n    writer.close()\nloop = asyncio.get_event_loop()\ntasks = [wget(host) for host in [\\'www.sina.com.cn\\', \\'www.sohu.com\\', \\'www.163.com\\']]\nloop.run_until_complete(asyncio.wait(tasks))\nloop.close()\n<\/pre>\n<\/div>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<div>\n<pre>wget www.sohu.com...\nwget www.sina.com.cn...\nwget www.163.com...\n(\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4)\n(\u6253\u5370\u51fasohu\u7684header)\nwww.sohu.com header &gt; HTTP\/1.1 200 OK\nwww.sohu.com header &gt; Content-Type: text\/html\n...\n(\u6253\u5370\u51fasina\u7684header)\nwww.sina.com.cn header &gt; HTTP\/1.1 200 OK\nwww.sina.com.cn header &gt; Date: Wed, 20 May 2015 04:56:33 GMT\n...\n(\u6253\u5370\u51fa163\u7684header)\nwww.163.com header &gt; HTTP\/1.0 302 Moved Temporarily\nwww.163.com header &gt; Server: Cdn Cache Server V2.0\n...\n<\/pre>\n<\/div>\n<p>@asyncio.coroutine\u628a\u4e00\u4e2agenerator\u6807\u8bb0\u4e3acoroutine\u7c7b\u578b\uff0c\u7136\u540e\uff0c\u6211\u4eec\u5c31\u628a\u8fd9\u4e2acoroutine\u6254\u5230EventLoop\u4e2d\u6267\u884c\u3002<br \/>\nyield from\u8bed\u6cd5\u53ef\u4ee5\u8ba9\u6211\u4eec\u65b9\u4fbf\u5730\u8c03\u7528\u53e6\u4e00\u4e2agenerator\u3002\u6240\u4ee5\u7ebf\u7a0b\u4e0d\u4f1a\u7b49\u5f85IO\u64cd\u4f5c\uff0c\u800c\u662f\u76f4\u63a5\u4e2d\u65ad\u5e76\u6267\u884c\u4e0b\u4e00\u4e2a\u6d88\u606f\u5faa\u73af\u3002\u5f53yield from\u8fd4\u56de\u65f6\uff0c\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u4eceyield from\u62ff\u5230\u8fd4\u56de\u503c\uff0c\u7136\u540e\u63a5\u7740\u6267\u884c\u4e0b\u4e00\u884c\u8bed\u53e5\u3002<br \/>\n\u5728\u6b64\u671f\u95f4\uff0c\u4e3b\u7ebf\u7a0b\u5e76\u672a\u7b49\u5f85\uff0c\u800c\u662f\u53bb\u6267\u884cEventLoop\u4e2d\u5176\u4ed6\u53ef\u4ee5\u6267\u884c\u7684coroutine\u4e86\uff0c\u56e0\u6b64\u6211\u4eec\u7528Task\u5c01\u88c5\u7684\u4e09\u4e2acoroutine\u53ef\u4ee5\u5b9e\u73b0\u7531\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c\u3002<\/p>\n<h2>async\/await<\/h2>\n<p>\u4e3a\u4e86\u7b80\u5316\u5e76\u66f4\u597d\u5730\u6807\u8bc6\u5f02\u6b65IO\uff0c\u4ecePython 3.5\u5f00\u59cb\u5f15\u5165\u4e86\u65b0\u7684\u8bed\u6cd5async\u548cawait\uff0c\u53ef\u4ee5\u8ba9coroutine\u7684\u4ee3\u7801\u66f4\u7b80\u6d01\u6613\u8bfb\u3002<br \/>\n\u4f7f\u7528\u65b0\u8bed\u6cd5\uff0c\u53ea\u9700\u8981\u505a\u4e24\u6b65\u7b80\u5355\u7684\u66ff\u6362\uff1a<\/p>\n<ol>\n<li>\u628a@asyncio.coroutine\u66ff\u6362\u4e3aasync\uff1b<\/li>\n<li>\u628ayield from\u66ff\u6362\u4e3aawait\u3002<\/li>\n<\/ol>\n<p>\u7528\u65b0\u8bed\u6cd5\u91cd\u65b0\u7f16\u5199\u4e0a\u4e00\u8282\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div>\n<pre>import asyncio\nasync def wget(host):\n    print(\\'wget %s...\\' % host)\n    connect = asyncio.open_connection(host, 80)\n    reader, writer = await connect\n    header = \\'GET \/ HTTP\/1.0\\\\r\\\\nHost: %s\\\\r\\\\n\\\\r\\\\n\\' % host\n    writer.write(header.encode(\\'utf-8\\'))\n    await writer.drain()\n    while True:\n        line = await reader.readline()\n        if line == b\\'\\\\r\\\\n\\':\n            break\n        print(\\'%s header &gt; %s\\' % (host, line.decode(\\'utf-8\\').rstrip()))\n    # Ignore the body, close the socket\n    writer.close()\nloop = asyncio.get_event_loop()\ntasks = [wget(host) for host in [\\'www.sina.com.cn\\', \\'www.sohu.com\\', \\'www.163.com\\']]\nloop.run_until_complete(asyncio.wait(tasks))\nloop.close()\n<\/pre>\n<\/div>\n<p>\u5269\u4e0b\u7684\u4ee3\u7801\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<h2>aiohttp<\/h2>\n<p>asyncio\u5b9e\u73b0\u4e86TCP\u3001UDP\u3001SSL\u7b49\u534f\u8bae\uff0caiohttp\u5219\u662f\u57fa\u4e8easyncio\u5b9e\u73b0\u7684HTTP\u6846\u67b6\u3002<br \/>\n\u5b89\u88c5aiohttp\uff1a<\/p>\n<div>\n<pre>pip install aiohttp\n<\/pre>\n<\/div>\n<p>\u7136\u540e\u7f16\u5199\u4e00\u4e2aHTTP\u670d\u52a1\u5668\uff0c\u5206\u522b\u5904\u7406\u4ee5\u4e0bURL\uff1a<\/p>\n<ul>\n<li>\/ &#8211; \u9996\u9875\u8fd4\u56deb\\'&lt;h1&gt;Index&lt;\/h1&gt;\\&#8217;\uff1b<\/li>\n<li>\/hello\/{name} &#8211; \u6839\u636eURL\u53c2\u6570\u8fd4\u56de\u6587\u672chello, %s!\u3002<\/li>\n<\/ul>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div>\n<pre>import asyncio\nfrom aiohttp import web\nasync def index(request):\n    await asyncio.sleep(0.5)\n    return web.Response(body=b\\'&lt;h1&gt;Index&lt;\/h1&gt;\\')\nasync def hello(request):\n    await asyncio.sleep(0.5)\n    text = \\'&lt;h1&gt;hello, %s!&lt;\/h1&gt;\\' % request.match_info[\\'name\\']\n    return web.Response(body=text.encode(\\'utf-8\\'))\nasync def init(loop):\n    app = web.Application(loop=loop)\n    app.router.add_route(\\'GET\\', \\'\/\\', index)\n    app.router.add_route(\\'GET\\', \\'\/hello\/{name}\\', hello)\n    srv = await loop.create_server(app.make_handler(), \\'127.0.0.1\\', 8000)\n    print(\\'Server started at http:\/\/127.0.0.1:8000...\\')\n    return srv\nloop = asyncio.get_event_loop()\nloop.run_until_complete(init(loop))\nloop.run_forever()\n<\/pre>\n<\/div>\n<p>\u6ce8\u610faiohttp\u7684\u521d\u59cb\u5316\u51fd\u6570init()\u4e5f\u662f\u4e00\u4e2acoroutine\uff0cloop.create_server()\u5219\u5229\u7528asyncio\u521b\u5efaTCP\u670d\u52a1\u3002\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u6b65IO CPU\u7684\u901f\u5ea6\u8fdc\u8fdc\u5feb\u4e8e\u78c1\u76d8\u3001\u7f51\u7edc\u7b49IO\u3002\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\uff0cCPU\u6267\u884c\u4ee3\u7801\u7684\u901f\u5ea6\u6781\u5feb\uff0c\u7136\u800c\uff0c\u4e00\u65e6\u9047\u5230IO\u64cd\u4f5c &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1650\"> <span class=\"screen-reader-text\">Python \u5f02\u6b65 IO<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[106,126],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"\u5f02\u6b65IO CPU\u7684\u901f\u5ea6\u8fdc\u8fdc\u5feb\u4e8e\u78c1\u76d8\u3001\u7f51\u7edc\u7b49IO\u3002\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\uff0cCPU\u6267\u884c\u4ee3\u7801\u7684\u901f\u5ea6\u6781\u5feb\uff0c\u7136\u800c\uff0c\u4e00\u65e6\u9047\u5230IO\u64cd\u4f5c&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1650"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1650"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1650\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1650"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}