{"id":1126398,"date":"2025-01-08T20:00:55","date_gmt":"2025-01-08T12:00:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126398.html"},"modified":"2025-01-08T20:00:57","modified_gmt":"2025-01-08T12:00:57","slug":"python%e4%b8%a4%e4%b8%aa%e7%ba%bf%e7%a8%8b%e4%b9%8b%e9%97%b4%e5%a6%82%e4%bd%95%e4%bc%a0%e9%80%92%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126398.html","title":{"rendered":"python\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5982\u4f55\u4f20\u9012\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090637\/8a0f3eaf-2c4d-4929-8bd8-77b48d992b68.webp\" alt=\"python\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5982\u4f55\u4f20\u9012\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u961f\u5217\uff08Queue\uff09\u3001\u5171\u4eab\u53d8\u91cf\u3001\u4e8b\u4ef6\uff08Event\uff09\u7b49\u3002\u4f7f\u7528\u961f\u5217\uff08Queue\uff09\u662f\u6700\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3001\u6613\u4e8e\u5b9e\u73b0\u3001\u65e0\u9700\u62c5\u5fc3\u9501\u673a\u5236\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u961f\u5217\uff08Queue\uff09<\/h3>\n<\/p>\n<p><h4>1\u3001\u961f\u5217\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>\u961f\u5217\u662fPython\u7684<code>queue<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u79cd\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\u3002\u5b83\u53ef\u4ee5\u5728\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u4e2d\u975e\u5e38\u6709\u6548\u5730\u4f20\u9012\u6570\u636e\u3002\u961f\u5217\u7684\u4f7f\u7528\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u7ba1\u7406\u9501\u673a\u5236\uff0c\u56e0\u4e3a<code>queue<\/code>\u6a21\u5757\u5df2\u7ecf\u5904\u7406\u4e86\u8fd9\u4e9b\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5982\u4f55\u4f7f\u7528\u961f\u5217<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165<code>queue<\/code>\u6a21\u5757\u5e76\u521b\u5efa\u4e00\u4e2a\u961f\u5217\u5b9e\u4f8b\u3002\u7136\u540e\uff0c\u5728\u7ebf\u7a0b\u4e4b\u95f4\u901a\u8fc7\u8be5\u961f\u5217\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>def producer(q):<\/p>\n<p>    for i in range(5):<\/p>\n<p>        q.put(i)<\/p>\n<p>        print(f&#39;Produced {i}&#39;)<\/p>\n<p>def consumer(q):<\/p>\n<p>    while True:<\/p>\n<p>        item = q.get()<\/p>\n<p>        if item is None:<\/p>\n<p>            break<\/p>\n<p>        print(f&#39;Consumed {item}&#39;)<\/p>\n<p>        q.task_done()<\/p>\n<p>q = queue.Queue()<\/p>\n<p>t1 = threading.Thread(target=producer, args=(q,))<\/p>\n<p>t2 = threading.Thread(target=consumer, args=(q,))<\/p>\n<p>t1.start()<\/p>\n<p>t2.start()<\/p>\n<p>t1.join()<\/p>\n<p>q.put(None)  # \u7528\u4e8e\u901a\u77e5\u6d88\u8d39\u8005\u9000\u51fa<\/p>\n<p>t2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u961f\u5217\u5b9e\u4f8b<code>q<\/code>\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e24\u4e2a\u7ebf\u7a0b\uff1a\u751f\u4ea7\u8005\u7ebf\u7a0b\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b\u3002\u751f\u4ea7\u8005\u7ebf\u7a0b\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6570\u636e\u5e76\u5904\u7406\u3002<code>q.put(None)<\/code>\u7528\u4e8e\u901a\u77e5\u6d88\u8d39\u8005\u7ebf\u7a0b\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5171\u4eab\u53d8\u91cf<\/h3>\n<\/p>\n<p><h4>1<\/h4>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u6570\u636e\u5171\u4eab\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u5728\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3001\u961f\u5217\uff08Queue\uff09\u3001\u4ee5\u53ca\u7ebf\u7a0b\u9501\uff08Lock\uff09\u3002\u5168\u5c40\u53d8\u91cf\u5141\u8bb8\u6240\u6709\u7ebf\u7a0b\u8bbf\u95ee\u76f8\u540c\u7684\u6570\u636e\uff0c\u800c\u961f\u5217\u63d0\u4f9b\u4e86\u4e00\u79cd\u7ebf\u7a0b\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u4f20\u9012\u6570\u636e\u3002\u7ebf\u7a0b\u9501\u5219\u7528\u4e8e\u786e\u4fdd\u5728\u67d0\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u907f\u514d\u6570\u636e\u7ade\u4e89\u3002<\/p>\n<p><strong>\u4f7f\u7528Queue\u6a21\u5757\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>Queue\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u5b9e\u73b0\uff0c\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u5b89\u5168\u5730\u653e\u5165\u548c\u53d6\u51fa\u6570\u636e\u3002\u4f7f\u7528\u961f\u5217\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u6570\u636e\u51b2\u7a81\u548c\u7ade\u4e89\u6761\u4ef6\uff0c\u56e0\u4e3a\u5b83\u5185\u90e8\u5df2\u7ecf\u5b9e\u73b0\u4e86\u5fc5\u8981\u7684\u9501\u673a\u5236\u3002\u8fd9\u4f7f\u5f97\u7ebf\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u5b89\u5168\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u4e8b\u4ef6\uff08Event\uff09\u6765\u540c\u6b65\u7ebf\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\uff1f<\/strong><br \/>\u4e8b\u4ef6\uff08Event\uff09\u662fPython threading\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u540c\u6b65\u539f\u8bed\uff0c\u53ef\u4ee5\u7528\u4e8e\u7ebf\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u3002\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u7b49\u5f85\u67d0\u4e2a\u4e8b\u4ef6\u7684\u53d1\u751f\uff0c\u800c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5728\u9002\u5f53\u7684\u65f6\u5019\u89e6\u53d1\u8be5\u4e8b\u4ef6\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u67d0\u4e9b\u6761\u4ef6\u6ee1\u8db3\u4e4b\u524d\uff0c\u7ebf\u7a0b\u4e0d\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u8fdb\u884c\u6570\u636e\u4f20\u9012\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u961f\u5217\uff08Queue\uff09\u3001\u5171\u4eab\u53d8\u91cf\u3001\u4e8b\u4ef6\uff08Event\uff09\u7b49\u3002\u4f7f\u7528\u961f\u5217\uff08Q [&hellip;]","protected":false},"author":3,"featured_media":1126405,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126398"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1126398"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126398\/revisions"}],"predecessor-version":[{"id":1126408,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126398\/revisions\/1126408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126405"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}