{"id":1116102,"date":"2025-01-08T18:13:21","date_gmt":"2025-01-08T10:13:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116102.html"},"modified":"2025-01-08T18:13:25","modified_gmt":"2025-01-08T10:13:25","slug":"python2%e5%8d%8f%e7%a8%8b%e5%a6%82%e4%bd%95%e6%8e%a7%e5%88%b6%e5%b9%b6%e5%8f%91%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1116102.html","title":{"rendered":"python2\u534f\u7a0b\u5982\u4f55\u63a7\u5236\u5e76\u53d1\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080600\/9e3987ff-7d80-45a4-a5d0-0e1e0275bd62.webp\" alt=\"python2\u534f\u7a0b\u5982\u4f55\u63a7\u5236\u5e76\u53d1\u6570\" \/><\/p>\n<p><p> <strong>Python2\u4e2d\u7684\u534f\u7a0b\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u548c\u7b2c\u4e09\u65b9\u5e93gevent\u6765\u5b9e\u73b0\uff0c\u63a7\u5236\u5e76\u53d1\u6570\u7684\u5173\u952e\u5728\u4e8e\u9650\u5236\u534f\u7a0b\u7684\u6570\u91cf\u3001\u4f7f\u7528\u961f\u5217\u7ba1\u7406\u4efb\u52a1\u3001\u7ed3\u5408\u4fe1\u53f7\u91cf\u3002<\/strong> \u8fd9\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u6c60\u6765\u7ba1\u7406\u534f\u7a0b\u7684\u6570\u91cf\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c\u961f\u5217\u7ba1\u7406\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u5728Python2\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u521b\u5efa\u534f\u7a0b\uff0c\u5e76\u4f7f\u7528<code>Queue<\/code>\u6765\u7ba1\u7406\u4efb\u52a1\u3002\u901a\u8fc7\u63a7\u5236\u961f\u5217\u7684\u957f\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u9650\u5236\u5e76\u53d1\u6267\u884c\u7684\u534f\u7a0b\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u751f\u6210\u5668\u534f\u7a0b<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662fPython\u4e2d\u7528\u4e8e\u521b\u5efa\u8fed\u4ee3\u5668\u7684\u4e00\u79cd\u7b80\u5355\u800c\u5f3a\u5927\u7684\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u521b\u5efa\u534f\u7a0b\uff0c\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u6765\u6302\u8d77\u548c\u6062\u590d\u51fd\u6570\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def my_coroutine(task):<\/p>\n<p>    print(f&quot;Starting task {task}&quot;)<\/p>\n<p>    time.sleep(1)  # Simulating I\/O-bound task<\/p>\n<p>    print(f&quot;Task {task} done&quot;)<\/p>\n<p>    yield<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u961f\u5217\u7ba1\u7406\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>Python\u7684<code>Queue<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684FIFO\u961f\u5217\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u7ba1\u7406\u548c\u8c03\u5ea6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Queue import Queue<\/p>\n<p>from threading import Thread<\/p>\n<p>task_queue = Queue()<\/p>\n<p>def worker():<\/p>\n<p>    while True:<\/p>\n<p>        task = task_queue.get()<\/p>\n<p>        if task is None:<\/p>\n<p>            break<\/p>\n<p>        my_coroutine(task).next()<\/p>\n<p>        task_queue.task_done()<\/p>\n<p>num_worker_threads = 3<\/p>\n<p>threads = []<\/p>\n<p>for i in range(num_worker_threads):<\/p>\n<p>    t = Thread(target=worker)<\/p>\n<p>    t.start()<\/p>\n<p>    threads.append(t)<\/p>\n<h2><strong>Enqueue tasks<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    task_queue.put(i)<\/p>\n<h2><strong>Block until all tasks are done<\/strong><\/h2>\n<p>task_queue.join()<\/p>\n<h2><strong>Stop workers<\/strong><\/h2>\n<p>for i in range(num_worker_threads):<\/p>\n<p>    task_queue.put(None)<\/p>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528gevent\u5e93<\/h3>\n<\/p>\n<p><p>Gevent\u662f\u4e00\u4e2a\u57fa\u4e8elibev\u6216libuv\u7684Python\u7f51\u7edc\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8f7b\u91cf\u7ea7\u7684\u534f\u7a0b\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3002Gevent\u4e2d\u7684\u534f\u7a0b\u662f\u901a\u8fc7monkey patching\u6765\u5b9e\u73b0\u7684\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u80fd\u591f\u5bf9\u6807\u51c6\u5e93\u4e2d\u7684I\/O\u64cd\u4f5c\u8fdb\u884c\u975e\u963b\u585e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5gevent<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install gevent<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528gevent\u5b9e\u73b0\u534f\u7a0b\u548c\u63a7\u5236\u5e76\u53d1\u6570<\/h4>\n<\/p>\n<p><p>Gevent\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Pool<\/code>\u7c7b\u6765\u9650\u5236\u5e76\u53d1\u534f\u7a0b\u7684\u6570\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u63a7\u5236\u5e76\u53d1\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gevent<\/p>\n<p>from gevent.pool import Pool<\/p>\n<p>import time<\/p>\n<p>def my_coroutine(task):<\/p>\n<p>    print(f&quot;Starting task {task}&quot;)<\/p>\n<p>    time.sleep(1)  # Simulating I\/O-bound task<\/p>\n<p>    print(f&quot;Task {task} done&quot;)<\/p>\n<p>pool = Pool(3)  # Limit the number of concurrent coroutines to 3<\/p>\n<p>tasks = [pool.spawn(my_coroutine, i) for i in range(10)]<\/p>\n<h2><strong>W<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t for all tasks to complete<\/strong><\/h2>\n<p>gevent.joinall(tasks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u4fe1\u53f7\u91cf\u63a7\u5236\u5e76\u53d1\u6570<\/h3>\n<\/p>\n<p><p>\u4fe1\u53f7\u91cf\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5e76\u53d1\u63a7\u5236\u673a\u5236\uff0c\u7528\u4e8e\u9650\u5236\u5bf9\u8d44\u6e90\u7684\u8bbf\u95ee\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4fe1\u53f7\u91cf\u6765\u63a7\u5236\u534f\u7a0b\u7684\u5e76\u53d1\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u4fe1\u53f7\u91cf\u63a7\u5236\u5e76\u53d1\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import gevent<\/p>\n<p>from gevent.lock import BoundedSemaphore<\/p>\n<p>sem = BoundedSemaphore(3)  # Allow only 3 coroutines to run concurrently<\/p>\n<p>def my_coroutine(task):<\/p>\n<p>    with sem:<\/p>\n<p>        print(f&quot;Starting task {task}&quot;)<\/p>\n<p>        time.sleep(1)  # Simulating I\/O-bound task<\/p>\n<p>        print(f&quot;Task {task} done&quot;)<\/p>\n<p>tasks = [gevent.spawn(my_coroutine, i) for i in range(10)]<\/p>\n<h2><strong>Wait for all tasks to complete<\/strong><\/h2>\n<p>gevent.joinall(tasks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python2\u4e2d\uff0c\u867d\u7136\u6ca1\u6709\u50cfPython3\u4e2d\u7684asyncio\u90a3\u6837\u5b8c\u5584\u7684\u539f\u751f\u534f\u7a0b\u652f\u6301\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u5668\u3001\u961f\u5217\u3001\u7b2c\u4e09\u65b9\u5e93gevent\u4ee5\u53ca\u4fe1\u53f7\u91cf\u7b49\u673a\u5236\u6765\u5b9e\u73b0\u534f\u7a0b\u5e76\u63a7\u5236\u5e76\u53d1\u6570\u3002<strong>\u751f\u6210\u5668\u548c\u961f\u5217\u7ba1\u7406\u4efb\u52a1\u3001\u4f7f\u7528gevent\u5e93\u3001\u7ed3\u5408\u4fe1\u53f7\u91cf\u63a7\u5236\u5e76\u53d1\u6570<\/strong>\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u9879\u76ee\u73af\u5883\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u9ad8\u6548\u7684\u5e76\u53d1\u63a7\u5236\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python2\u4e2d\u5b9e\u73b0\u534f\u7a0b\u7684\u5e76\u53d1\u63a7\u5236\uff1f<\/strong><br \/>\u5728Python2\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>gevent<\/code>\u6216<code>eventlet<\/code>\u7b49\u5e93\u6765\u5b9e\u73b0\u534f\u7a0b\u7684\u5e76\u53d1\u63a7\u5236\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u57fa\u4e8e\u7eff\u8272\u7ebf\u7a0b\u7684\u534f\u4f5c\u5f0f\u8c03\u5ea6\u673a\u5236\uff0c\u5141\u8bb8\u4f60\u8bbe\u5b9a\u540c\u65f6\u8fd0\u884c\u7684\u534f\u7a0b\u6570\u91cf\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\u6c60\uff08\u5982<code>gevent.pool.Pool<\/code>\uff09\u6765\u9650\u5236\u5e76\u53d1\u6570\u76ee\uff0c\u786e\u4fdd\u5728\u6267\u884c\u591a\u4e2a\u534f\u7a0b\u65f6\uff0c\u7cfb\u7edf\u8d44\u6e90\u5f97\u5230\u5408\u7406\u4f7f\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528Python2\u7684\u534f\u7a0b\u65f6\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f<\/strong><br \/>\u5728Python2\u7684\u534f\u7a0b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u90e8\u5206\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u534f\u7a0b\u4e2d\u7684\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u5355\u4e2a\u534f\u7a0b\u7684\u9519\u8bef\u800c\u5d29\u6e83\u3002\u901a\u8fc7\u5728\u6bcf\u4e2a\u534f\u7a0b\u5185\u8fdb\u884c\u9002\u5f53\u7684\u5f02\u5e38\u6355\u83b7\u548c\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e76\u5728\u53d1\u751f\u9519\u8bef\u65f6\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\uff0c\u5982\u91cd\u8bd5\u6216\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<p><strong>\u5728Python2\u534f\u7a0b\u4e2d\uff0c\u5982\u4f55\u4fdd\u8bc1\u4efb\u52a1\u7684\u987a\u5e8f\u6267\u884c\uff1f<\/strong><br \/>\u5982\u679c\u5728Python2\u7684\u534f\u7a0b\u4e2d\u9700\u8981\u786e\u4fdd\u4efb\u52a1\u6309\u7167\u7279\u5b9a\u7684\u987a\u5e8f\u6267\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>gevent.joinall()<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u534f\u7a0b\u4efb\u52a1\u653e\u5165\u4e00\u4e2a\u5217\u8868\uff0c\u5e76\u5728\u6267\u884c\u540e\u8c03\u7528<code>joinall()<\/code>\uff0c\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u534f\u7a0b\u6267\u884c\u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u540e\u7eed\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u961f\u5217\u6765\u7ba1\u7406\u4efb\u52a1\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u7ec6\u81f4\u7684\u987a\u5e8f\u63a7\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python2\u4e2d\u7684\u534f\u7a0b\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u548c\u7b2c\u4e09\u65b9\u5e93gevent\u6765\u5b9e\u73b0\uff0c\u63a7\u5236\u5e76\u53d1\u6570\u7684\u5173\u952e\u5728\u4e8e\u9650\u5236\u534f\u7a0b\u7684\u6570\u91cf\u3001\u4f7f\u7528\u961f\u5217 [&hellip;]","protected":false},"author":3,"featured_media":1116112,"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\/1116102"}],"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=1116102"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116102\/revisions"}],"predecessor-version":[{"id":1116113,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116102\/revisions\/1116113"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116112"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}