{"id":2021,"date":"2024-10-13T22:45:06","date_gmt":"2024-10-13T14:45:06","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=2021"},"modified":"2024-10-13T22:45:06","modified_gmt":"2024-10-13T14:45:06","slug":"%e4%bb%bb%e5%8a%a1%e9%98%9f%e5%88%97-modelqueue","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2024\/10\/13\/%e4%bb%bb%e5%8a%a1%e9%98%9f%e5%88%97-modelqueue\/","title":{"rendered":"\u4efb\u52a1\u961f\u5217 ModelQueue"},"content":{"rendered":"\n<p>\u63d0\u4f9b\u4e00\u4e2a\u793a\u4f8b\u7684&nbsp;model_queue&nbsp;\u7c7b\u7684\u5b9e\u73b0\uff0c\u5e76\u89e3\u91ca\u5176\u5de5\u4f5c\u539f\u7406\u3002\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u60a8\u6ca1\u6709\u63d0\u4f9b&nbsp;model_queue&nbsp;\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u6211\u5c06\u57fa\u4e8e\u5e38\u89c1\u7684\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u6a21\u5f0f\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b&nbsp;model_queue&nbsp;\u7c7b<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u53ef\u80fd\u7684\u00a0model_queue\u00a0\u7c7b\u7684\u5b9e\u73b0\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\n\nclass ModelQueue:\n    def __init__(self):\n        self.queue = asyncio.Queue()  # \u4f7f\u7528 asyncio.Queue \u6765\u7ba1\u7406\u4efb\u52a1\n\n    async def add_task(self, model, task):\n        \"\"\"\n        \u5c06\u4efb\u52a1\u6dfb\u52a0\u5230\u961f\u5217\u5e76\u6267\u884c\u3002\n\n        Args:\n            model: \u8981\u4f7f\u7528\u7684\u6a21\u578b\u3002\n            task: \u8981\u6267\u884c\u7684\u5f02\u6b65\u4efb\u52a1\u3002\n\n        Returns:\n            \u4efb\u52a1\u7684\u7ed3\u679c\u3002\n        \"\"\"\n        # \u5c06\u4efb\u52a1\u653e\u5165\u961f\u5217\n        await self.queue.put((model, task))\n        return await self.process_queue()  # \u5904\u7406\u961f\u5217\u4e2d\u7684\u4efb\u52a1\n\n    async def process_queue(self):\n        \"\"\"\n        \u5904\u7406\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u3002\n\n        Returns:\n            \u4efb\u52a1\u7684\u7ed3\u679c\u3002\n        \"\"\"\n        while not self.queue.empty():\n            model, task = await self.queue.get()  # \u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u4efb\u52a1\n            try:\n                result = await task()  # \u6267\u884c\u4efb\u52a1\n                return result  # \u8fd4\u56de\u4efb\u52a1\u7684\u7ed3\u679c\n            except Exception as e:\n                print(f\"Error processing task for model {model}: {e}\")\n            finally:\n                self.queue.task_done()  # \u6807\u8bb0\u4efb\u52a1\u5b8c\u6210<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u91ca<\/h3>\n\n\n\n<p>\u961f\u5217\u521d\u59cb\u5316\uff1aself.queue = asyncio.Queue()\uff1a\u4f7f\u7528\u00a0asyncio.Queue\u00a0\u6765\u7ba1\u7406\u5f02\u6b65\u4efb\u52a1\u3002\u8fd9\u4e2a\u961f\u5217\u53ef\u4ee5\u5b58\u50a8\u591a\u4e2a\u4efb\u52a1\uff0c\u5e76\u5728\u9700\u8981\u65f6\u9010\u4e2a\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u6dfb\u52a0\u4efb\u52a1\uff1aasync def add_task(self, model, task)\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u4efb\u52a1\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\uff0c\u5e76\u8c03\u7528\u00a0process_queue\u00a0\u65b9\u6cd5\u6765\u5904\u7406\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>await self.queue.put((model, task))\uff1a\u5c06\u6a21\u578b\u548c\u4efb\u52a1\u5143\u7ec4\u653e\u5165\u961f\u5217\u3002<\/p>\n\n\n\n<p>\u5904\u7406\u961f\u5217\uff1aasync def process_queue(self)\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u5904\u7406\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>while not self.queue.empty()\uff1a\u5faa\u73af\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\u3002<\/p>\n\n\n\n<p>model, task = await self.queue.get()\uff1a\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>result = await task()\uff1a\u6267\u884c\u4efb\u52a1\u5e76\u7b49\u5f85\u5176\u5b8c\u6210\u3002<\/p>\n\n\n\n<p>self.queue.task_done()\uff1a\u6807\u8bb0\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u4f9b\u4e00\u4e2a\u793a\u4f8b\u7684&nbsp;model_queue&nbsp;\u7c7b\u7684\u5b9e\u73b0\uff0c\u5e76\u89e3\u91ca\u5176\u5de5\u4f5c\u539f\u7406\u3002\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u60a8\u6ca1\u6709\u63d0\u4f9b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-2021","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2021"}],"version-history":[{"count":1,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2021\/revisions"}],"predecessor-version":[{"id":2022,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2021\/revisions\/2022"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2021"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}