{"id":978146,"date":"2024-12-27T06:35:24","date_gmt":"2024-12-26T22:35:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/978146.html"},"modified":"2024-12-27T06:35:26","modified_gmt":"2024-12-26T22:35:26","slug":"%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e8%bf%90%e8%a1%8cpython%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/978146.html","title":{"rendered":"\u5982\u4f55\u81ea\u52a8\u8fd0\u884cpython\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203252\/57420ffa-1faf-4bbb-b0a4-66d34d238e63.webp\" alt=\"\u5982\u4f55\u81ea\u52a8\u8fd0\u884cpython\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u81ea\u52a8\u8fd0\u884cPython\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\u3001\u7f16\u5199\u811a\u672c\u5e76\u7ed3\u5408\u542f\u52a8\u9879\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\uff0c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0Python\u7a0b\u5e8f\u7684\u81ea\u52a8\u5316\u8fd0\u884c\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\u662f\u81ea\u52a8\u8fd0\u884cPython\u7a0b\u5e8f\u7684\u6700\u4f73\u9009\u62e9\u4e4b\u4e00\u3002\u4ee5Windows\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u4e3a\u4f8b\uff0c\u7528\u6237\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4efb\u52a1\uff0c\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u6216\u4e8b\u4ef6\u89e6\u53d1\u65f6\u8fd0\u884cPython\u811a\u672c\u3002\u9996\u5148\uff0c\u7528\u6237\u9700\u8981\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u5e76\u9009\u62e9\u521b\u5efa\u4efb\u52a1\u3002\u5728\u201c\u64cd\u4f5c\u201d\u9009\u9879\u5361\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u6307\u5b9aPython\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u5e76\u5728\u53c2\u6570\u4e2d\u8f93\u5165Python\u811a\u672c\u7684\u8def\u5f84\u3002\u901a\u8fc7\u8bbe\u7f6e\u89e6\u53d1\u5668\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u6216\u9891\u7387\uff0c\u4f8b\u5982\u6bcf\u5929\u7684\u56fa\u5b9a\u65f6\u95f4\u6216\u6bcf\u5468\u7684\u7279\u5b9a\u65e5\u671f\u3002\u6b64\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8eWindows\uff0c\u4e5f\u53ef\u4ee5\u5728Linux\u7cfb\u7edf\u4e2d\u901a\u8fc7cron jobs\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5e26\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\u662f\u81ea\u52a8\u8fd0\u884cPython\u7a0b\u5e8f\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u6216\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u8fd0\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>1. Windows\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f<\/h3>\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\u7528\u6237\u81ea\u52a8\u5316\u5404\u79cd\u4efb\u52a1\uff0c\u5305\u62ec\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u65b0\u4efb\u52a1\uff1a<\/strong> \u6253\u5f00\u201c\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u201d\uff0c\u9009\u62e9\u201c\u521b\u5efa\u57fa\u672c\u4efb\u52a1\u201d\u6216\u201c\u521b\u5efa\u4efb\u52a1\u201d\u3002\u8f93\u5165\u4efb\u52a1\u540d\u79f0\u548c\u63cf\u8ff0\uff0c\u5e76\u9009\u62e9\u89e6\u53d1\u5668\uff0c\u4f8b\u5982\u6bcf\u5929\u3001\u6bcf\u5468\u6216\u7279\u5b9a\u65f6\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u64cd\u4f5c\uff1a<\/strong> \u5728\u64cd\u4f5c\u9009\u9879\u5361\u4e2d\uff0c\u9009\u62e9\u201c\u542f\u52a8\u7a0b\u5e8f\u201d\u3002\u5728\u7a0b\u5e8f\u6216\u811a\u672c\u680f\u4e2d\u8f93\u5165Python\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\uff08\u4f8b\u5982\uff0c<code>C:\\Python39\\python.exe<\/code>\uff09\uff0c\u5728\u201c\u6dfb\u52a0\u53c2\u6570\u201d\u4e2d\u8f93\u5165Python\u811a\u672c\u7684\u8def\u5f84\uff08\u4f8b\u5982\uff0c<code>C:\\Scripts\\myscript.py<\/code>\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u6761\u4ef6\uff1a<\/strong> \u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u9644\u52a0\u6761\u4ef6\uff0c\u4f8b\u5982\u4ec5\u5728\u8ba1\u7b97\u673a\u7a7a\u95f2\u65f6\u8fd0\u884c\uff0c\u6216\u4ec5\u5728\u7279\u5b9a\u7f51\u7edc\u8fde\u63a5\u65f6\u8fd0\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>2. Linux\u4e2d\u7684Cron Jobs<\/h3>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0ccron\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u7f16\u8f91Crontab\uff1a<\/strong> \u4f7f\u7528\u547d\u4ee4<code>crontab -e<\/code>\u6253\u5f00crontab\u7f16\u8f91\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u4efb\u52a1\uff1a<\/strong> \u5728crontab\u6587\u4ef6\u4e2d\u6dfb\u52a0\u65b0\u884c\uff0c\u6307\u5b9a\u4efb\u52a1\u7684\u65f6\u95f4\u548c\u9891\u7387\u3002\u4f8b\u5982\uff0c\u8981\u6bcf\u5929\u5348\u591c\u8fd0\u884cPython\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code>0 0 * * * \/usr\/bin\/python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>\/usr\/bin\/python3<\/code>\u662fPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\uff0c<code>\/path\/to\/script.py<\/code>\u662fPython\u811a\u672c\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58\u5e76\u9000\u51fa\uff1a<\/strong> \u4fdd\u5b58\u66f4\u6539\u540e\uff0ccron\u5c06\u81ea\u52a8\u5f00\u59cb\u6839\u636e\u8bbe\u5b9a\u7684\u65f6\u95f4\u8868\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u7f16\u5199\u811a\u672c\u5e76\u7ed3\u5408\u542f\u52a8\u9879<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u7f16\u5199\u811a\u672c\u5e76\u5c06\u5176\u52a0\u5165\u5230\u7cfb\u7edf\u542f\u52a8\u9879\u4e2d\uff0c\u53ef\u4ee5\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u81ea\u52a8\u8fd0\u884cPython\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>1. Windows\u4e2d\u7684\u542f\u52a8\u9879<\/h3>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u811a\u672c\u6216\u5176\u5feb\u6377\u65b9\u5f0f\u6dfb\u52a0\u5230\u542f\u52a8\u6587\u4ef6\u5939\u6765\u5b9e\u73b0\u81ea\u52a8\u8fd0\u884c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u6279\u5904\u7406\u6587\u4ef6\uff1a<\/strong> \u65b0\u5efa\u4e00\u4e2a\u6279\u5904\u7406\u6587\u4ef6\uff08<code>.bat<\/code>\uff09\uff0c\u5185\u5bb9\u5305\u62ecPython\u53ef\u6267\u884c\u6587\u4ef6\u548c\u811a\u672c\u8def\u5f84\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-batch\">@echo off<\/p>\n<p>C:\\Python39\\python.exe C:\\Scripts\\myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u5230\u542f\u52a8\u6587\u4ef6\u5939\uff1a<\/strong> \u5c06\u6279\u5904\u7406\u6587\u4ef6\u6216\u5176\u5feb\u6377\u65b9\u5f0f\u590d\u5236\u5230\u542f\u52a8\u6587\u4ef6\u5939\uff0c\u901a\u5e38\u4f4d\u4e8e<code>C:\\Users\\&lt;\u7528\u6237\u540d&gt;\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>2. Linux\u4e2d\u7684\u542f\u52a8\u9879<\/h3>\n<\/p>\n<p><p>\u5728Linux\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u811a\u672c\u6dfb\u52a0\u5230\u542f\u52a8\u76ee\u5f55\u6216\u4f7f\u7528\u542f\u52a8\u670d\u52a1\u6765\u5b9e\u73b0\u81ea\u52a8\u8fd0\u884c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528rc.local\u6587\u4ef6\uff1a<\/strong> \u7f16\u8f91<code>\/etc\/rc.local<\/code>\u6587\u4ef6\uff0c\u5728<code>exit 0<\/code>\u4e4b\u524d\u6dfb\u52a0Python\u811a\u672c\u7684\u6267\u884c\u547d\u4ee4\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">\/usr\/bin\/python3 \/path\/to\/script.py &amp;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u7cfb\u7edf\u670d\u52a1\uff1a<\/strong> \u521b\u5efa\u4e00\u4e2a\u7cfb\u7edf\u670d\u52a1\uff0c\u7f16\u5199\u670d\u52a1\u6587\u4ef6\u5e76\u5c06\u5176\u653e\u7f6e\u5728<code>\/etc\/systemd\/system\/<\/code>\u76ee\u5f55\u4e2d\uff0c\u7136\u540e\u4f7f\u7528<code>systemctl<\/code>\u547d\u4ee4\u542f\u7528\u670d\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177<\/h2>\n<\/p>\n<p><p>\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0Python\u7a0b\u5e8f\u7684\u81ea\u52a8\u5316\u8fd0\u884c\uff0c\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u63d0\u4f9b\u66f4\u591a\u529f\u80fd\u548c\u66f4\u53cb\u597d\u7684\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h3>1. Anaconda Scheduler<\/h3>\n<\/p>\n<p><p>Anaconda\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3aAnaconda Scheduler\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728Anaconda\u73af\u5883\u4e2d\u81ea\u52a8\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u8c03\u5ea6\u4efb\u52a1\uff1a<\/strong> \u4f7f\u7528Anaconda Navigator\u6253\u5f00Anaconda Scheduler\uff0c\u521b\u5efa\u65b0\u4efb\u52a1\u5e76\u8bbe\u7f6e\u811a\u672c\u8def\u5f84\u548c\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ba1\u7406\u4efb\u52a1\uff1a<\/strong> \u53ef\u4ee5\u901a\u8fc7Scheduler\u754c\u9762\u67e5\u770b\u3001\u7f16\u8f91\u548c\u5220\u9664\u5df2\u521b\u5efa\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>2. Jenkins<\/h3>\n<\/p>\n<p><p>Jenkins\u662f\u4e00\u6b3e\u6d41\u884c\u7684\u6301\u7eed\u96c6\u6210\u5de5\u5177\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5b9a\u65f6\u6216\u6761\u4ef6\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5\u5e76\u914d\u7f6eJenkins\uff1a<\/strong> \u4e0b\u8f7d\u5e76\u5b89\u88c5Jenkins\uff0c\u914d\u7f6ePython\u63d2\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaJenkins\u4efb\u52a1\uff1a<\/strong> \u5728Jenkins\u4e2d\u521b\u5efa\u65b0\u4efb\u52a1\uff0c\u9009\u62e9\u6784\u5efa\u89e6\u53d1\u5668\uff0c\u4f8b\u5982\u5b9a\u65f6\u6784\u5efa\u3002\u914d\u7f6e\u6784\u5efa\u6b65\u9aa4\uff0c\u8bbe\u7f6ePython\u811a\u672c\u7684\u8def\u5f84\u548c\u53c2\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u4f7f\u7528Python\u5185\u7f6e\u6a21\u5757<\/h2>\n<\/p>\n<p><p>Python\u5185\u7f6e\u4e86\u4e00\u4e9b\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7a0b\u5e8f\u7684\u81ea\u52a8\u8fd0\u884c\u548c\u5b9a\u65f6\u4efb\u52a1\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528Schedule\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Schedule\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5Schedule\uff1a<\/strong> \u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5Schedule\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install schedule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u5b9a\u65f6\u4efb\u52a1\uff1a<\/strong> \u4f7f\u7528Schedule\u5e93\u7f16\u5199\u5b9a\u65f6\u4efb\u52a1\uff0c\u4f8b\u5982\u6bcf\u96941\u5c0f\u65f6\u8fd0\u884c\u4e00\u6b21\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import schedule<\/p>\n<p>import time<\/p>\n<p>def job():<\/p>\n<p>    print(&quot;Running scheduled task...&quot;)<\/p>\n<p>schedule.every(1).hour.do(job)<\/p>\n<p>while True:<\/p>\n<p>    schedule.run_pending()<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>2. \u4f7f\u7528APScheduler\u6a21\u5757<\/h3>\n<\/p>\n<p><p>APScheduler\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u8c03\u5ea6\u5e93\uff0c\u652f\u6301\u590d\u6742\u7684\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5APScheduler\uff1a<\/strong> \u4f7f\u7528pip\u547d\u4ee4\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\u5b9a\u65f6\u4efb\u52a1\uff1a<\/strong> \u4f7f\u7528APScheduler\u7f16\u5199\u5b9a\u65f6\u4efb\u52a1\uff0c\u53ef\u4ee5\u9009\u62e9\u591a\u79cd\u8c03\u5ea6\u65b9\u5f0f\uff0c\u4f8b\u5982\u95f4\u9694\u8c03\u5ea6\u3001\u5b9a\u65f6\u8c03\u5ea6\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from apscheduler.schedulers.blocking import BlockingScheduler<\/p>\n<p>def job():<\/p>\n<p>    print(&quot;Running scheduled task...&quot;)<\/p>\n<p>scheduler = BlockingScheduler()<\/p>\n<p>scheduler.add_job(job, &#39;interval&#39;, hours=1)<\/p>\n<p>scheduler.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u8eab\u9700\u6c42\u548c\u73af\u5883\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0Python\u7a0b\u5e8f\u7684\u81ea\u52a8\u8fd0\u884c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\u548c\u591a\u6837\u6027\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u8bbe\u7f6ePython\u7a0b\u5e8f\u5728\u8ba1\u7b97\u673a\u542f\u52a8\u65f6\u81ea\u52a8\u8fd0\u884c\uff1f<\/strong><br \/>\u8981\u4f7fPython\u7a0b\u5e8f\u5728\u8ba1\u7b97\u673a\u542f\u52a8\u65f6\u81ea\u52a8\u8fd0\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u6279\u5904\u7406\u6587\u4ef6\u6216\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u5668\u6765\u5b9e\u73b0\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u5c06Python\u811a\u672c\u7684\u8def\u5f84\u6dfb\u52a0\u5230\u201c\u542f\u52a8\u201d\u6587\u4ef6\u5939\u4e2d\uff0c\u6216\u8005\u5728\u4efb\u52a1\u8c03\u5ea6\u5668\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4efb\u52a1\uff0c\u8bbe\u7f6e\u89e6\u53d1\u6761\u4ef6\u4e3a\u201c\u5f00\u673a\u65f6\u201d\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u5c06\u811a\u672c\u6dfb\u52a0\u5230rc.local\u6587\u4ef6\u6216\u4f7f\u7528cron\u4f5c\u4e1a\u5b89\u6392\u5728\u542f\u52a8\u65f6\u8fd0\u884c\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u4ec0\u4e48\u5de5\u5177\u6765\u7ba1\u7406\u81ea\u52a8\u8fd0\u884c\u7684Python\u811a\u672c\uff1f<\/strong><br \/>\u6709\u5f88\u591a\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u7ba1\u7406\u81ea\u52a8\u8fd0\u884c\u7684Python\u811a\u672c\u3002\u6bd4\u5982\uff0cWindows\u7684\u4efb\u52a1\u8c03\u5ea6\u5668\u63d0\u4f9b\u4e86\u56fe\u5f62\u754c\u9762\u6765\u8bbe\u7f6e\u4efb\u52a1\u7684\u89e6\u53d1\u6761\u4ef6\u548c\u8fd0\u884c\u53c2\u6570\u3002\u5728Linux\u4e0a\uff0ccron\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5b89\u6392\u811a\u672c\u5728\u7279\u5b9a\u65f6\u95f4\u6216\u4e8b\u4ef6\u53d1\u751f\u65f6\u81ea\u52a8\u8fd0\u884c\u3002\u6b64\u5916\uff0c\u6709\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u5982NSSM\uff08Non-Sucking Service Manager\uff09\u53ef\u4ee5\u5c06Python\u811a\u672c\u4f5c\u4e3a\u670d\u52a1\u8fd0\u884c\uff0c\u65b9\u4fbf\u7ba1\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6211\u7684Python\u7a0b\u5e8f\u5728\u81ea\u52a8\u8fd0\u884c\u65f6\u4e0d\u4f1a\u51fa\u9519\uff1f<\/strong><br \/>\u4e3a\u786e\u4fddPython\u7a0b\u5e8f\u5728\u81ea\u52a8\u8fd0\u884c\u65f6\u4e0d\u4f1a\u51fa\u9519\uff0c\u5efa\u8bae\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u4f8b\u5982\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u52a0\u5165\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0c\u4ee5\u4fbf\u5728\u53d1\u751f\u9519\u8bef\u65f6\u53ef\u4ee5\u67e5\u770b\u65e5\u5fd7\u6587\u4ef6\u6765\u8bca\u65ad\u95ee\u9898\u3002\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u7684\u5e93\u548c\u73af\u5883\u914d\u7f6e\u90fd\u5df2\u6b63\u786e\u8bbe\u7f6e\uff0c\u5e76\u8003\u8651\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u4ee5\u907f\u514d\u7248\u672c\u51b2\u7a81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u81ea\u52a8\u8fd0\u884cPython\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\u3001\u7f16\u5199\u811a\u672c\u5e76\u7ed3\u5408\u542f\u52a8\u9879\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\uff0c\u4f7f\u7528\u8fd9\u4e9b\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":978156,"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\/978146"}],"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=978146"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978146\/revisions"}],"predecessor-version":[{"id":978160,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978146\/revisions\/978160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/978156"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=978146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=978146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=978146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}