\u5f02\u6b65\u7f16\u7a0b\u662f\u5e76\u884c\u7f16\u7a0b\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5355\u4e2a\u5de5\u4f5c\u5355\u5143\u72ec\u7acb\u4e8e\u4e3b\u5e94\u7528\u7a0b\u5e8f\u7ebf\u7a0b\u8fd0\u884c\uff0c\u5e76\u901a\u77e5\u8c03\u7528\u7ebf\u7a0b\u5176\u5b8c\u6210\u3001\u5931\u8d25\u60c5\u51b5\u6216\u8fdb\u5ea6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u5f02\u6b65\u7f16\u7a0b\u662f\u5e76\u884c\u7f16\u7a0b\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5355\u4e2a\u5de5\u4f5c\u5355\u5143\u72ec\u7acb\u4e8e\u4e3b\u5e94\u7528\u7a0b\u5e8f\u7ebf\u7a0b\u8fd0\u884c\uff0c\u5e76\u901a\u77e5\u8c03\u7528\u7ebf\u7a0b\u5176\u5b8c\u6210\u3001\u5931\u8d25\u60c5\u51b5\u6216\u8fdb\u5ea6\u3002\u4e0b\u9762\u8fd9\u5f20\u56fe\u7406\u89e3\u8d77\u6765\u4f1a\u66f4\u76f4\u89c2\u4e00\u4e9b\uff1a
\n
\n\u540c\u6b65\u7f16\u7a0b\u5f88\u666e\u904d\u3002\u5982\u56fe\uff0c\u8bf7\u6c421\u88ab\u53d1\u51fa\u540e\u7b49\u5f85\u54cd\u5e941\uff1b\u4e00\u65e6\u5f97\u5230\u54cd\u5e941\u5c31\u53d1\u51fa\u8bf7\u6c422\uff0c\u7136\u540e\u7b49\u5f85\u5b83\u7684\u54cd\u5e94\u3002\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5411\u51fd\u6570\u4f20\u9012\u53c2\u6570\u201ca\u201d\u540e\u7b49\u5f85\u51fd\u6570\u8fd4\u56de\u66f4\u6539\u540e\u7684\u503c\uff0c\u7136\u540e\u518d\u6b21\u8c03\u7528\u4ee5\u66f4\u6539\u6570\u5b57\uff0c\u6700\u540e\u518d\u6b21\u5f97\u5230\u54cd\u5e94\uff0c\u8fd9\u5c31\u662f\u540c\u6b65\u7f16\u7a0b\u3002<\/p>\n
\u800c\u5bf9\u4e8e\u5f02\u6b65\u7f16\u7a0b\u6765\u8bf4\uff0c\u8bf7\u6c421\u88ab\u53d1\u51fa\u540e\uff0c\u65e0\u9700\u7b49\u54cd\u5e941\u4fbf\u53ef\u76f4\u63a5\u53d1\u51fa\u8bf7\u6c422\u3002\u4e24\u4e2a\u8bf7\u6c42\u5b8c\u6210\u540e\u5f97\u5230\u4e24\u4e2a\u54cd\u5e94\u3002\u7b80\u5355\u5730\u8bf4\u5c31\u662f\u8bf7\u6c421\u548c\u8bf7\u6c422\u662f\u5e76\u884c\u5904\u7406\u7684\uff0c\u4e0d\u7b49\u524d\u4e00\u4e2a\u8bf7\u6c42\u7684\u54cd\u5e94\u4fbf\u63d0\u51fa\u65b0\u7684\u8bf7\u6c42\u3002<\/p>\n
\u7b80\u8a00\u4e4b\uff0c\u53ea\u8981\u6253\u5f00\u4efb\u52a1\u7ba1\u7406\u5668\uff08macOS\u4e2d\u7684\u6d3b\u52a8\u76d1\u89c6\u5668\uff09\u5c31\u80fd\u770b\u5230\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u540c\u6b65\u8fd0\u884c\uff1b\u6216\u8005\u4e00\u4e2aPython Shell\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u7ec8\u7aef\u7a97\u53e3\u4e2d\u8fd0\u884c\u3002\u4e13\u4e1a\u672f\u8bed\u53eb\u505a\u591a\u8fdb\u7a0b\uff08MultiProcessing\uff09\uff0c\u987e\u540d\u601d\u4e49\uff0c\u5373\u4e0d\u6b62\u4e00\u4e2a\u8fdb\u7a0b\u5728\u8fd0\u884c\u3002<\/p>\n
\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\uff1f<\/p>\n
\u4e00\u4e2a\u540c\u6b65\u7f16\u7a0b\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
\r\ndeffun(length,a):\r\nb = a\r\nfor i inrange(length):\r\na+=1\r\nprint(\"value of a before: \"+str(b)+\" now it's \"+str(a))\r\nreturn a\r\n\r\ndefmain():\r\nr1 =fun(50000000,0)\r\nr2 =fun(100,12)\r\nr3 =fun(100,41)\r\nif __name__==\"__main__\":\r\nmain()<\/pre>\n\u4ee5\u4e0a\u4ee3\u7801\u7684\u8f93\u51fa\uff1a
\n
\n\u5728Python\u4e2d\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff0c\u53ea\u9700\u8981\u8fd9\u51e0\u6b65\u5c31\u591f\u4e86
\n\u8fd9\u6bb5\u4ee3\u7801\u4f20\u9012\u4e86for\u5faa\u73af\u7684\u8303\u56f4\u3002\u6267\u884c\u4ee3\u7801\u8017\u65f6\u957f\u8fbe13.843\u79d2\uff0c\u56e0\u4e3ar1\u7684\u8303\u56f4\u662f5000\uff0c\u6240\u4ee5\u8017\u65f6\u4e45\u3002\u73b0\u5728\u7684\u95ee\u9898\u662f\uff0c\u5fc5\u987b\u5148\u5f85r1\u4efb\u52a1\u5b8c\u6210\uff0c\u5426\u5219\u65e0\u6cd5\u5f97\u5230r2\u548cr3\u3002\u53ef\u80fd\u5728\u5f97\u5230r1\u4e4b\u524d\u5c31\u5f97\u5230r2\u548cr3\u5417\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c\u8fd9\u6b63\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u7528\u6b66\u4e4b\u5730\u3002<\/p>\n
\u9996\u5148\u7528pip\u6307\u4ee4\u5b89\u88c5\u5f02\u6b65\u5305\u3002<\/p>\n
pip install asyncio<\/pre>\n\u5b89\u88c5\u540e\uff0c\u770b\u4e00\u4e0b\u65b0\u4ee3\u7801\u3002\u4f7f\u7528\u5f02\u6b65\u5305\uff1a<\/p>\n
\r\nimport asyncio\r\nasyncdeffun(length,a):\r\nb = a\r\nfor i inrange(length):\r\na+=1\r\nif i %10000==0:\r\nawait asyncio.sleep(0.0001)\r\nprint(\"value of a before: \"+str(b)+\" now it's \"+str(a))\r\nreturn a\r\nasyncdefmain():\r\n#creating subroutines.\r\nt1 = loop.create_task(fun(50000000,0))\r\nt2 = loop.create_task(fun(100,12))\r\nt3 = loop.create_task(fun(100,41))\r\nawait asyncio.wait([t1,t2,t3])\r\nif __name__==\"__main__\":\r\nloop = asyncio.get_event_loop()\r\nloop.run_until_complete(main())\r\nloop.close()<\/pre>\n\u9996\u5148\u89c2\u5bdf\u8be5\u4ee3\u7801\u7684\u8f93\u51fa\uff0c\u63a5\u4e0b\u6765\u8ba8\u8bba\u8be5\u4ee3\u7801\uff1a<\/p>\n
\u5728Python\u4e2d\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff0c\u53ea\u9700\u8981\u8fd9\u51e0\u6b65\u5c31\u591f\u4e86<\/p>\n
\n\u8f93\u51fa-1<\/p>\n
\u5728Python\u4e2d\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff0c\u53ea\u9700\u8981\u8fd9\u51e0\u6b65\u5c31\u591f\u4e86
\n
\n\u8f93\u51fa-2<\/p>\n
\u8f93\u51fa-1\u4e2d\u9996\u5148\u80fd\u5f97\u5230t2\u548ct3\u8fdb\u7a0b\u7684\u7ed3\u679c\uff0c\u7136\u540e\u5728\u8f93\u51fa-2\u7684\u622a\u56fe\u4e2d\u5f97\u5230\u4e86t1\u8fdb\u7a0b\u7684\u7ed3\u679c\uff0c\u8fd9\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u529f\u52b3\u3002t1\u8fdb\u7a0b\u8017\u65f6\u6700\u957f\uff0c\u6240\u4ee5\u5b83\u7684\u7ed3\u679c\u6700\u540e\u4ea7\u751f\uff0c\u4e14t1\u3001t2\u548ct3\u8fdb\u7a0b\u5747\u5e76\u884c\u8fd0\u884c\u3002\u5f02\u6b65\u7f16\u7a0b\u7684\u597d\u5904\u5c31\u5728\u4e8e\u4e0d\u5fc5\u7b49\u5f85\u4efb\u4f55\u8fdb\u7a0b\u7684\u7ed3\u679c\uff0c\u4fbf\u53ef\u83b7\u5f97\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u7684\u7ed3\u679c\u3002<\/p>\n
\u8ba9\u6211\u4eec\u8ba8\u8bba\u4e00\u4e0b\u6b64\u4ee3\u7801\u3002<\/p>\n
\u9996\u5148\uff0c\u5728if __name__==\"__main__\"\u4e2d\u5b9a\u4e49\u4e86asyncio.get_event_loop()\uff0c\u5e76\u5c06\u8fd9\u4e2a\u5faa\u73af\u4f5c\u4e3a\u5904\u7406\u5faa\u73af\u4e8b\u4ef6\u7684\u5f02\u6b65\u5bf9\u8c61\u3002\u7136\u540e\u521b\u5efa\u4e00\u4e2amain\u7684\u4f8b\u884c\u7a0b\u5e8f\uff0c\u5e76\u8bbe\u7f6e\u6761\u4ef6\uff1a\u82e5main\u6ca1\u5b8c\u6210\u5219\u7ee7\u7eed\u5faa\u73af\u3002\u6bcf\u6b21\u5f02\u6b65\uff0c\u90fd\u8981\u8fdb\u884cloop.close()\u7684\u7f16\u7a0b\uff0c\u5426\u5219\u7ed3\u679c\u5c31\u4f1a\u9519\u8bef\u6216\u5f02\u5e38\u3002<\/p>\n
\u7136\u540e\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3aasyncdeffunc_name\uff0c\u8fd9\u6837\u89e3\u91ca\u5668\u5c31\u77e5\u9053\u51fd\u6570\u7528\u4e86\u5f02\u6b65\u7684\u65b9\u6cd5\u3002\u5728main()\u4e2d\u5b9a\u4e49\u4e86\u4e09\u4e2a\u4efb\u52a1\uff08\u4e5f\u53ef\u79f0\u4e3a\u5b50\u4f8b\u7a0b\uff09\uff0c\u5e76\u4f7f\u7528\u4e86await\u51fd\u6570\uff0c\u4ee5\u4fbf\u5b83\u7b49\u5f85\u4e09\u4e2a\u8fdb\u7a0b\u7ed3\u675f\uff08\u5373\u4f7f\u5b83\u6ca1\u6709\u591a\u5927\u610f\u4e49\uff0c\u4e5f\u5fc5\u987b\u4f7f\u7528\u5b83\uff09\u3002<\/p>\n
\u6700\u540e\u7528\u4e86fun\uff08\uff09\u51fd\u6570\u3002i %10000\u7684if\u6761\u4ef6\u8ba9\u6700\u5927\u8303\u56f4\u7684\u8fdb\u7a0b\u5f90\u5f90\u8fd0\u884c\uff0c\u6700\u540e\u5f97\u5230\u4e86\u7b54\u6848\u3002\u4ed4\u7ec6\u7814\u7a76fun\uff08\uff09\u51fd\u6570\u5e76\u81ea\u5df1\u5c1d\u8bd5\u7684\u8bdd\uff0c\u5176\u4e2d\u7684\u903b\u8f91\u4f1a\u663e\u5f97\u975e\u5e38\u5408\u7406\u548c\u76f4\u63a5\u3002<\/p>\n
\u5728Python\u4e2d\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff0c\u4f60\u5b66\u4f1a\u4e86\u5417\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5f02\u6b65\u7f16\u7a0b\u662f\u5e76\u884c\u7f16\u7a0b\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5355\u4e2a\u5de5\u4f5c\u5355\u5143\u72ec\u7acb\u4e8e\u4e3b\u5e94\u7528\u7a0b\u5e8f\u7ebf\u7a0b\u8fd0\u884c\uff0c\u5e76\u901a\u77e5\u8c03\u7528\u7ebf\u7a0b\u5176\u5b8c\u6210\u3001\u5931\u8d25\u60c5\u51b5\u6216\u8fdb\u5ea6\u3002\u4e0b\u9762 […]<\/p>\n","protected":false},"author":1469,"featured_media":204003,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-203996","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\/203996","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\/1469"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=203996"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/203996\/revisions"}],"predecessor-version":[{"id":204007,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/203996\/revisions\/204007"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/204003"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=203996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=203996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=203996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}