{"id":2845,"date":"2025-03-16T12:26:17","date_gmt":"2025-03-16T04:26:17","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2845"},"modified":"2025-03-16T12:26:17","modified_gmt":"2025-03-16T04:26:17","slug":"python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b12%ef%bc%9a%e5%bc%82%e6%ad%a5%e9%98%9f%e5%88%97%e5%92%8c%e4%bb%bb%e5%8a%a1%e8%b0%83%e5%ba%a6%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b12%ef%bc%9a%e5%bc%82%e6%ad%a5%e9%98%9f%e5%88%97%e5%92%8c%e4%bb%bb%e5%8a%a1%e8%b0%83%e5%ba%a6%e7%b3%bb%e7%bb%9f\/","title":{"rendered":"Python3 \u7f51\u7edc\u7f16\u7a0b12\uff1a\u5f02\u6b65\u961f\u5217\u548c\u4efb\u52a1\u8c03\u5ea6\u7cfb\u7edf"},"content":{"rendered":"\n<p>\u5728\u9ad8\u5e76\u53d1\u6216\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5f02\u6b65\u961f\u5217\u548c\u4efb\u52a1\u8c03\u5ea6\u7cfb\u7edf\u7528\u4e8e\u7ba1\u7406\u548c\u8c03\u5ea6\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5206\u5e03\u5f0f\u6267\u884c\u548c\u5f02\u6b65\u5904\u7406\u65f6\u3002\u4f7f\u7528\u961f\u5217\u53ef\u4ee5\u786e\u4fdd\u4efb\u52a1\u7684\u987a\u5e8f\u6267\u884c\u548c\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u5b9e\u73b0\u5f02\u6b65\u961f\u5217\u548c\u4efb\u52a1\u8c03\u5ea6\uff0c<code>asyncio<\/code> \u548c <code>Celery<\/code> \u662f\u6700\u5e38\u89c1\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>12.1 \u4f7f\u7528 <code>asyncio<\/code> \u5b9e\u73b0\u5f02\u6b65\u961f\u5217<\/strong><\/h2>\n\n\n\n<p><code>asyncio.Queue<\/code> \u662f Python \u539f\u751f\u652f\u6301\u7684\u5f02\u6b65\u961f\u5217\uff0c\u7528\u4e8e\u5728\u4efb\u52a1\u95f4\u4f20\u9012\u6570\u636e\u3002\u5b83\u57fa\u4e8e <code>asyncio<\/code> \u4e8b\u4ef6\u5faa\u73af\uff0c\u5e76\u4e14\u652f\u6301\u5f02\u6b65\u7684 <code>put()<\/code> \u548c <code>get()<\/code> \u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.1.1 \u5f02\u6b65\u961f\u5217\u57fa\u672c\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u751f\u4ea7\u8005\u4e0e\u6d88\u8d39\u8005\u6a21\u5f0f<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u751f\u4ea7\u8005<\/strong>\u5c06\u4efb\u52a1\u653e\u5165\u961f\u5217\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u6d88\u8d39\u8005<\/strong>\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u4efb\u52a1\u5e76\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\n\nasync def producer(queue):\n    for i in range(5):\n        await asyncio.sleep(1)  # \u6a21\u62df\u4efb\u52a1\u751f\u4ea7\n        await queue.put(i)\n        print(f\"\u751f\u4ea7\u4efb\u52a1 {i}\")\n\nasync def consumer(queue):\n    while True:\n        task = await queue.get()\n        if task is None:\n            break  # \u7528 None \u4f5c\u4e3a\u7ed3\u675f\u6807\u5fd7\n        print(f\"\u6d88\u8d39\u4efb\u52a1 {task}\")\n        await asyncio.sleep(2)  # \u6a21\u62df\u4efb\u52a1\u5904\u7406\n        queue.task_done()\n\nasync def main():\n    queue = asyncio.Queue()\n    prod = asyncio.create_task(producer(queue))\n    cons = asyncio.create_task(consumer(queue))\n    await prod\n    await queue.join()  # \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\n    cons.cancel()  # \u53d6\u6d88\u6d88\u8d39\u8005\u4efb\u52a1\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(main())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.1.2 \u5f02\u6b65\u961f\u5217\u7684\u7279\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f02\u6b65\u961f\u5217\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u4efb\u52a1\u8c03\u5ea6\u3002<\/li>\n\n\n\n<li>\u9002\u7528\u4e8e\u9700\u8981\u5f02\u6b65\u5904\u7406\u7684\u573a\u666f\uff0c\u5982\u591a\u4e2a\u4efb\u52a1\u7684\u5206\u53d1\u4e0e\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u652f\u6301\u961f\u5217\u957f\u5ea6\u9650\u5236\uff0c\u53ef\u4ee5\u907f\u514d\u4efb\u52a1\u5806\u79ef\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>12.2 \u4f7f\u7528 <code>Celery<\/code> \u5b9e\u73b0\u4efb\u52a1\u961f\u5217\u548c\u8c03\u5ea6\u7cfb\u7edf<\/strong><\/h2>\n\n\n\n<p><code>Celery<\/code> \u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u4efb\u52a1\u961f\u5217\uff0c\u5b83\u975e\u5e38\u9002\u7528\u4e8e\u9700\u8981\u5f02\u6b65\u4efb\u52a1\u5904\u7406\u7684\u573a\u666f\uff0c\u5e76\u4e14\u652f\u6301\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.2.1 \u5b89\u88c5\u548c\u914d\u7f6e Celery<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u5b89\u88c5 Celery\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install celery<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u914d\u7f6e\u4e00\u4e2a\u7b80\u5355\u7684 Celery \u5e94\u7528\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4efb\u52a1\u5b9a\u4e49<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from celery import Celery\n\napp = Celery('tasks', broker='redis:\/\/localhost:6379\/0')\n\n@app.task\ndef add(x, y):\n    return x + y<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4efb\u52a1 <code>add<\/code>\uff0c\u5b83\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.2.2 \u8fd0\u884c Celery worker<\/strong><\/h3>\n\n\n\n<p>\u5728\u547d\u4ee4\u884c\u542f\u52a8 Celery worker\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>celery -A tasks worker --loglevel=info<\/code><\/pre>\n\n\n\n<p>Celery worker \u4f1a\u76d1\u542c Redis \u4e2d\u7684\u4efb\u52a1\u961f\u5217\uff0c\u7b49\u5f85\u63a5\u6536\u548c\u6267\u884c\u4efb\u52a1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.2.3 \u8c03\u7528\u4efb\u52a1<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7 Celery \u53d1\u9001\u5f02\u6b65\u4efb\u52a1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from tasks import add\n\nresult = add.delay(4, 6)  # \u53d1\u9001\u5f02\u6b65\u4efb\u52a1\nprint(f\"\u4efb\u52a1\u8fd4\u56de\u503c: {result.get(timeout=10)}\")  # \u83b7\u53d6\u4efb\u52a1\u7ed3\u679c<\/code><\/pre>\n\n\n\n<p><code>add.delay(4, 6)<\/code> \u4f1a\u5c06\u4efb\u52a1 <code>add<\/code> \u5f02\u6b65\u53d1\u9001\u5230 Celery \u961f\u5217\u4e2d\u8fdb\u884c\u6267\u884c\uff0c<code>result.get(timeout=10)<\/code> \u4f1a\u7b49\u5f85\u4efb\u52a1\u7684\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.2.4 \u5b9a\u65f6\u4efb\u52a1<\/strong><\/h3>\n\n\n\n<p>Celery \u8fd8\u652f\u6301\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>celery.beat<\/code> \u5b9e\u73b0\u4efb\u52a1\u5b9a\u65f6\u6267\u884c\u3002\u914d\u7f6e Celery Beat \u548c Celery Worker \u7684\u8c03\u5ea6\u529f\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from celery.schedules import crontab\n\napp.conf.beat_schedule = {\n    'add-every-10-seconds': {\n        'task': 'tasks.add',\n        'schedule': crontab(minute='*\/1'),  # \u6bcf\u5206\u949f\u6267\u884c\u4e00\u6b21\n        'args': (16, 16),\n    },\n}\n\napp.conf.timezone = 'UTC'<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>12.3 \u9ad8\u7ea7\u7528\u6cd5\uff1a\u4efb\u52a1\u4f18\u5148\u7ea7\u548c\u7ed3\u679c\u5b58\u50a8<\/strong><\/h2>\n\n\n\n<p>\u5728 Celery \u4e2d\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4efb\u52a1\u4f18\u5148\u7ea7\uff0c\u5e76\u4e14\u5b58\u50a8\u4efb\u52a1\u7ed3\u679c\u4ee5\u4f9b\u540e\u7eed\u67e5\u8be2\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.3.1 \u4efb\u52a1\u4f18\u5148\u7ea7<\/strong><\/h3>\n\n\n\n<p>Celery \u652f\u6301\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u961f\u5217\u914d\u7f6e\uff0c\u53ef\u4ee5\u63a7\u5236\u4efb\u52a1\u7684\u6267\u884c\u987a\u5e8f\u3002\u4f7f\u7528 <code>priority<\/code> \u53c2\u6570\u8bbe\u7f6e\u4efb\u52a1\u4f18\u5148\u7ea7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from tasks import add\n\nresult = add.apply_async((4, 6), priority=10)  # \u8bbe\u7f6e\u4f18\u5148\u7ea7<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.3.2 \u4efb\u52a1\u7ed3\u679c\u5b58\u50a8<\/strong><\/h3>\n\n\n\n<p>Celery \u8fd8\u53ef\u4ee5\u5c06\u4efb\u52a1\u7ed3\u679c\u5b58\u50a8\u5230\u6570\u636e\u5e93\u6216\u7f13\u5b58\u7cfb\u7edf\u4e2d\uff08\u5982 Redis\u3001MongoDB\uff09\u3002\u8fd9\u5bf9\u4e8e\u540e\u7eed\u7684\u4efb\u52a1\u7ed3\u679c\u67e5\u8be2\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.conf.result_backend = 'redis:\/\/localhost:6379\/0'<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>12.4 \u4efb\u52a1\u8c03\u5ea6\u548c\u961f\u5217\u4f18\u5316<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.4.1 \u4f18\u5316\u4efb\u52a1\u961f\u5217<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4efb\u52a1\u5206\u914d<\/strong>\uff1a\u901a\u8fc7\u8bbe\u7f6e\u4f18\u5148\u7ea7\uff0c\u53ef\u4ee5\u786e\u4fdd\u5173\u952e\u4efb\u52a1\u5f97\u5230\u4f18\u5148\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>\u8d1f\u8f7d\u5747\u8861<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7 Celery \u7684\u81ea\u52a8\u8c03\u5ea6\u7cfb\u7edf\uff08\u5982 Flower\uff09\u76d1\u63a7\u4efb\u52a1\u8d1f\u8f7d\uff0c\u786e\u4fdd\u4efb\u52a1\u5206\u914d\u5747\u8861\u3002<\/li>\n\n\n\n<li><strong>\u9650\u6d41\u63a7\u5236<\/strong>\uff1a\u9650\u5236\u4efb\u52a1\u7684\u5e76\u53d1\u91cf\uff0c\u907f\u514d\u7cfb\u7edf\u8fc7\u8f7d\uff0c\u786e\u4fdd\u4efb\u52a1\u6309\u987a\u5e8f\u6709\u5e8f\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12.4.2 \u4efb\u52a1\u8c03\u5ea6<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7\u8c03\u5ea6\u5668\uff08\u5982 Celery Beat\uff09\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5b89\u6392\u5468\u671f\u4efb\u52a1\u3001\u5b9a\u65f6\u4efb\u52a1\u7b49\uff0c\u51cf\u5c11\u4eba\u5de5\u5e72\u9884\uff0c\u63d0\u9ad8\u81ea\u52a8\u5316\u7a0b\u5ea6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>asyncio.Queue<\/code><\/strong> \u9002\u5408\u4e8e\u5355\u673a\u9ad8\u5e76\u53d1\u7684\u5f02\u6b65\u961f\u5217\uff0c\u80fd\u591f\u5728\u5185\u5b58\u4e2d\u8fdb\u884c\u9ad8\u6548\u7684\u4efb\u52a1\u5206\u914d\u548c\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong><code>Celery<\/code><\/strong> \u5219\u662f\u4e00\u4e2a\u66f4\u9002\u5408\u5927\u89c4\u6a21\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u4efb\u52a1\u961f\u5217\uff0c\u652f\u6301\u4f18\u5148\u7ea7\u961f\u5217\u3001\u4efb\u52a1\u7ed3\u679c\u5b58\u50a8\u3001\u4efb\u52a1\u8c03\u5ea6\u7b49\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u5206\u5e03\u5f0f\u4efb\u52a1\u5904\u7406\u548c\u5b9a\u65f6\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e0b\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u6784\u5efa\u9ad8\u6548\u7684\u5f02\u6b65 Web \u670d\u52a1\u548c API\uff01<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9ad8\u5e76\u53d1\u6216\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5f02\u6b65\u961f\u5217\u548c\u4efb\u52a1\u8c03\u5ea6\u7cfb\u7edf\u7528\u4e8e\u7ba1\u7406\u548c\u8c03\u5ea6\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5206\u5e03\u5f0f\u6267\u884c\u548c\u5f02\u6b65\u5904\u7406\u65f6\u3002\u4f7f\u7528\u961f\u5217 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2846,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2845","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2845"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2845\/revisions"}],"predecessor-version":[{"id":2847,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2845\/revisions\/2847"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2846"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}