{"id":1004972,"date":"2024-12-27T10:31:29","date_gmt":"2024-12-27T02:31:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004972.html"},"modified":"2024-12-27T10:31:31","modified_gmt":"2024-12-27T02:31:31","slug":"%e5%a6%82%e4%bd%95%e5%ae%9a%e6%97%b6%e6%89%a7%e8%a1%8cpython%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004972.html","title":{"rendered":"\u5982\u4f55\u5b9a\u65f6\u6267\u884cpython\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081710\/8cb0cbd9-d46e-4a31-b92b-e59a96ceffd8.webp\" alt=\"\u5982\u4f55\u5b9a\u65f6\u6267\u884cpython\u811a\u672c\" \/><\/p>\n<p><p> <strong>\u8981\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5e26\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u5982Windows\u7684\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u3001Linux\u7684Cron\u4f5c\u4e1a\u3001Python\u7684\u5b9a\u65f6\u4efb\u52a1\u5e93APScheduler\u7b49\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528Windows\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u548cLinux\u7684Cron\u4f5c\u4e1a\u8fdb\u884c\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001WINDOWS\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f<\/p>\n<\/p>\n<p><p>Windows\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u7279\u5b9a\u65f6\u95f4\u6216\u5468\u671f\u6267\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u57fa\u672c\u4efb\u52a1<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\uff0c\u9009\u62e9\u201c\u521b\u5efa\u57fa\u672c\u4efb\u52a1\u201d\u3002\u5728\u5411\u5bfc\u4e2d\uff0c\u8f93\u5165\u4efb\u52a1\u540d\u79f0\u548c\u63cf\u8ff0\uff0c\u7136\u540e\u9009\u62e9\u89e6\u53d1\u5668\uff08\u4f8b\u5982\u6bcf\u5929\u3001\u6bcf\u5468\u3001\u6bcf\u6708\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u89e6\u53d1\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u89e6\u53d1\u5668\u65f6\u95f4\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u5b9a\u6bcf\u5929\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u6216\u8005\u6bcf\u5468\u7684\u67d0\u5929\u6267\u884c\u811a\u672c\u3002\u89e6\u53d1\u5668\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u6216\u7528\u6237\u767b\u5f55\u65f6\u89e6\u53d1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u201c\u64cd\u4f5c\u201d\u9009\u9879\u5361\u4e2d\uff0c\u9009\u62e9\u201c\u542f\u52a8\u7a0b\u5e8f\u201d\u3002\u6d4f\u89c8\u5230Python\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\uff08\u901a\u5e38\u4e3a<code>python.exe<\/code>\uff09\uff0c\u5e76\u5728\u201c\u6dfb\u52a0\u53c2\u6570\u201d\u5b57\u6bb5\u4e2d\u8f93\u5165Python\u811a\u672c\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b8c\u6210\u4efb\u52a1<\/strong><\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u8bbe\u7f6e\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u201c\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u5e93\u201d\u4e2d\u67e5\u770b\u548c\u7ba1\u7406\u4efb\u52a1\u3002\u786e\u4fdd\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u670d\u52a1\u6b63\u5728\u8fd0\u884c\uff0c\u5e76\u6d4b\u8bd5\u4efb\u52a1\u4ee5\u786e\u4fdd\u5176\u6309\u9884\u671f\u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001LINUX CRON\u4f5c\u4e1a<\/p>\n<\/p>\n<p><p>Cron\u662fLinux\u7cfb\u7edf\u4e0a\u7528\u4e8e\u8c03\u5ea6\u5468\u671f\u6027\u4efb\u52a1\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00Crontab<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u547d\u4ee4<code>crontab -e<\/code>\u6253\u5f00\u5f53\u524d\u7528\u6237\u7684Crontab\u6587\u4ef6\u3002\u5982\u679c\u8fd9\u662f\u60a8\u9996\u6b21\u4f7f\u7528Crontab\uff0c\u7cfb\u7edf\u53ef\u80fd\u4f1a\u63d0\u793a\u60a8\u9009\u62e9\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6eCron\u4f5c\u4e1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728Crontab\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u884c\u6765\u8bbe\u5b9a\u811a\u672c\u7684\u6267\u884c\u65f6\u95f4\u548c\u9891\u7387\u3002Cron\u8bed\u6cd5\u901a\u5e38\u4e3a<code>* * * * *<\/code>\uff0c\u5206\u522b\u5bf9\u5e94\u5206\u949f\u3001\u5c0f\u65f6\u3001\u65e5\u671f\u3001\u6708\u4efd\u548c\u661f\u671f\u51e0\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u82e5\u8981\u6bcf\u5929\u51cc\u66683\u70b9\u6267\u884c\u811a\u672c\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code>0 3 * * * \/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58\u5e76\u9000\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u7f16\u8f91\u5b8c\u6210\u540e\uff0c\u4fdd\u5b58\u5e76\u9000\u51faCrontab\u6587\u4ef6\u3002Cron\u670d\u52a1\u4f1a\u81ea\u52a8\u8bfb\u53d6\u548c\u6267\u884c\u65b0\u6dfb\u52a0\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528APScheduler\u5e93<\/p>\n<\/p>\n<p><p>APScheduler\u662f\u4e00\u4e2aPython\u5e93\uff0c\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u5185\u90e8\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5APScheduler<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5APScheduler\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install apscheduler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u8c03\u5ea6\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\u6765\u914d\u7f6e\u548c\u542f\u52a8\u8c03\u5ea6\u5668\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528APScheduler\u7684\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from apscheduler.schedulers.blocking import BlockingScheduler<\/p>\n<p>def my_job():<\/p>\n<p>    print(&quot;Job executed!&quot;)<\/p>\n<p>scheduler = BlockingScheduler()<\/p>\n<p>scheduler.add_job(my_job, &#39;interval&#39;, hours=1)  # \u6bcf\u5c0f\u65f6\u6267\u884c\u4e00\u6b21<\/p>\n<p>scheduler.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u8c03\u5ea6\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u8fd0\u884c\u8be5\u811a\u672c\uff0cAPScheduler\u4f1a\u6309\u7167\u8bbe\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u4f7f\u7528\u73af\u5883\u548c\u9700\u6c42\u3002<strong>Windows\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f<\/strong>\u9002\u5408\u5728Windows\u73af\u5883\u4e0b\u7684\u7528\u6237\u754c\u9762\u64cd\u4f5c\uff0c<strong>Cron\u4f5c\u4e1a<\/strong>\u9002\u5408Linux\u670d\u52a1\u5668\u7684\u540e\u53f0\u4efb\u52a1\u8c03\u5ea6\uff0c<strong>APScheduler<\/strong>\u9002\u5408\u9700\u8981\u5728Python\u5e94\u7528\u4e2d\u96c6\u6210\u5b9a\u65f6\u4efb\u52a1\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>Windows\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f<\/strong>\uff1a\u9002\u5408\u4e60\u60ef\u4e8e\u56fe\u5f62\u754c\u9762\u7684\u7528\u6237\uff0c\u6613\u4e8e\u914d\u7f6e\u3002<\/li>\n<li><strong>Cron\u4f5c\u4e1a<\/strong>\uff1a\u7075\u6d3b\u5f3a\u5927\uff0c\u9002\u5408\u670d\u52a1\u5668\u73af\u5883\uff0c\u652f\u6301\u590d\u6742\u7684\u8c03\u5ea6\u89c4\u5219\u3002<\/li>\n<li><strong>APScheduler<\/strong>\uff1a\u9002\u5408\u5728Python\u9879\u76ee\u4e2d\u96c6\u6210\u5b9a\u65f6\u4efb\u52a1\uff0c\u4e0d\u4f9d\u8d56\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<\/ul>\n<p><p>\u65e0\u8bba\u60a8\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u786e\u4fddPython\u73af\u5883\u914d\u7f6e\u6b63\u786e\uff0c\u811a\u672c\u8def\u5f84\u65e0\u8bef\uff0c\u5e76\u5b9a\u671f\u68c0\u67e5\u4efb\u52a1\u6267\u884c\u60c5\u51b5\u4ee5\u786e\u4fdd\u5176\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Windows\u7cfb\u7edf\u4e0a\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u6765\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\u3002\u9996\u5148\uff0c\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\uff0c\u9009\u62e9\u201c\u521b\u5efa\u57fa\u672c\u4efb\u52a1\u201d\uff0c\u7136\u540e\u8bbe\u7f6e\u4efb\u52a1\u540d\u79f0\u548c\u89e6\u53d1\u5668\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u201c\u64cd\u4f5c\u201d\u4e2d\u9009\u62e9\u201c\u542f\u52a8\u7a0b\u5e8f\u201d\uff0c\u5e76\u6d4f\u89c8\u5230Python\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08\u5982python.exe\uff09\uff0c\u5728\u201c\u53c2\u6570\u201d\u4e2d\u8f93\u5165\u811a\u672c\u7684\u5b8c\u6574\u8def\u5f84\u3002\u5b8c\u6210\u8bbe\u7f6e\u540e\uff0c\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u5c06\u6839\u636e\u8bbe\u5b9a\u7684\u65f6\u95f4\u81ea\u52a8\u8fd0\u884c\u60a8\u7684\u811a\u672c\u3002<\/p>\n<p><strong>Linux\u7cfb\u7edf\u4e2d\u5982\u4f55\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Cron\u4f5c\u4e1a\u6765\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\u3002\u901a\u8fc7\u547d\u4ee4<code>crontab -e<\/code>\u6253\u5f00Cron\u914d\u7f6e\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u6761\u65b0\u8bb0\u5f55\u6765\u5b9a\u4e49\u6267\u884c\u65f6\u95f4\u548c\u8981\u8fd0\u884c\u7684Python\u811a\u672c\u3002\u4f8b\u5982\uff0c<code>0 * * * * \/usr\/bin\/python3 \/path\/to\/script.py<\/code>\u5c06\u4f1a\u6bcf\u5c0f\u65f6\u6267\u884c\u4e00\u6b21\u8be5\u811a\u672c\u3002\u786e\u4fdd\u811a\u672c\u5177\u6709\u6267\u884c\u6743\u9650\uff0c\u5e76\u4e14\u8def\u5f84\u6307\u5411\u6b63\u786e\u7684Python\u89e3\u91ca\u5668\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u6765\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u9664\u4e86\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5e26\u7684\u5b9a\u65f6\u4efb\u52a1\u529f\u80fd\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\uff0c\u4f8b\u5982<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rflow\u3001Celery\u6216apscheduler\u3002\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u66f4\u52a0\u7075\u6d3b\u7684\u4efb\u52a1\u8c03\u5ea6\u529f\u80fd\uff0c\u9002\u5408\u590d\u6742\u7684\u4efb\u52a1\u7ba1\u7406\u548c\u6267\u884c\u9700\u6c42\u3002\u5b83\u4eec\u652f\u6301\u5b9a\u65f6\u3001\u5468\u671f\u6027\u4efb\u52a1\u4ee5\u53ca\u4efb\u52a1\u4f9d\u8d56\u5173\u7cfb\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u9ad8\u6548\u5730\u7ba1\u7406Python\u811a\u672c\u7684\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5e26\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u5982Windows\u7684\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u3001Linux\u7684C [&hellip;]","protected":false},"author":3,"featured_media":1004983,"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\/1004972"}],"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=1004972"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004972\/revisions"}],"predecessor-version":[{"id":1004985,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004972\/revisions\/1004985"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004983"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}