{"id":1044216,"date":"2024-12-31T13:12:41","date_gmt":"2024-12-31T05:12:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1044216.html"},"modified":"2024-12-31T13:12:44","modified_gmt":"2024-12-31T05:12:44","slug":"%e5%a6%82%e4%bd%95%e4%bd%bfpython%e7%a8%8b%e5%ba%8f%e4%b8%80%e7%9b%b4%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1044216.html","title":{"rendered":"\u5982\u4f55\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2727cfb6-7b7b-4fd1-8722-05ddc9b632a2.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>\u8981\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u3001\u8c03\u5ea6\u7a0b\u5e8f\u3001\u5b88\u62a4\u8fdb\u7a0b\u7b49\u65b9\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6765\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u65e0\u9650\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u4f7fPython\u7a0b\u5e8f\u6301\u7eed\u8fd0\u884c\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u624d\u9000\u51fa\u3002\u6700\u7b80\u5355\u7684\u5f62\u5f0f\u662f<code>while True:<\/code>\uff0c\u8fd9\u610f\u5473\u7740\u7a0b\u5e8f\u5c06\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u624b\u52a8\u505c\u6b62\u6216\u9047\u5230break\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    # \u4f60\u7684\u4ee3\u7801\u903b\u8f91<\/p>\n<p>    print(&quot;\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8c03\u5ea6\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u8c03\u5ea6\u7a0b\u5e8f\uff08\u5982<code>cron<\/code>\u5728Linux\u4e0a\u6216<code>Task Scheduler<\/code>\u5728Windows\u4e0a\uff09\u6765\u5b9a\u671f\u8fd0\u884cPython\u811a\u672c\u3002\u8fd9\u6837\u5373\u4f7f\u811a\u672c\u8fd0\u884c\u4e00\u6b21\u540e\u7ed3\u675f\uff0c\u4e5f\u4f1a\u5728\u4e0b\u4e00\u4e2a\u8c03\u5ea6\u65f6\u95f4\u70b9\u518d\u6b21\u542f\u52a8\u3002<\/p>\n<\/p>\n<p><h4>1. Linux\u4e0a\u7684<code>cron<\/code><\/h4>\n<\/p>\n<p><p>\u5728Linux\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>cron<\/code>\u6765\u8c03\u5ea6Python\u811a\u672c\u7684\u6267\u884c\u3002\u9996\u5148\uff0c\u6253\u5f00<code>crontab<\/code>\u7f16\u8f91\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">crontab -e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u6dfb\u52a0\u4e00\u884c\u6765\u8c03\u5ea6\u4f60\u7684\u811a\u672c\uff0c\u4f8b\u5982\u6bcf\u5206\u949f\u8fd0\u884c\u4e00\u6b21\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">* * * * * \/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Windows\u4e0a\u7684\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u6765\u8c03\u5ea6Python\u811a\u672c\u7684\u6267\u884c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u4efb\u52a1\u3002<\/li>\n<li>\u9009\u62e9\u89e6\u53d1\u5668\uff08\u5982\u6bcf\u5929\u3001\u6bcf\u5c0f\u65f6\u7b49\uff09\u3002<\/li>\n<li>\u5728\u64cd\u4f5c\u9009\u9879\u4e2d\u9009\u62e9\u201c\u542f\u52a8\u7a0b\u5e8f\u201d\u5e76\u6d4f\u89c8\u5230Python\u89e3\u91ca\u5668\u53ca\u811a\u672c\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5b88\u62a4\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5b88\u62a4\u8fdb\u7a0b\u662f\u4e00\u79cd\u5728\u540e\u53f0\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u901a\u5e38\u5728Unix\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff0c\u4f46\u5728Windows\u4e0a\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u548c<code>threading<\/code>\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Process<\/p>\n<p>import time<\/p>\n<p>def your_task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    p = Process(target=your_task)<\/p>\n<p>    p.daemon = True<\/p>\n<p>    p.start()<\/p>\n<p>    p.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>threading<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def your_task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    t = threading.Thread(target=your_task)<\/p>\n<p>    t.daemon = True<\/p>\n<p>    t.start()<\/p>\n<p>    t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0Python\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002\u4f8b\u5982\uff0c<code>schedule<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u8c03\u5ea6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5<code>schedule<\/code>\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install schedule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>schedule<\/code>\u5e93<\/h4>\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;\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>schedule.every(1).minute.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<p><h3>\u4e94\u3001\u4f7f\u7528\u5f02\u6b65IO<\/h3>\n<\/p>\n<p><p>Python\u7684\u5f02\u6b65IO\uff08\u5982<code>asyncio<\/code>\uff09\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u65f6\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u8fd9\u5bf9\u4e8e\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u4efb\u52a1\u7684\u7a0b\u5e8f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>asyncio<\/code><\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def your_task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>loop = asyncio.get_event_loop()<\/p>\n<p>loop.run_until_complete(your_task())<\/p>\n<p>loop.run_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u901a\u8fc7\u6355\u6349\u7cfb\u7edf\u4fe1\u53f7\u6765\u4fdd\u6301\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002Python\u7684<code>signal<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>signal<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import time<\/p>\n<p>def handle_signal(signum, frame):<\/p>\n<p>    print(&quot;\u6355\u6349\u5230\u4fe1\u53f7&quot;, signum)<\/p>\n<p>signal.signal(signal.SIGINT, handle_signal)<\/p>\n<p>signal.signal(signal.SIGTERM, handle_signal)<\/p>\n<p>while True:<\/p>\n<p>    print(&quot;\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec\u65e0\u9650\u5faa\u73af\u3001\u8c03\u5ea6\u7a0b\u5e8f\u3001\u5b88\u62a4\u8fdb\u7a0b\u3001\u7b2c\u4e09\u65b9\u5e93\u3001\u5f02\u6b65IO\u548c\u4fe1\u53f7\u5904\u7406\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u4f60\u7684Python\u7a0b\u5e8f\u6301\u7eed\u8fd0\u884c\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u8981\u6ce8\u610f\u5904\u7406\u597d\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u548c\u9519\u8bef\uff0c\u4ee5\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8be6\u7ec6\u4e86\u89e3\u548c\u5b9e\u8df5\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u65b9\u5f0f\u6765\u4fdd\u6301Python\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u786e\u4fdd\u6211\u7684Python\u7a0b\u5e8f\u5728\u5d29\u6e83\u540e\u81ea\u52a8\u91cd\u542f\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fddPython\u7a0b\u5e8f\u5728\u5d29\u6e83\u540e\u80fd\u591f\u81ea\u52a8\u91cd\u542f\uff0c\u53ef\u4ee5\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u529f\u80fd\u6216\u8005\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u76d1\u63a7\u811a\u672c\u3002\u5bf9\u4e8eLinux\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528<code>systemd<\/code>\u670d\u52a1\u6765\u7ba1\u7406\u7a0b\u5e8f\u7684\u8fd0\u884c\uff0c\u800cWindows\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u6355\u83b7\u5f02\u5e38\uff0c\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u6765\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u3002\u5982\u679c\u4f7f\u7528Docker\u5bb9\u5668\uff0c\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u91cd\u542f\u7b56\u7565\u6765\u786e\u4fdd\u5bb9\u5668\u5728\u5931\u8d25\u540e\u91cd\u542f\u3002<\/p>\n<p><strong>\u5728\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u5185\u5b58\u4f7f\u7528\u4ee5\u907f\u514d\u5d29\u6e83\uff1f<\/strong><br \/>\u5185\u5b58\u7ba1\u7406\u662f\u786e\u4fddPython\u7a0b\u5e8f\u6301\u7eed\u8fd0\u884c\u7684\u91cd\u8981\u65b9\u9762\u3002\u53ef\u4ee5\u901a\u8fc7\u76d1\u63a7\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u4f7f\u7528<code>psutil<\/code>\u5e93\u6765\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u5185\u5b58\u4fe1\u606f\uff0c\u5e76\u5728\u8fbe\u5230\u4e00\u5b9a\u9608\u503c\u65f6\u91c7\u53d6\u63aa\u65bd\uff0c\u4f8b\u5982\u91ca\u653e\u4e0d\u5fc5\u8981\u7684\u8d44\u6e90\u6216\u91cd\u542f\u7a0b\u5e8f\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u8fdb\u884c\u5185\u5b58\u6e05\u7406\uff0c\u4f7f\u7528\u751f\u6210\u5668\u4ee3\u66ff\u5217\u8868\u7b49\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u5185\u5b58\u5360\u7528\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u76d1\u63a7Python\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff1f<\/strong><br \/>\u6709\u591a\u79cd\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u76d1\u63a7Python\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3002\u53ef\u4ee5\u4f7f\u7528<code>supervisord<\/code>\u3001<code>pm2<\/code>\u7b49\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\uff0c\u8fd9\u4e9b\u5de5\u5177\u80fd\u591f\u76d1\u63a7\u4f60\u7684\u7a0b\u5e8f\uff0c\u5e76\u5728\u5b83\u4eec\u5d29\u6e83\u65f6\u81ea\u52a8\u91cd\u542f\u3002\u6b64\u5916\uff0c<code>prometheus<\/code>\u548c<code>Grafana<\/code>\u53ef\u4ee5\u7528\u4e8e\u66f4\u9ad8\u7ea7\u7684\u76d1\u63a7\uff0c\u63d0\u4f9b\u5b9e\u65f6\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\uff0c\u5e2e\u52a9\u4f60\u53ca\u65f6\u53d1\u73b0\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u3001\u8c03\u5ea6\u7a0b\u5e8f\u3001\u5b88\u62a4\u8fdb\u7a0b\u7b49\u65b9\u5f0f\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528w [&hellip;]","protected":false},"author":3,"featured_media":1044231,"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\/1044216"}],"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=1044216"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044216\/revisions"}],"predecessor-version":[{"id":1044235,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044216\/revisions\/1044235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1044231"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1044216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1044216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1044216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}