{"id":1201,"date":"2019-06-07T08:28:57","date_gmt":"2019-06-07T00:28:57","guid":{"rendered":"https:\/\/swf.com.tw\/?p=1201"},"modified":"2019-06-07T08:29:00","modified_gmt":"2019-06-07T00:29:00","slug":"python%e7%9a%84%e9%9d%9e%e9%98%bb%e5%a1%9e%e5%bc%8f%ef%bc%88non-blocking%ef%bc%89socket%e9%80%9a%e8%a8%8a%e7%a8%8b%e5%bc%8f%ef%bc%88%e4%ba%8c%ef%bc%89%ef%bc%9a%e4%bd%bf%e7%94%a8select%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/swf.com.tw\/?p=1201","title":{"rendered":"Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab"},"content":{"rendered":"<p><a href=\"\/?p=1198\" target=\"_blank\" rel=\"noopener noreferrer\">\u4e0a\u4e00\u7bc7\u6587\u7ae0<\/a>\u4f7f\u7528try\u2026except\u5305\u570d\u6240\u6709socket\u76f8\u95dc\u6558\u8ff0\uff0c\u672c\u6587\u5c07\u63a1\u7528\u88ab\u5ee3\u6cdb\u7528\u5728socket\u901a\u8a0a\u7a0b\u5f0f\u7684<a href=\"https:\/\/docs.python.org\/3\/library\/select.html\" target=\"_blank\" rel=\"noopener noreferrer\">select\u7a0b\u5f0f\u5eab<\/a>\uff0c\u89e3\u6c7asocket\u963b\u585e\u554f\u984c\u3002<\/p>\n<p><strong>select\u7a0b\u5f0f\u5eab\u7684select()\u51fd\u5f0f\uff0c\u53ef\u63a5\u6536\u4f86\u81ea\u4f5c\u696d\u7cfb\u7d71\u7684socket\u72c0\u614b\u8a0a\u606f\uff0c\u6bcf\u7576\u6709\u8cc7\u6599\u8f38\u5165\u6216\u8005\u6e96\u5099\u597d\u8981\u8f38\u51fa\u6642\uff0cPython\u7a0b\u5f0f\u78bc\u5c31\u6703\u6536\u5230\u901a\u77e5\u3002<\/strong>select()\u4e0d\u50c5\u80fd\u5075\u807dsocket\u72c0\u614b\uff0c\u5728UNIX\/Linux\u7cfb\u7d71\u4e0a\u4e5f\u53ef\u4ee5\u5075\u807d\u6a94\u6848\u8b80\u5beb\u3001\u4f7f\u7528\u8005\u5728\u7d42\u7aef\u6a5f\u7684\u8f38\u5165\u64cd\u4f5c\u2026\u7b49\u6240\u6709\u53ef\u50b3\u56de\u6709\u6548\u300c\u6a94\u6848\u63cf\u8ff0\u7b26\u300d\uff08file descriptor\uff0c\u76f8\u7576\u65bc\u6a94\u6848\u7684\u8b58\u5225\u78bc\uff09\u7684\u7269\u4ef6\u3002<\/p>\n<p>select()\u51fd\u5f0f\u63a5\u65363\u500b\u5217\u8868\uff08list\uff09\u985e\u578b\u53c3\u6578\uff0c\u4e26\u50b3\u56de3\u500b\u5217\u8868\u503c\uff0c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/images\/books\/micropython\/socket_select_syntax.png\" width=\"652\" height=\"184\" alt=\"select()\u51fd\u5f0f\u8a9e\u6cd5\u8aaa\u660e\"\/><\/p>\n<p>select()\u51fd\u5f0f\u5c07\u4e0d\u505c\u5730\u6aa2\u67e5\u8f38\u5165\u3001\u8f38\u51fa\u548c\u4f8b\u5916\u932f\u8aa4\u4e09\u500b\u5217\u8868\u7684\u7269\u4ef6\uff0c\u82e5\u5176\u4e2d\u4efb\u4f55\u7269\u4ef6\u6709\u52d5\u975c\uff0c\u4f8b\u5982\uff0c\u300c\u8f38\u5165\u5217\u8868\u300d\u88e1\u7684\u4f3a\u670d\u5668socket\u6709\u65b0\u7684\u9023\u7dda\u8acb\u6c42\uff0c\u5b83\u5c07\u50b3\u56de\u8a72\u7269\u4ef6\uff0c\u8b93\u5e95\u4e0b\u7684\u7a0b\u5f0f\u78bc\u8655\u7406\u5f8c\u7e8c\u4f5c\u696d\uff0c\u4f8b\u5982\uff0c\u56de\u61c9\u9023\u7dda\u8acb\u6c42\u3002<\/p>\n<p><strong>\u5047\u5982\u6c92\u6709\u8a2d\u5b9a\u8d85\u6642\u79d2\u6578\u53c3\u6578\uff0cselect()\u51fd\u5f0f\u5c07\u963b\u585e\u7a0b\u5f0f\u6d41\u7a0b\uff0c\u76f4\u5230\u4e0a\u8ff0\u4efb\u4e00\u300c\u5075\u807d\u5c0d\u8c61\u300d\u6709\u52d5\u975c\uff0c\u624d\u6703\u7e7c\u7e8c\u5f80\u4e0b\u57f7\u884c\u3002<\/strong><\/p>\n<p>\u9996\u5148\u4f7f\u7528select()\u51fd\u5f0f\u6539\u5beb\u300a<a href=\"\/?p=1129\" target=\"_blank\" rel=\"noopener noreferrer\">\u8d85\u5716\u89e3Python\u7269\u806f\u7db2\u5be6\u4f5c\u5165\u9580<\/a>\u300b17-2\u9801\u7684MicroPython\u7db2\u7ad9\u4f3a\u670d\u5668\u7a0b\u5f0f\uff0c\u8acb\u5728\u958b\u982d\u5f15\u7528select\u6a21\u7d44\uff0c\u4e26\u5728\u7a0b\u5f0f\u4e2d\u5ba3\u544a\u4e00\u500b\u5c07\u7528\u65bcselect()\u51fd\u5f0f\u7684\u300c\u8f38\u5165\u300d\u7269\u4ef6\u5217\u8868\uff1a<\/p>\n<pre>import select\nimport socket\n\nserver = socket.socket()\nHOST = '0.0.0.0'\nPORT = 80\nhttpHeader = b\"\"\"\\\nHTTP\/1.0 200 OK\n\nWelcome to MicroPython!\n\"\"\"\n\nserver.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\nserver.bind((HOST, PORT))\nserver.setblocking(0)  # socket\u8a2d\u6210\u300c\u975e\u963b\u585e\u300d\u6a21\u5f0f\nserver.listen(5)\ninputs = [server]      # \u5ba3\u544a\u300c\u8f38\u5165\u300d\u5217\u8868\uff0c\u5b58\u5165\u4f3a\u670d\u5668socket\u3002\n\nprint(\"Server running on port \", PORT)\n\ndef hello():         # \u5c07\u88ab\u4e0d\u505c\u57f7\u884c\u7684\u81ea\u8a02\u51fd\u5f0f\n    print('hello!')\n\n<\/pre>\n<p>\u6539\u5beb\u5f8c\u7684\u4e3b\u7a0b\u5f0f\u8ff4\u5708\u5982\u4e0b\uff0c\u7db2\u7ad9\u4f3a\u670d\u5668\u7a0b\u5f0f\u53ea\u9700\u8981\u8b93select()\u5075\u807d\u300c\u8f38\u5165\u300d\u7269\u4ef6\u7684\u72c0\u614b\uff0c\u300c\u8f38\u51fa\u300d\u548c\u300c\u4f8b\u5916\u932f\u8aa4\u300d\u5169\u500b\u5217\u8868\u90fd\u8a2d\u6210\u7a7a\u767d\uff1b\u300c\u8d85\u6642\u79d2\u6578\u300d\u53c3\u6578\u8a2d\u62101\uff0c\u4ee3\u8868\u5047\u59821\u79d2\u9418\u4e4b\u5167\uff0c\u300c\u8f38\u5165\u300d\u5217\u8868\u88e1\u7684\u7269\u4ef6\u90fd\u6c92\u6709\u52d5\u975c\uff0c\u5c31\u89e3\u9664\u963b\u585e\uff0c\u7e7c\u7e8c\u5f80\u4e0b\u57f7\u884c\u3002\u56e0\u6b64\uff0c\u9019\u500b\u8ff4\u5708\u5c07\u4f7f\u5f97\u6700\u5f8c\u4e00\u884c\u7684hello()\u51fd\u5f0f\u6bcf\u96941\u79d2\u88ab\u547c\u53eb\u4e00\u6b21\u3002\u300c\u8d85\u6642\u79d2\u6578\u300d\u53ef\u4ee5\u8a2d\u5b9a\u6210\u5f88\u5c0f\u7684\u6b63\u6d6e\u9ede\u6578\uff0c\u4f8b\u59820.5\u6216\u80050\uff08\u4ee3\u8868\u300c\u8f38\u5165\u300d\u6c92\u6709\u52d5\u975c\u5c31\u7acb\u5373\u5f80\u4e0b\u57f7\u884c\uff09\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/images\/books\/micropython\/socket_select_code.png\" width=\"687\" height=\"427\" alt=\"select()\u51fd\u5f0f\u904b\u4f5c\u6d41\u7a0b\"\/><\/p>\n<p>\u6bcf\u7576\u300c\u8f38\u5165\u300d\u5217\u8868\u88e1\u7684\u7269\u4ef6\uff08\u521d\u59cb\u5167\u5bb9\u50c5\u6709\u4f3a\u670d\u5668socket\u7269\u4ef6\uff09\u6536\u5230\u8cc7\u6599\uff0cselect()\u5c31\u6703\u89e3\u9664\u963b\u585e\uff0c\u9032\u5165\u4e0a\u9762\u7a0b\u5f0f\u7de8\u865f3\u6240\u5728\u7684for\u8ff4\u5708\u3002\u6b64\u6642\uff0creadable\u5217\u8868\u88e1\u9762\u5c07\u5305\u542b\u4f3a\u670d\u5668socket\u7269\u4ef6\uff0c\u56e0\u6b64if\u689d\u4ef6\u6210\u7acb\uff0c\u4e0a\u5716\u7de8\u865f4\u90a3\u4e00\u884c\uff0c\u5c07\u5efa\u7acb\u4e00\u500b\u9023\u7d50\u7528\u6236\u7aef\u7684socket\u7269\u4ef6\u3002<\/p>\n<p>\u65b0\u5efa\u7acb\u7684\u7528\u6236\u7aefsocket\u4e5f\u8981\u4ea4\u7d66select()\u51fd\u5f0f\u5075\u807d\u52d5\u975c\uff0c\u6240\u4ee5\u7de8\u865f5\u90a3\u4e00\u884c\u5c07\u5b83\u9644\u52a0\u5230inputs\u5217\u8868\uff0c<strong>\u9019\u6642\u7684inputs\u5217\u8868\u5305\u542b\u5169\u500bsocket\u7269\u4ef6\u3002<\/strong><\/p>\n<p>\u7576\u7528\u6236\u7aefsocket\u6536\u5230\u4f86\u81ea\u700f\u89bd\u5668\u7684HTTP\u8acb\u6c42\u8a0a\u606f\u6642\uff0cselect()\u5c07\u518d\u6b21\u50b3\u56de\u300c\u6709\u8cc7\u6599\u8f38\u5165\u300d\u7684socket\u7269\u4ef6\uff0c\u9019\u4e00\u6b21\u662f\u7528\u6236\u7aefsocket\uff0c\u6240\u4ee5for\u8ff4\u5708\u88e1\u7684else\u5340\u584a\u88ab\u57f7\u884c\uff0c\u50b3\u9001HTTP\u56de\u61c9\u7d66\u700f\u89bd\u5668\uff0c\u7136\u5f8c\u95dc\u9589socket\u9023\u7dda\u3002<\/p>\n<p>\u7528\u6236\u7aefsocket\u8207\u700f\u89bd\u5668\u7684\u9023\u7dda\u95dc\u9589\u4e86\uff0c\u4e5f\u5c31\u4e0d\u6703\u518d\u6536\u5230\u8a0a\u606f\uff0c\u56e0\u6b64\u7de8\u865f7\u90a3\u4e00\u884c\u5c07\u5b83\u5f9einputs\u5217\u8868\u4e2d\u522a\u9664\u3002\u65e5\u5f8c\u82e5\u6709\u65b0\u7684HTTP\u9023\u7dda\u8acb\u6c42\uff0c\u540c\u6a23\u6703\u7531\u4f3a\u670d\u5668socket\u5148\u6536\u5230\u8cc7\u6599\uff0c\u518d\u52d5\u614b\u5efa\u7acb\u65b0\u7684\u7528\u6236\u7aefsocket\u4f86\u670d\u52d9\u8a72\u7528\u6236\uff08\u53c3\u95b1<a href=\"\/?p=1198\" target=\"_blank\" rel=\"noopener noreferrer\">\u4e0a\u4e00\u7bc7\u6587\u7ae0<\/a>\u8aaa\u660e\uff09\u3002<\/p>\n<p>\u5e95\u4e0b\u662f\u4fee\u6539\u5f8c\u7684diy17_1.py\u5b8c\u6574\u7a0b\u5f0f\u78bc\uff0c\u63a1\u7528select()\u51fd\u5f0f\u8655\u7406\u975e\u963b\u585esocket\u901a\u8a0a\uff1a<\/p>\n<pre>import select\nimport socket\n\nserver = socket.socket()\nHOST = '0.0.0.0'\nPORT = 80\nhttpHeader = b\"\"\"\\\nHTTP\/1.0 200 OK\n\nWelcome to MicroPython!\n\"\"\"\n\nserver.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\nserver.bind((HOST, PORT))\nserver.setblocking(0)  # socket\u8a2d\u6210\u300c\u975e\u963b\u585e\u300d\u6a21\u5f0f\nserver.listen(5)\ninputs = [server]      # \u5ba3\u544a\u300c\u8f38\u5165\u300d\u5217\u8868\uff0c\u5b58\u5165\u4f3a\u670d\u5668socket\u3002\n\nprint(\"Server running on port \", PORT)\n\n\ndef hello():\n    print('hello!')\n\nwhile True:\n    readable, _, _ = select.select(inputs, [], [], 1)\n\n    for sck in readable:\n        if sck is server:\n            client, addr = sck.accept()\n            client.setblocking(0)\n            inputs.append(client)\n        else:\n            req = sck.recv(1024)\n            sck.send(httpHeader)\n            sck.close()\n            inputs.remove(sck)\n\n    hello()\n<\/pre>\n<p>\u628a\u9019\u500b\u7a0b\u5f0f\u6a94\u4e0a\u50b3\u5230Wemos D1 mini\u6216\u5176\u4ed6\u71d2\u9304MicroPython\u7684ESP8266\u63a7\u5236\u677f\u5f8c\uff0c\u900f\u904e\u7d42\u7aef\u6a5f\u57f7\u884c\u5b83\u7684\u7d50\u679c\u5982\u4e0b\uff0c\u5b83\u5c07\u6bcf\u96941\u79d2\u986f\u793a\u2018hello!\u2019\uff0c\u4ee3\u8868socket\u4e26\u6c92\u6709\u9020\u6210\u963b\u585e\uff0c\u800c\u4e14\u5b83\u4e5f\u53ef\u4ee5\u56de\u61c9\u700f\u89bd\u5668\u7684\u9023\u7dda\u8acb\u6c42\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/images\/books\/micropython\/socket_select_running.png\" width=\"555\" height=\"129\" alt=\"\u4f7f\u7528select\u51fd\u5f0f\u7684socket\u7a0b\u5f0f\u904b\u4f5c\u7d50\u679c\"\/><\/p>\n<h2>\u4f7f\u7528select()\u6539\u5beb\u5b58\u53d6\u7db2\u9801\u6a94\u6848\u7684MicroPython\u7db2\u7ad9\u4f3a\u670d\u5668\u7a0b\u5f0f<\/h2>\n<p>\u6211\u5011\u53ef\u4ee5\u7528\u76f8\u540c\u624b\u6cd5\u6539\u5beb17-14\u9801\u7684http_file.py\u6a94\uff0c\u540c\u6a23\u5148\u5728\u958b\u982d\u5f15\u7528select\u6a21\u7d44\uff0c\u7b46\u8005\u4fee\u6539\u4e86err()\u51fd\u5f0f\uff0c\u8abf\u6574\u5b83\u7684\u52d5\u614b\u5b57\u4e32\u683c\u5f0f\u4e26\u52a0\u4e0a\u6355\u6349\u4f8b\u5916\u932f\u8aa4\uff0c\u4ee5\u9632\u5728\u50b3\u9001\u56de\u61c9\u6642\u7528\u6236\u7aef\u5148\u884c\u96e2\u7dda\u800c\u5f15\u767c\u932f\u8aa4\u3002<\/p>\n<pre>import select\nimport socket\nimport os\nimport gc\n\ndef err(socket, code, msg):\n    rsp = b'''\\\n    HTTP\/1.0 {code}\n    Content-type:text\/html\n\n    &lt;h1&gt;{msg}&lt;\/h1&gt;\n\n    '''\n    try:\n        socket.write(rsp.format(code=code, msg=msg))\n    except:\n        print('response error!')\n\n<\/pre>\n<p>\u8b80\u53d6\u8207\u50b3\u9001\u7db2\u9801\u8cc7\u6e90\u7684writeFile()\u51fd\u5f0f\uff0c\u8acb\u5c07\u8b80\u53d6\u6a94\u6848\u7684\u7de9\u885d\u8a18\u61b6\u9ad4\u5f9e64\u4f4d\u5143\u7d44\u6539\u62101\u4f4d\u5143\u7d44\uff0c\u82e5\u4e0d\u4fee\u6539\uff0c\u6539\u7528select()\u51fd\u5f0f\u8655\u7406socket\u7684Wemos D1 mini\u63a7\u5236\u677f\u53ef\u80fd\u7121\u6cd5\u8b80\u53d6\u5b8c\u6574\u7684\u6a94\u6848\uff0c\u4f8b\u5982\uff0c\u5047\u8a2d\u5716\u6a94\u70ba7.4KB\uff0c\u5b83\u53ef\u80fd\u53ea\u8b80\u52306.3KB\u5c31\u56de\u5831\u8b80\u53d6\u5b8c\u7562\u3002\u540c\u6a23\u5730\uff0c\u70ba\u4e86\u907f\u514d\u7528\u6236\u7aef\u5728\u6a94\u6848\u50b3\u9001\u5b8c\u7562\u4e4b\u524d\u65b7\u7dda\u800c\u5f15\u767c\u932f\u8aa4\uff0cclient.write(chunk)\u6558\u8ff0\u4e5f\u7528try\u2026except\u5305\u570d\u3002<\/p>\n<pre>def writeFile(client, fileName):\n    contentType = checkMimeType(fileName)\n\n    if contentType:\n        fileSize = checkFileSize(WWWROOT+fileName)\n\n        if fileSize != None:\n            header = httpHeader.format(contentType, fileSize)\n            client.write(header.encode('utf-8'))\n\n            with open(WWWROOT+fileName, 'rb') as f:\n                while True:\n                    chunk = f.read(1) # \u539f\u672c\u662fread(64)\n                    if not chunk:     # \u7b49\u540c\u9019\u500b\u5beb\u6cd5\uff1aif chunk == b'':\n                        break\n                    try:\n                        client.write(chunk)\n                    except OSError:\n                        print('client disconnected?')\n                        break\n        else:\n            err(client, \"404\", \"Not Found\")\n    else:\n        err(client, \"415\", \"Unsupported Media Type\")\n\ndef hello():\n    print('hello!')\n<\/pre>\n<p>\u6700\u5f8c\u8981\u4fee\u6539\u5730\u65b9\u7684\u662fmain()\u4e3b\u51fd\u5f0f\uff0c\u70ba\u4e86\u5075\u807d\u4e26\u8655\u7406socket\u53ef\u80fd\u5f15\u767c\u7684\u932f\u8aa4\uff0cselect()\u51fd\u5f0f\u7684\u300c\u4f8b\u5916\u932f\u8aa4\u300d\u5217\u8868\u53c3\u6578\u8a2d\u5b9a\u6210inputs\uff0c\u7576\u5176\u4e2d\u7684\u4efb\u4e00socket\u5f15\u767c\u4f8b\u5916\u932f\u8aa4\u6642\uff0c\u8a72socket\u5c07\u88ab\u50b3\u5165err\u8b8a\u6578\u3002<\/p>\n<pre>def main():\n    server = socket.socket()\n    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n    server.bind((HOST, PORT))\n    server.listen(5)\n    server.setblocking(0)\n    inputs = [server]     # \u63d0\u4f9b\u7d66select()\u7684\u300c\u8f38\u5165\u300d\u5217\u8868\n    print('Web server running on port', PORT)\n\n    while True:\n        # \u5075\u807d\u300c\u8f38\u5165\u300d\u5217\u8868\u53ef\u80fd\u5f15\u767c\u7684\u932f\u8aa4\n        readable, _, err = select.select(inputs, [], inputs, 1)\n\n        for s in readable:\n            if s is server:\n                client = s.accept()[0]\n                client.setblocking(0)\n                inputs.append(client)\n            else:\n                handleRequest(s)\n                s.close()\n                inputs.remove(s)\n                print('Free RAM before GC:', gc.mem_free())\n                gc.collect()\n                print('Free RAM after GC:', gc.mem_free())\n\n        # \u82e5socket\u767c\u751f\u4f8b\u5916\u932f\u8aa4\uff0c\u5c31\u4ee4\u5b83\u65b7\u7dda\u4e26\u522a\u9664\u3002\n        for s in err:\n            print('Socket ERROR!')\n            inputs.remove(s)\n            s.close()\n\n        hello()\n\nif __name__ == '__main__':\n    main()\n<\/pre>\n<p>\u5176\u9918\u7a0b\u5f0f\u78bc\u4e0d\u7528\u6539\uff0c\u4e0a\u50b3\u4e26\u57f7\u884c\u6b64http_file.py\u6a94\uff0c\u7d42\u7aef\u6a5f\u5c07\u6bcf\u96941\u79d2\u986f\u793a\u2018hello!\u2019\uff0c\u800c\u4e14\u5b83\u4e5f\u53ef\u4ee5\u56de\u61c9\u700f\u89bd\u5668\u7684\u9023\u7dda\u8acb\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p><strong>select\u7a0b\u5f0f\u5eab\u7684select()\u51fd\u5f0f\uff0c\u53ef\u63a5\u6536\u4f86\u81ea\u4f5c\u696d\u7cfb\u7d71\u7684socket\u72c0\u614b\u8a0a\u606f\uff0c\u6bcf\u7576\u6709\u8cc7\u6599\u8f38\u5165\u6216\u8005\u6e96\u5099\u597d\u8981\u8f38\u51fa\u6642\uff0cPython\u7a0b\u5f0f\u78bc\u5c31\u6703\u6536\u5230\u901a\u77e5\u3002se<\/strong>lect()\u4e0d\u50c5\u80fd\u5075\u807dsocket\u72c0\u614b\uff0c\u5728UNIX\/Linux\u7cfb\u7d71\u4e0a\u4e5f\u53ef\u4ee5\u5075\u807d\u6a94\u6848\u8b80\u5beb\u3001\u4f7f\u7528\u8005\u5728\u7d42\u7aef\u6a5f\u7684\u8f38\u5165\u64cd\u4f5c\u2026\u7b49\u6240\u6709\u53ef\u50b3\u56de\u6709\u6548\u300c\u6a94\u6848\u63cf\u8ff0\u7b26\u300d\uff08file descriptor\uff0c\u76f8\u7576\u65bc\u6a94\u6848\u7684\u8b58\u5225\u78bc\uff09\u7684\u7269\u4ef6\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/images\/books\/micropython\/socket_select_code.png\" width=\"687\" height=\"427\" alt=\"select()\u51fd\u5f0f\u904b\u4f5c\u6d41\u7a0b\"\/><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[3,15],"tags":[44,54,49],"class_list":["post-1201","post","type-post","status-publish","format-standard","hentry","category-3","category-diy","tag-esp8266","tag-micropython","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab - \u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swf.com.tw\/?p=1201\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005:\" \/>\n\t<meta name=\"twitter:data1\" content=\"cubie\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9810\u4f30\u95b1\u8b80\u6642\u9593\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\u9418\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swf.com.tw\\\/?p=1201#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swf.com.tw\\\/?p=1201\"},\"author\":{\"name\":\"cubie\",\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#\\\/schema\\\/person\\\/24480fda9184aa137df8f05d79bc2f57\"},\"headline\":\"Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab\",\"datePublished\":\"2019-06-07T00:28:57+00:00\",\"dateModified\":\"2019-06-07T00:29:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swf.com.tw\\\/?p=1201\"},\"wordCount\":102,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#organization\"},\"keywords\":[\"ESP8266\",\"MicroPython\",\"Python\"],\"articleSection\":[\"\u6559\u5b78\u6587\u4ef6\",\"\u786c\u9ad4\u8207DIY\"],\"inLanguage\":\"zh-TW\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/swf.com.tw\\\/?p=1201#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swf.com.tw\\\/?p=1201\",\"url\":\"https:\\\/\\\/swf.com.tw\\\/?p=1201\",\"name\":\"Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab - \u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#website\"},\"datePublished\":\"2019-06-07T00:28:57+00:00\",\"dateModified\":\"2019-06-07T00:29:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swf.com.tw\\\/?p=1201#breadcrumb\"},\"inLanguage\":\"zh-TW\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swf.com.tw\\\/?p=1201\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swf.com.tw\\\/?p=1201#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swf.com.tw\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#website\",\"url\":\"https:\\\/\\\/swf.com.tw\\\/\",\"name\":\"\u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8\",\"description\":\"Arduino+Python+JavaScript+\u7a0b\u5f0f\u8a2d\u8a08+\u786c\u9ad4DIY\",\"publisher\":{\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swf.com.tw\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-TW\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#organization\",\"name\":\"\u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8\",\"url\":\"https:\\\/\\\/swf.com.tw\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-TW\",\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swf.com.tw\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/logo_bat.png\",\"contentUrl\":\"https:\\\/\\\/swf.com.tw\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/logo_bat.png\",\"width\":71,\"height\":65,\"caption\":\"\u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8\"},\"image\":{\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swf.com.tw\\\/#\\\/schema\\\/person\\\/24480fda9184aa137df8f05d79bc2f57\",\"name\":\"cubie\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-TW\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c77b6f4d3b4ccdcdc343aeee53301c212f54787c43bd2ebf6648bf74ff59b474?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c77b6f4d3b4ccdcdc343aeee53301c212f54787c43bd2ebf6648bf74ff59b474?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c77b6f4d3b4ccdcdc343aeee53301c212f54787c43bd2ebf6648bf74ff59b474?s=96&d=retro&r=g\",\"caption\":\"cubie\"},\"sameAs\":[\"https:\\\/\\\/swf.com.tw\"],\"url\":\"https:\\\/\\\/swf.com.tw\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab - \u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swf.com.tw\/?p=1201","twitter_misc":{"\u4f5c\u8005:":"cubie","\u9810\u4f30\u95b1\u8b80\u6642\u9593":"2 \u5206\u9418"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swf.com.tw\/?p=1201#article","isPartOf":{"@id":"https:\/\/swf.com.tw\/?p=1201"},"author":{"name":"cubie","@id":"https:\/\/swf.com.tw\/#\/schema\/person\/24480fda9184aa137df8f05d79bc2f57"},"headline":"Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab","datePublished":"2019-06-07T00:28:57+00:00","dateModified":"2019-06-07T00:29:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swf.com.tw\/?p=1201"},"wordCount":102,"commentCount":1,"publisher":{"@id":"https:\/\/swf.com.tw\/#organization"},"keywords":["ESP8266","MicroPython","Python"],"articleSection":["\u6559\u5b78\u6587\u4ef6","\u786c\u9ad4\u8207DIY"],"inLanguage":"zh-TW","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/swf.com.tw\/?p=1201#respond"]}]},{"@type":"WebPage","@id":"https:\/\/swf.com.tw\/?p=1201","url":"https:\/\/swf.com.tw\/?p=1201","name":"Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab - \u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8","isPartOf":{"@id":"https:\/\/swf.com.tw\/#website"},"datePublished":"2019-06-07T00:28:57+00:00","dateModified":"2019-06-07T00:29:00+00:00","breadcrumb":{"@id":"https:\/\/swf.com.tw\/?p=1201#breadcrumb"},"inLanguage":"zh-TW","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swf.com.tw\/?p=1201"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/swf.com.tw\/?p=1201#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swf.com.tw\/"},{"@type":"ListItem","position":2,"name":"Python\u7684\u975e\u963b\u585e\u5f0f\uff08non-blocking\uff09socket\u901a\u8a0a\u7a0b\u5f0f\uff08\u4e8c\uff09\uff1a\u4f7f\u7528select\u7a0b\u5f0f\u5eab"}]},{"@type":"WebSite","@id":"https:\/\/swf.com.tw\/#website","url":"https:\/\/swf.com.tw\/","name":"\u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8","description":"Arduino+Python+JavaScript+\u7a0b\u5f0f\u8a2d\u8a08+\u786c\u9ad4DIY","publisher":{"@id":"https:\/\/swf.com.tw\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swf.com.tw\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-TW"},{"@type":"Organization","@id":"https:\/\/swf.com.tw\/#organization","name":"\u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8","url":"https:\/\/swf.com.tw\/","logo":{"@type":"ImageObject","inLanguage":"zh-TW","@id":"https:\/\/swf.com.tw\/#\/schema\/logo\/image\/","url":"https:\/\/swf.com.tw\/wp-content\/uploads\/2019\/10\/logo_bat.png","contentUrl":"https:\/\/swf.com.tw\/wp-content\/uploads\/2019\/10\/logo_bat.png","width":71,"height":65,"caption":"\u8d85\u5716\u89e3\u7cfb\u5217\u5716\u66f8"},"image":{"@id":"https:\/\/swf.com.tw\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/swf.com.tw\/#\/schema\/person\/24480fda9184aa137df8f05d79bc2f57","name":"cubie","image":{"@type":"ImageObject","inLanguage":"zh-TW","@id":"https:\/\/secure.gravatar.com\/avatar\/c77b6f4d3b4ccdcdc343aeee53301c212f54787c43bd2ebf6648bf74ff59b474?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c77b6f4d3b4ccdcdc343aeee53301c212f54787c43bd2ebf6648bf74ff59b474?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c77b6f4d3b4ccdcdc343aeee53301c212f54787c43bd2ebf6648bf74ff59b474?s=96&d=retro&r=g","caption":"cubie"},"sameAs":["https:\/\/swf.com.tw"],"url":"https:\/\/swf.com.tw\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/swf.com.tw\/index.php?rest_route=\/wp\/v2\/posts\/1201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swf.com.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swf.com.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swf.com.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swf.com.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1201"}],"version-history":[{"count":0,"href":"https:\/\/swf.com.tw\/index.php?rest_route=\/wp\/v2\/posts\/1201\/revisions"}],"wp:attachment":[{"href":"https:\/\/swf.com.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swf.com.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swf.com.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}