{"id":204636,"date":"2025-05-29T14:36:11","date_gmt":"2025-05-29T06:36:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204636\/"},"modified":"2025-05-29T14:36:11","modified_gmt":"2025-05-29T06:36:11","slug":"python%e7%ba%bf%e7%a8%8b%e4%b8%ad%e5%8a%a0%e9%94%81%e8%8c%83%e5%9b%b4%ef%bc%9a%e8%8c%83%e5%9b%b4%e8%b6%8a%e5%a4%a7%e8%b6%8a%e5%a5%bd%e8%bf%98%e6%98%af%e8%b6%8a%e5%b0%8f%e8%b6%8a%e5%a5%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204636\/","title":{"rendered":"Python\u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\uff1a\u8303\u56f4\u8d8a\u5927\u8d8a\u597d\u8fd8\u662f\u8d8a\u5c0f\u8d8a\u597d\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\uff1a\u8303\u56f4\u8d8a\u5927\u8d8a\u597d\u8fd8\u662f\u8d8a\u5c0f\u8d8a\u597d\uff1f<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython\u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\uff1a\u8303\u56f4\u8d8a\u5927\u8d8a\u597d\u8fd8\u662f\u8d8a\u5c0f\u8d8a\u597d\uff1f\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17313818926732ca84129e5.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\uff1a\u8d8a\u5927\u8d8a\u597d\u8fd8\u662f\u8d8a\u5c0f\u8d8a\u597d\uff1f<\/strong><\/p>\n<p>\u5728python\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u9501\u5177\u6709\u4e00\u5b9a\u7684\u8303\u56f4\uff0c\u8fd9\u76f4\u63a5\u5f71\u54cd\u5e76\u53d1\u6267\u884c\u7684\u6b63\u786e\u6027\u548c\u6548\u7387\u3002\u4e3a\u4e86\u6df1\u5165\u7406\u89e3\u8fd9\u4e00\u95ee\u9898\uff0c\u6211\u4eec\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u6765\u63a2\u8ba8\u4e24\u79cd\u52a0\u9501\u8303\u56f4\u4e0b\u7684\u5dee\u5f02\u3002<\/p>\n<p><strong>\u6848\u4f8b1\uff1a\u628a\u9501\u653e\u5728\u5faa\u73af\u5916\u9762<\/strong><\/p>\n<pre>from threading import thread, lock\nimport time\n\nnum = 0\n\ndef test1():\n    global num\n    mutex.acquire()\n    for i in range(1000000):\n        num += 1\n    mutex.release()\n    print(\"--test1--num=%d\" % num)\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>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9501\u7684\u4f5c\u7528\u57df\u6db5\u76d6\u4e86\u6574\u4e2a\u5faa\u73af\u4f53\u3002\u8fd9\u786e\u4fdd\u4e86\u5bf9\u5171\u4eab\u53d8\u91cf<strong>num<\/strong>\u7684\u8bbf\u95ee\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u7136\u800c\uff0c\u56e0\u4e3a\u9501\u7684\u8303\u56f4\u8f83\u5927\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u5728\u6267\u884c\u5faa\u73af\u65f6\u90fd\u4f1a\u88ab\u963b\u585e\uff0c\u5bfc\u81f4\u6574\u4f53\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<p><strong>\u6848\u4f8b2\uff1a\u628a\u9501\u653e\u5728for\u91cc\u9762<\/strong><\/p>\n<pre>from threading import Thread, Lock\nimport time\n\nnum = 0\n\ndef test1():\n    global num\n    for i in range(1000000):\n        mutex.acquire()\n        num += 1\n        mutex.release()\n    print(\"--test1--num=%d\" % num)\n\ndef test2():\n    global num\n    for i in range(1000000):\n        mutex.acquire()\n        num += 1\n        mutex.release()\n    print(\"--test2--num=%d\" % num)<\/pre>\n<p>\u5728\u6b64\u4f8b\u4e2d\uff0c\u9501\u7684\u8303\u56f4\u4ec5\u9650\u4e8e\u5bf9<strong>num<\/strong>\u7684\u5355\u6b21\u56de\u5199\u64cd\u4f5c\u3002\u8fd9\u79cd\u7f29\u5c0f\u7684\u8303\u56f4\u5141\u8bb8\u7ebf\u7a0b\u5728\u4e92\u4e0d\u5e72\u6270\u7684\u60c5\u51b5\u4e0b\u540c\u65f6\u6267\u884c\u5faa\u73af\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u56e0\u4e3a\u7ebf\u7a0b\u5728\u5199<strong>num<\/strong>\u4ee5\u5916\u7684\u4ee3\u7801\u65f6\u4e0d\u9700\u8981\u9501\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u52a0\u9501\u8303\u56f4\u7684\u5927\u5c0f\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<ul>\n<li><strong>\u5f53\u9700\u8981\u786e\u4fdd\u5171\u4eab\u8d44\u6e90\u5728\u4efb\u4f55\u65f6\u5019\u53ea\u80fd\u7531\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u65f6\uff0c\u5e94\u5c06\u9501\u653e\u5728\u5faa\u73af\u5916\u9762\u3002<\/strong><\/li>\n<li><strong>\u5f53\u591a\u4e2a\u7ebf\u7a0b\u9700\u8981\u540c\u65f6\u8bfb\u5199\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u91c7\u7528\u66f4\u7cbe\u7ec6\u7684\u52a0\u9501\u7b56\u7565\uff0c\u5c06\u9501\u653e\u5728\u8f83\u5c0f\u7684\u8303\u56f4\u5185\uff08\u4f8b\u5982\uff0c\u4ec5\u6db5\u76d6\u5355\u6b21\u8bfb\u53d6\u6216\u5199\u5165\u64cd\u4f5c\uff09\uff0c\u4ee5\u6700\u5927\u9650\u5ea6\u5730\u63d0\u9ad8\u5e76\u53d1\u6027\u3002<\/strong><\/li>\n<\/ul>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPython\u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\uff1a\u8303\u56f4\u8d8a\u5927\u8d8a\u597d\u8fd8\u662f\u8d8a\u5c0f\u8d8a\u597d\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\uff1a\u8303\u56f4&#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-204636","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204636","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=204636"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204636\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}