{"id":930,"date":"2024-11-07T16:43:40","date_gmt":"2024-11-07T08:43:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/930\/"},"modified":"2024-11-07T16:43:40","modified_gmt":"2024-11-07T08:43:40","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:\/\/fwq.ai\/blog\/930\/","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><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1046\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173077815312905.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173077815312905.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173077815312905-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173077815312905-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173077815312905-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Python \u7ebf\u7a0b\u52a0\u9501\uff1a\u8303\u56f4\u8d8a\u5c0f\u8d8a\u597d\uff0c\u4f46\u8fd9\u771f\u7684\u603b\u662f\u5bf9\u7684\u5417\uff1f\u63d2\u56fe\" alt=\"Python \u7ebf\u7a0b\u52a0\u9501\uff1a\u8303\u56f4\u8d8a\u5c0f\u8d8a\u597d\uff0c\u4f46\u8fd9\u771f\u7684\u603b\u662f\u5bf9\u7684\u5417\uff1f\u63d2\u56fe\" \/><\/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><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/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<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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\u662fPython \u7ebf\u7a0b\u52a0\u9501\uff1a\u8303\u56f4\u8d8a\u5c0f\u8d8a\u597d\uff0c\u4f46\u8fd9\u771f\u7684\u603b\u662f\u5bf9\u7684\u5417\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u7ebf\u7a0b\u4e2d\u52a0\u9501\u8303\u56f4\u8d8a\u5c0f\u8d8a\u597d \u95ee\u9898\u8bf4\u660e \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 \u5bf9\u6bd4\u4e24\u79cd\u60c5\u51b5 \u7acb\u5373\u5b66\u4e60\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b \u628a\u9501\u653e\u5728\u5faa\u73af\u5916\u9762\uff1a \u4ee3\u7801\u5982\u4e0b\uff1a from threading import thread, lock import time num = 0 mutex = lock() # \u521b\u5efa\u4e00\u628a\u4e92\u65a5\u9501 def test1(): for i in range(1000000): # \u4e0a\u9501 mutex.acquire() num += 1 # \u89e3\u9501 mutex.release() def test2(): for i in range(1000000): # \u4e0a\u9501 mutex.acquire() num += 1 # \u89e3\u9501 mutex.release() if __name__ == [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-930","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=930"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/930\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}