{"id":204707,"date":"2025-05-29T10:40:56","date_gmt":"2025-05-29T02:40:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204707\/"},"modified":"2025-05-29T10:40:56","modified_gmt":"2025-05-29T02:40:56","slug":"python-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e9%87%8d%e5%a4%8d%e6%89%a7%e8%a1%8c%e9%97%ae%e9%a2%98%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e7%ba%bf%e7%a8%8b5%e4%bc%9a%e9%87%8d%e5%a4%8d%e6%89%a7%e8%a1%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204707\/","title":{"rendered":"Python \u591a\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u7ebf\u7a0b5\u4f1a\u91cd\u590d\u6267\u884c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u591a\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u7ebf\u7a0b5\u4f1a\u91cd\u590d\u6267\u884c\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 \u591a\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u7ebf\u7a0b5\u4f1a\u91cd\u590d\u6267\u884c\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\/20241114\/173155809067357aca37da6.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u539f\u56e0\u53ca\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u4e3a\u4ec0\u4e48\u5728\u5305\u542b\u5b50\u7a0b\u5e8fsub\u7684python\u7a0b\u5e8f\u4e2d\uff0c\u5f53\u7a0b\u5e8f\u6267\u884c\u5230\u6700\u540e\u65f6\uff0c\u4f1a\u6253\u5370\u51fa\u591a\u4e2athread-5\uff0c\u8868\u660e\u7ebf\u7a0b5\u91cd\u590d\u6267\u884c\u4e86\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u5728\u7ed9\u5b9a\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0cfor\u5faa\u73af\u91cd\u590d\u521b\u5efa\u4e865\u4e2a\u7ebf\u7a0b\uff0c\u5176\u76ee\u6807\u90fd\u662f\u8c03\u7528sub\u51fd\u6570\u3002\u7136\u800c\uff0c\u5faa\u73af\u7ed3\u675f\u540e\uff0ct\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u6240\u6307\u5411\u7684\u7ebf\u7a0b\u90fd\u662f\u7ebf\u7a0b5\uff0c\u5bfc\u81f4\u53ea\u6709\u7ebf\u7a0b5\u91cd\u590d\u6267\u884c\u3002<\/p>\n<p><strong>\u89e3\u51b3\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\uff0ct\u9700\u8981\u5728for\u5faa\u73af\u4e2d\u7528\u4e00\u4e2a\u65b0\u7684thread\u5b9e\u4f8b\u91cd\u65b0\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u6b63\u786e\u7684\u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n<pre>import threading, time\n\nnum = 5\nlock = threading.Lock() # \u521b\u5efa\u540c\u6b65\u9501\n\ndef sub():\n    global num\n    print('sub %s' % t.name)\n    lock.acquire() # \u83b7\u5f97\u540c\u6b65\u9501\uff1a\u4e0d\u8ba9\u522b\u7684\u7ebf\u7a0b\u5728\u540c\u4e00\u65f6\u523b\u8fd0\u884c\n    print(threading.current_thread().getName())\n    temp = num\n    time.sleep(1)\n    num = temp - 1\n    lock.release() #\u89e3\u9664\u540c\u6b65\u9501\n    print('%s ' % t.name, num)\n\nfor i in range(5):\n    t = threading.Thread(target=sub)\n    t.start()\n    print('start %s' % t.name)\n\n# for T in L:\n#     T.join()\n\nprint('s', num)<\/pre>\n<p>\u5728\u8fd9\u4e2a\u6539\u8fdb\u7248\u672c\u4e2d\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684t\u5bf9\u8c61\uff0c\u4ece\u800c\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u90fd\u80fd\u6309\u987a\u5e8f\u6267\u884csub\u51fd\u6570\uff0c\u800c\u4e0d\u4f1a\u53d1\u751f\u91cd\u590d\u6267\u884c\u95ee\u9898\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u591a\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u7ebf\u7a0b5\u4f1a\u91cd\u590d\u6267\u884c\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u591a\u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898&#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-204707","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204707","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=204707"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204707\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}