{"id":50069,"date":"2024-12-03T12:31:40","date_gmt":"2024-12-03T04:31:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50069\/"},"modified":"2024-12-03T12:31:40","modified_gmt":"2024-12-03T04:31:40","slug":"python-%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%e5%90%8c%e4%b8%80%e4%b8%aa%e5%8f%98%e9%87%8f%e5%af%bc%e8%87%b4%e5%a4%9a%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50069\/","title":{"rendered":"Python \u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u540c\u4e00\u4e2a\u53d8\u91cf\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u7ed3\u679c\u76f8\u540c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u540c\u4e00\u4e2a\u53d8\u91cf\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u7ed3\u679c\u76f8\u540c\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPython \u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u540c\u4e00\u4e2a\u53d8\u91cf\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u7ed3\u679c\u76f8\u540c\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241111\/1731289187673160630dd40.jpg\" class=\"aligncenter\" title=\"Python \u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u540c\u4e00\u4e2a\u53d8\u91cf\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u7ed3\u679c\u76f8\u540c\uff1f\u63d2\u56fe\" alt=\"Python \u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u540c\u4e00\u4e2a\u53d8\u91cf\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u7ed3\u679c\u76f8\u540c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python\u7ebf\u7a0b\u91cd\u590d\u6267\u884c<\/strong><\/p>\n<p>\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u9884\u671f\u4e0d\u540c\u7684\u7ebf\u7a0b\u5c06\u4f9d\u6b21\u6267\u884c\uff0c\u4f46\u5b9e\u9645\u5374\u91cd\u590d\u6267\u884c\u4e86\u6700\u540e\u4e00\u4e2a\u7ebf\u7a0b\uff1a<\/p>\n<pre>import threading, time\n\nnum = 5\nlock = threading.lock()  # \u521b\u5efa\u540c\u6b65\u9501\nl = []\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(t.name)\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    l.append(t)\n\nfor t in l:\n    t.join()\n\nprint('s', num)<\/pre>\n<p>\u95ee\u9898\u6839\u6e90\u5728\u4e8e t \u53d8\u91cf<strong>\u5168\u5c40\u5f15\u7528<\/strong>\u7684\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>for \u5faa\u73af\u4e2d\uff0c\u6bcf\u6b21\u8fed\u4ee3\u90fd\u4f1a\u521b\u5efa\u5e76\u542f\u52a8\u65b0\u7ebf\u7a0b\u3002<\/li>\n<li>\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u8fdb\u5165 sub \u51fd\u6570\u65f6\u90fd\u4f1a\u83b7\u53d6\u5bf9 t \u7684\u5f15\u7528\u3002<\/li>\n<li>\u7531\u4e8e t \u662f\u5168\u5c40\u53d8\u91cf\uff0c\u5faa\u73af\u7684\u6700\u540e\u4e00\u6b21\u8fed\u4ee3\u540e\uff0ct \u5c06\u6307\u5411\u6700\u540e\u4e00\u4e2a\u521b\u5efa\u7684\u7ebf\u7a0b\uff08\u4e5f\u5c31\u662f\u7ebf\u7a0b 5\uff09\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u5f53\u4e0d\u540c\u7684\u7ebf\u7a0b\u8c03\u7528 sub \u51fd\u6570\u65f6\uff0c\u5b83\u4eec\u5b9e\u9645\u90fd\u5f15\u7528\u7684\u662f\u540c\u4e00\u4e2a t \u5bf9\u8c61\uff0c\u5bfc\u81f4\u6700\u540e\u4ec5\u6253\u5370\u51fa\u4e86\u7ebf\u7a0b 5 \u7684\u8f93\u51fa\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u663e\u5f0f\u4f20\u9012 t \u53c2\u6570\u6216\u4f7f\u7528 threading.current_thread() \u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u4fe1\u606f\u3002<\/p>\n<p><strong>\u4fee\u6539\u540e\u7684\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import threading, time\n\nnum = 5\nlock = threading.Lock()  # \u521b\u5efa\u540c\u6b65\u9501\n# L = []\n\ndef sub(t):\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    # L.append(t)\n\n# for T in L:\n#     T.join()\n\nprint('s', num)<\/pre>\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\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u540c\u4e00\u4e2a\u53d8\u91cf\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u7ed3\u679c\u76f8\u540c\uff1f \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPython \u7ebf\u7a0b\u91cd\u590d\u6267\u884c\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u540c\u4e00\u4e2a\u53d8\u91cf\u5bfc\u81f4\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u7ed3\u679c\u76f8\u540c\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 python\u7ebf\u7a0b\u91cd\u590d\u6267\u884c \u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u9884\u671f\u4e0d\u540c\u7684\u7ebf\u7a0b\u5c06\u4f9d\u6b21\u6267\u884c\uff0c\u4f46\u5b9e\u9645\u5374\u91cd\u590d\u6267\u884c\u4e86\u6700\u540e\u4e00\u4e2a\u7ebf\u7a0b\uff1a import threading, time num = 5 lock = threading.lock() # \u521b\u5efa\u540c\u6b65\u9501 l = [] def sub(): global num print(&#8216;sub %s&#8217; % t.name) lock.acquire() # \u83b7\u5f97\u540c\u6b65\u9501\uff1a\u4e0d\u8ba9\u522b\u7684\u7ebf\u7a0b\u5728\u540c\u4e00\u65f6\u523b\u8fd0\u884c print(t.name) temp = num time.sleep(1) num = temp &#8211; 1 lock.release() # \u89e3\u9664\u540c\u6b65\u9501 print(&#8216;%s &#8216; % t.name, num) for i in range(5): t = [&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-50069","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50069","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=50069"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50069\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}