{"id":204527,"date":"2025-05-29T10:24:57","date_gmt":"2025-05-29T02:24:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204527\/"},"modified":"2025-05-29T10:24:57","modified_gmt":"2025-05-29T02:24:57","slug":"python-%e7%ba%bf%e7%a8%8b%e4%b8%ad%e5%8a%a0%e9%94%81%e8%8c%83%e5%9b%b4%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%ef%bc%9a%e5%a4%a7%e8%8c%83%e5%9b%b4%e5%8a%a0%e9%94%81%e8%bf%98%e6%98%af%e5%b0%8f%e8%8c%83","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204527\/","title":{"rendered":"Python \u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\u5982\u4f55\u9009\u62e9\uff1a\u5927\u8303\u56f4\u52a0\u9501\u8fd8\u662f\u5c0f\u8303\u56f4\u52a0\u9501\u66f4\u4f18\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\u5982\u4f55\u9009\u62e9\uff1a\u5927\u8303\u56f4\u52a0\u9501\u8fd8\u662f\u5c0f\u8303\u56f4\u52a0\u9501\u66f4\u4f18\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241109\/1731160298672f68eaa6a9a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\u5927\u5c0f\u7684\u9009\u62e9<\/strong><\/p>\n<p>\u5728python\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u5bf9\u4e8e\u52a0\u9501\u8303\u56f4\u7684\u786e\u5b9a\u4e00\u76f4\u5b58\u5728\u4e00\u4e2a\u4e89\u8bba\uff1a\u662f\u5927\u8303\u56f4\u52a0\u9501\u597d\u8fd8\u662f\u5c0f\u8303\u56f4\u52a0\u9501\u597d\uff1f\u672c\u6587\u5c06\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u8bf4\u660e\u4e0d\u540c\u52a0\u9501\u8303\u56f4\u5bf9\u7ebf\u7a0b\u6267\u884c\u65f6\u95f4\u7684\u5f71\u54cd\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<pre>from threading import thread, lock\nimport time\n\nnum = 0\n\n\ndef test1():\n    global num\n    # \u4e0a\u9501\n    mutex.acquire()\n    for i in range(1000000):\n        num += 1\n    # \u89e3\u9501\n    mutex.release()\n    print(\"--test1--num=%d\" % num)\n\n\ndef test2():\n    global num\n    mutex.acquire()\n    for i in range(1000000):\n        num += 1\n    mutex.release()\n    print(\"--test2--num=%d\" % num)<\/pre>\n<p><strong>\u52a0\u9501\u8303\u56f4\u7684\u6bd4\u8f83<\/strong><\/p>\n<ul>\n<li><strong>\u5927\u8303\u56f4\u52a0\u9501\uff1a<\/strong>\u5c06\u6574\u4e2a\u5faa\u73af\u4f53\u90fd\u653e\u5728\u52a0\u9501\u4ee3\u7801\u5757\u5185\uff08\u89c1test1()\uff09\u3002<\/li>\n<li><strong>\u5c0f\u8303\u56f4\u52a0\u9501\uff1a<\/strong>\u4ec5\u9488\u5bf9\u589e\u51cf\u64cd\u4f5c (num += 1) \u8fdb\u884c\u52a0\u9501\uff08\u89c1test2()\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u9a8c\u7ed3\u679c<\/strong><\/p>\n<p>\u4f7f\u7528\u4e0d\u540c\u7684\u52a0\u9501\u8303\u56f4\u8fd0\u884c\u4ee3\u7801\u591a\u6b21\uff0c\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<p><strong>\u5927\u8303\u56f4\u52a0\u9501<\/strong><\/p>\n<pre>--test1--num=1890838\n--test2--num=2000000\nnum = 2000000\n\u8fd0\u884c\u65f6\u95f4:4.694896s<\/pre>\n<p><strong>\u5c0f\u8303\u56f4\u52a0\u9501<\/strong><\/p>\n<pre>--test1--num=1000000\n--test2--num=2000000\nnum = 2000000\n\u8fd0\u884c\u65f6\u95f4:0.287206s<\/pre>\n<p><strong>\u5206\u6790<\/strong><\/p>\n<p>\u4ece\u5b9e\u9a8c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u5c0f\u8303\u56f4\u52a0\u9501\u7684\u8fd0\u884c\u65f6\u95f4\u8fdc\u5c0f\u4e8e\u5927\u8303\u56f4\u52a0\u9501\u3002\u8fd9\u662f\u56e0\u4e3a\u5c0f\u8303\u56f4\u52a0\u9501\u4ec5\u9488\u5bf9\u5173\u952e\u90e8\u5206\uff08\u589e\u51cf\u64cd\u4f5c\uff09\u8fdb\u884c\u52a0\u9501\uff0c\u4ece\u800c\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u4e86\u7ebf\u7a0b\u963b\u585e\u65f6\u95f4\u3002\u800c\u5927\u8303\u56f4\u52a0\u9501\u5219\u5bf9\u6574\u4e2a\u5faa\u73af\u4f53\u8fdb\u884c\u4e86\u52a0\u9501\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u9891\u7e41\u7684\u7ebf\u7a0b\u963b\u585e\u548c\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u4ece\u800c\u964d\u4f4e\u4e86\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u51b3\u5b9a\u52a0\u9501\u8303\u56f4\u65f6\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u6743\u8861\u3002\u4e00\u822c\u800c\u8a00\uff0c\u5bf9\u4e8e\u5e76\u53d1\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8f83\u4f4e\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5c0f\u8303\u56f4\u52a0\u9501\u3002\u800c\u5bf9\u4e8e\u5e76\u53d1\u51b2\u7a81\u9891\u7e41\u7684\u64cd\u4f5c\uff0c\u5219\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5927\u8303\u56f4\u52a0\u9501\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPython \u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\u5982\u4f55\u9009\u62e9\uff1a\u5927\u8303\u56f4\u52a0\u9501\u8fd8\u662f\u5c0f\u8303\u56f4\u52a0\u9501\u66f4\u4f18\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\u5982\u4f55&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204527","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204527"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204527\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}