{"id":702,"date":"2021-03-04T11:11:56","date_gmt":"2021-03-04T03:11:56","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=702"},"modified":"2022-05-17T09:29:34","modified_gmt":"2022-05-17T01:29:34","slug":"sanic","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=702","title":{"rendered":"Sanic\u4e2d\u6dfb\u52a0\u57fa\u4e8eCron\u8868\u8fbe\u5f0f\u7684\u534f\u7a0b\u5b9a\u65f6\u4efb\u52a1"},"content":{"rendered":"<h1>\u9700\u6c42<\/h1>\n<p>\u4ece\u9875\u9762\u4e0a\u89e6\u53d1\u67d0\u4e00\u4e2a\u63a5\u53e3\u4e4b\u540e\uff0c\u8981\u5728sanic\u91cc\u9762\u6dfb\u52a0\u4e00\u4e2a\u8ba1\u5212\u4efb\u52a1\u3002<br \/>\u4f8b\u5982\uff1a\u4ece\u9875\u9762\u4e0a\u586b\u5199\u4e00\u4e2acon\u8868\u8fbe\u5f0f\uff0c\u540e\u53f0\u6839\u636e\u6b64cron\u8868\u8fbe\u5f0f\u5b9a\u65f6\u6267\u884c\u4efb\u52a1<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-707 size-full\" style=\"font-size: 1rem;\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-8.png\" alt=\"\" width=\"349\" height=\"180\" \/><\/p>\n<pre class=\"\">'*\/1 * * * *'   # \u8fd9\u4e2a\u662f\u6bcf1\u5206\u949f\u6267\u884c\u4e00\u6b21\u7684\u8868\u8fbe\u5f0f<\/pre>\n<p>\u9700\u8981\u7528\u5230\u7684python\u5305\u6709:Sanic\uff0cApscheduler<\/p>\n<hr \/>\n<h1>\u793a\u4f8b\u4ee3\u7801<\/h1>\n<pre class=\"lang:python decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">import time\nfrom sanic import Sanic\nfrom sanic.response import json\napp = Sanic(\"App Name\")\nfrom apscheduler.schedulers.asyncio import AsyncIOScheduler\nfrom apscheduler.triggers.cron import CronTrigger\ndef cron_job():\n    print('\u6253\u5370\u5f53\u524d\u65f6\u95f4:', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))\n@app.route('\/')\nasync def index(request):\n    # generate a URL for the endpoint `post_handler`\n    print(\"\u6d4f\u89c8\u5668\u91cc\u6253\u5f00\u4e00\u4e0b\u9996\u9875\")\n    scheduler = AsyncIOScheduler()\n    scheduler.add_job(cron_job, CronTrigger.from_crontab('*\/1 * * * *'))\n    scheduler.start()\n    return json({\"hello\": \"thank you\"})\nif __name__ == \"__main__\":\n    app.run(host=\"0.0.0.0\", port=8000)\n<\/code><\/pre>\n<hr \/>\n<h1>\u5b9e\u9a8c\u6548\u679c<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-704 size-large\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-7-1-1024x220.png\" alt=\"\" width=\"900\" height=\"193\" \/><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 \u4ece\u9875\u9762\u4e0a\u89e6\u53d1\u67d0\u4e00\u4e2a\u63a5\u53e3\u4e4b\u540e\uff0c\u8981\u5728sanic\u91cc\u9762\u6dfb\u52a0\u4e00\u4e2a\u8ba1\u5212\u4efb\u52a1\u3002\u4f8b\u5982\uff1a\u4ece\u9875\u9762\u4e0a\u586b\u5199\u4e00\u4e2acon\u8868\u8fbe\u5f0f\uff0c\u540e\u53f0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-702","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=702"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/702\/revisions"}],"predecessor-version":[{"id":855,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/702\/revisions\/855"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}