{"id":205205,"date":"2025-05-29T08:40:06","date_gmt":"2025-05-29T00:40:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205205\/"},"modified":"2025-05-29T08:40:06","modified_gmt":"2025-05-29T00:40:06","slug":"python-%e7%ba%bf%e7%a8%8b%e5%8a%a0%e9%94%81%ef%bc%9a%e8%8c%83%e5%9b%b4%e8%b6%8a%e5%b0%8f%e8%b6%8a%e5%a5%bd%ef%bc%8c%e4%bd%86%e8%bf%99%e7%9c%9f%e7%9a%84%e6%80%bb%e6%98%af%e5%af%b9%e7%9a%84%e5%90%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205205\/","title":{"rendered":"Python \u7ebf\u7a0b\u52a0\u9501\uff1a\u8303\u56f4\u8d8a\u5c0f\u8d8a\u597d\uff0c\u4f46\u8fd9\u771f\u7684\u603b\u662f\u5bf9\u7684\u5417\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u7ebf\u7a0b\u52a0\u9501\uff1a\u8303\u56f4\u8d8a\u5c0f\u8d8a\u597d\uff0c\u4f46\u8fd9\u771f\u7684\u603b\u662f\u5bf9\u7684\u5417\uff1f<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython \u7ebf\u7a0b\u52a0\u9501\uff1a\u8303\u56f4\u8d8a\u5c0f\u8d8a\u597d\uff0c\u4f46\u8fd9\u771f\u7684\u603b\u662f\u5bf9\u7684\u5417\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241126\/1732585432674527d88c1fa.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\u8d8a\u5c0f\u8d8a\u597d<\/strong><\/p>\n<p><strong>\u95ee\u9898\u8bf4\u660e<\/strong><\/p>\n<p>\u5728python\u7ebf\u7a0b\u4e2d\uff0c\u4f7f\u7528\u9501\u4fdd\u8bc1\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u5171\u4eab\u6570\u636e\u7684\u5e76\u53d1\u8bbf\u95ee\u65f6\uff0c\u52a0\u9501\u7684\u8303\u56f4\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\uff0c\u662f\u5c06\u9501\u653e\u5728\u5faa\u73af\u5916\u9762\u8fd8\u662f\u91cc\u9762\u3002<\/p>\n<p><strong>\u5bf9\u6bd4\u4e24\u79cd\u60c5\u51b5<\/strong><\/p>\n<p><strong>\u628a\u9501\u653e\u5728\u5faa\u73af\u5916\u9762\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ee3\u7801\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre>from threading import thread, lock\nimport time\n\nnum = 0\n\nmutex = lock()  # \u521b\u5efa\u4e00\u628a\u4e92\u65a5\u9501\n\ndef test1():\n    for i in range(1000000):\n        # \u4e0a\u9501\n        mutex.acquire()\n        num += 1\n        # \u89e3\u9501\n        mutex.release()\n\n\ndef test2():\n    for i in range(1000000):\n        # \u4e0a\u9501\n        mutex.acquire()\n        num += 1\n        # \u89e3\u9501\n        mutex.release()\n\nif __name__ == \"__main__\":\n    start_time = time.time()  # \u5f00\u59cb\u65f6\u95f4\n\n    p1 = thread(target=test1)\n    p1.start()\n    p2 = thread(target=test2)\n    p2.start()\n\n    p1.join()\n    p2.join()\n\n    end_time = time.time()  # \u7ed3\u675f\u65f6\u95f4\n    print(\"\u8fd0\u884c\u65f6\u95f4\uff1a%.2fs\" % (end_time - start_time))<\/pre>\n<ul>\n<li><strong>\u7279\u70b9\uff1a<\/strong>\u52a0\u9501\u8303\u56f4\u5927\uff0c\u6574\u4e2a\u5faa\u73af\u4f53\u90fd\u88ab\u9501\u4f4f\u4e86\u3002<\/li>\n<li><strong>\u4f18\u70b9\uff1a<\/strong>\u7b80\u5355\u6613\u5b9e\u73b0\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u5171\u4eab\u6570\u636e\u7684\u539f\u5b50\u6027\u3002<\/li>\n<li><strong>\u7f3a\u70b9\uff1a<\/strong>\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u4e3a\u6574\u4e2a\u5faa\u73af\u90fd\u4f1a\u88ab\u963b\u585e\uff0c\u5bfc\u81f4\u5176\u4ed6\u7ebf\u7a0b\u65e0\u6cd5\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><strong>\u628a\u9501\u653e\u5728for\u91cc\u9762\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ee3\u7801\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre>from threading import Thread, Lock\nimport time\n\nnum = 0\n\nmutex = Lock()  # \u521b\u5efa\u4e00\u628a\u4e92\u65a5\u9501\n\ndef test1():\n    for i in range(1000000):\n        if mutex.acquire(False):  # \u5c1d\u8bd5\u83b7\u53d6\u9501\uff0c\u6210\u529f\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\n            num += 1\n            mutex.release()  # \u91ca\u653e\u9501\n\ndef test2():\n    for i in range(1000000):\n        if mutex.acquire(False):  # \u5c1d\u8bd5\u83b7\u53d6\u9501\uff0c\u6210\u529f\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\n            num += 1\n            mutex.release()  # \u91ca\u653e\u9501\n\nif __name__ == \"__main__\":\n    start_time = time.time()  # \u5f00\u59cb\u65f6\u95f4\n\n    p1 = Thread(target=test1)\n    p1.start()\n    p2 = Thread(target=test2)\n    p2.start()\n\n    p1.join()\n    p2.join()\n\n    end_time = time.time()  # \u7ed3\u675f\u65f6\u95f4\n    print(\"\u8fd0\u884c\u65f6\u95f4\uff1a%.2fs\" % (end_time - start_time))<\/pre>\n<ul>\n<li><strong>\u7279\u70b9\uff1a<\/strong>\u52a0\u9501\u8303\u56f4\u5c0f\uff0c\u53ea\u6709\u5bf9\u5171\u4eab\u6570\u636e\u7684\u64cd\u4f5c\u65f6\u624d\u4f1a\u52a0\u9501\u3002<\/li>\n<li><strong>\u4f18\u70b9\uff1a<\/strong>\u6548\u7387\u8f83\u9ad8\uff0c\u53ea\u6709\u9700\u8981\u65f6\u624d\u52a0\u9501\uff0c\u5176\u4ed6\u65f6\u95f4\u7ebf\u7a0b\u53ef\u4ee5\u5e76\u884c\u8fd0\u884c\u3002<\/li>\n<li><strong>\u7f3a\u70b9\uff1a<\/strong>\u5b9e\u73b0\u8d77\u6765\u8f83\u590d\u6742\uff0c\u9700\u8981\u5bf9\u4ee3\u7801\u8fdb\u884c\u8f83\u591a\u7684\u6539\u52a8\u3002<\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u5efa\u8bae<\/strong><\/p>\n<p>\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6765\u9009\u62e9\u52a0\u9501\u8303\u56f4\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u9700\u8981\u4fdd\u8bc1\u6570\u636e\u7edd\u5bf9\u7684\u539f\u5b50\u6027\uff0c\u53ef\u4ee5\u5c06\u9501\u653e\u5728\u5faa\u73af\u5916\u9762\u3002<\/li>\n<li>\u5982\u679c\u6548\u7387\u662f\u9996\u8981\u8003\u8651\u56e0\u7d20\uff0c\u53ef\u4ee5\u5c06\u9501\u653e\u5728for\u91cc\u9762\u3002<\/li>\n<\/ul>\n<p><strong>\u5e38\u89c1\u7684\u9519\u8bef<\/strong><\/p>\n<ul>\n<li><strong>\u65e0\u9501\uff1a<\/strong>\u4e0d\u4f7f\u7528\u9501\u5bf9\u5171\u4eab\u6570\u636e\u5e76\u53d1\u8bbf\u95ee\uff0c\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/li>\n<li><strong>\u6b7b\u9501\uff1a<\/strong>\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u91ca\u653e\u9501\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7ebf\u7a0b\u52a0\u9501\uff1a\u8303\u56f4\u8d8a\u5c0f&#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-205205","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205205","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=205205"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205205\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}