{"id":204725,"date":"2025-05-29T09:20:47","date_gmt":"2025-05-29T01:20:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204725\/"},"modified":"2025-05-29T09:20:47","modified_gmt":"2025-05-29T01:20:47","slug":"%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d-python-%e7%ba%bf%e7%a8%8b%e6%b1%a0%e7%88%ac%e8%99%ab%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae%e7%b4%8a%e4%b9%b1%e9%97%ae%e9%a2%98%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204725\/","title":{"rendered":"\u5982\u4f55\u907f\u514d Python \u7ebf\u7a0b\u6c60\u722c\u866b\u4e2d\u7684\u6570\u636e\u7d0a\u4e71\u95ee\u9898\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u907f\u514d Python \u7ebf\u7a0b\u6c60\u722c\u866b\u4e2d\u7684\u6570\u636e\u7d0a\u4e71\u95ee\u9898\uff1f<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u907f\u514d Python \u7ebf\u7a0b\u6c60\u722c\u866b\u4e2d\u7684\u6570\u636e\u7d0a\u4e71\u95ee\u9898\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241114\/17315770136735c4b545d5f.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u7ebf\u7a0b\u6c60\u722c\u866b\uff1a\u6570\u636e\u7d0a\u4e71\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 python \u7ebf\u7a0b\u6c60\u5b9e\u73b0\u722c\u866b\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6570\u636e\u7d0a\u4e71\u95ee\u9898\u3002\u8fd9\u662f\u56e0\u4e3a\u591a\u7ebf\u7a0b\u7684\u6267\u884c\u987a\u5e8f\u4e0d\u4e00\u81f4\uff0c\u5bfc\u81f4\u54cd\u5e94\u5904\u7406\u548c\u89e3\u6790\u7684\u987a\u5e8f\u4e0d\u6b63\u786e\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a\u4f7f\u7528\u6709\u5e8f\u961f\u5217<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u6709\u5e8f\u961f\u5217\u3002\u6709\u5e8f\u961f\u5217\u6309\u7167\u5143\u7d20\u63d2\u5165\u7684\u987a\u5e8f\u4fdd\u5b58\u5143\u7d20\uff0c\u4ece\u800c\u786e\u4fdd\u54cd\u5e94\u5904\u7406\u548c\u89e3\u6790\u7684\u987a\u5e8f\u51c6\u786e\u3002<\/p>\n<p>\u4e0b\u9762\u63d0\u4f9b\u4fee\u6539\u540e\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u4f7f\u7528\u6709\u5e8f\u961f\u5217\u6765\u6709\u5e8f\u5904\u7406\u54cd\u5e94\uff1a<\/p>\n<pre>import queue\n\nclass Response:\n    def __init__(self, data, index):\n        self.data = data\n        self.index = index\n    \n    def __lt__(self, other):\n        return self.index &lt; other.index\n        \nq = queue.PriorityQueue()  # \u521b\u5efa\u6709\u5e8f\u961f\u5217\n\ndef get_response(i):\n    # \u83b7\u53d6\u54cd\u5e94\u5e76\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\n    response = YourResponseFunction(i)\n    q.put(Response(response, i))\n\ndef parse_response(q):\n    while not q.empty():\n        response = q.get()\n        # \u5904\u7406\u54cd\u5e94\u5e76\u5b58\u50a8\u6570\u636e\n        parse(response.data)\n\n# \u521b\u5efa\u7ebf\u7a0b\u6c60\npool = ThreadPoolExecutor(30)\n\n# \u5411\u7ebf\u7a0b\u6c60\u63d0\u4ea4\u4efb\u52a1\nfor i in range(idqueue.qsize()):\n    pool.submit(get_response, i)\n\n# \u7528\u961f\u5217\u4e2d\u7684\u54cd\u5e94\u5904\u7406\u6570\u636e\nparse_response(q)\n<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86python\u7684 priorityqueue\uff08\u4f18\u5148\u961f\u5217\uff09\uff0c\u5b83\u6309\u7167\u5143\u7d20\u7684 index \u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u6837\uff0c\u54cd\u5e94\u5c06\u6309\u7167\u5230\u8fbe\u987a\u5e8f\u63d2\u5165\u548c\u5904\u7406\u3002<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u6709\u5e8f\u961f\u5217\uff0c\u60a8\u53ef\u4ee5\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u786e\u4fdd\u54cd\u5e94\u7684\u987a\u5e8f\u6b63\u786e\uff0c\u4ece\u800c\u907f\u514d\u6570\u636e\u7d0a\u4e71\u95ee\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u907f\u514d Python \u7ebf\u7a0b\u6c60\u722c\u866b\u4e2d\u7684\u6570\u636e\u7d0a\u4e71\u95ee\u9898\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u907f\u514d Python \u7ebf\u7a0b\u6c60\u722c&#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-204725","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204725","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=204725"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204725\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}