{"id":998364,"date":"2024-12-27T09:32:42","date_gmt":"2024-12-27T01:32:42","guid":{"rendered":""},"modified":"2024-12-27T09:32:44","modified_gmt":"2024-12-27T01:32:44","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%b8%b8%e9%a9%bb%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998364.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u5e38\u9a7b\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073838\/24258c05-262d-4f70-8798-58f77c597372.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u5e38\u9a7b\u8fd0\u884c\" \/><\/p>\n<p><p> Python\u5b9e\u73b0\u5e38\u9a7b\u8fd0\u884c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b88\u62a4\u8fdb\u7a0b\u3001\u4f7f\u7528\u8ba1\u5212\u4efb\u52a1\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784<\/strong>\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u5b88\u62a4\u8fdb\u7a0b\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b88\u62a4\u8fdb\u7a0b\u80fd\u591f\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\uff0c\u800c\u4e0d\u9700\u8981\u7528\u6237\u5e72\u9884\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u5b88\u62a4\u8fdb\u7a0b\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5b88\u62a4\u8fdb\u7a0b\uff08Daemon\uff09\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u4e0d\u4e0e\u7528\u6237\u76f4\u63a5\u4ea4\u4e92\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u6bd4\u5982\u670d\u52a1\u5668\u8fdb\u7a0b\u3001\u540e\u53f0\u670d\u52a1\u7b49\u3002Python\u4e2d\u53ef\u4ee5\u901a\u8fc7<code>threading<\/code>\u6a21\u5757\u6216<code>multiprocessing<\/code>\u6a21\u5757\u6765\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>daemonize<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 THREADING \u6a21\u5757\u5b9e\u73b0\u5b88\u62a4\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p><code>threading<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u7684\u529f\u80fd\u3002\u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u8c03\u5ea6\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u7ebf\u7a0b\u672c\u8eab\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u5229\u7528\u591a\u6838CPU\u7684\u4f18\u52bf\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u521b\u5efa\u5b88\u62a4\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u7684<code>daemon<\/code>\u5c5e\u6027\u4e3a<code>True<\/code>\u6765\u521b\u5efa\u5b88\u62a4\u7ebf\u7a0b\u3002\u5b88\u62a4\u7ebf\u7a0b\u5c06\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u81ea\u52a8\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def background_task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Running background task&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>t = threading.Thread(target=background_task)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b<\/strong><\/h2>\n<p>t.setDaemon(True)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>t.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b<\/strong><\/h2>\n<p>print(&quot;M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n thread is running&quot;)<\/p>\n<p>time.sleep(5)<\/p>\n<p>print(&quot;Main thread is exiting&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>background_task<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u7684\u4efb\u52a1\uff0c\u4f5c\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u8fd0\u884c\u3002\u5f53\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u7ebf\u7a0b\u7684\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u7ebf\u7a0b\u9002\u5408\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u6bd4\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u64cd\u4f5c\u7b49\uff0c\u56e0\u4e3aPython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u9650\u5236\u4e86CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u591a\u7ebf\u7a0b\u5e76\u53d1\u6027\u80fd\u3002\u5982\u679c\u4efb\u52a1\u662fCPU\u5bc6\u96c6\u578b\u7684\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 MULTIPROCESSING \u6a21\u5757\u5b9e\u73b0\u5b88\u62a4\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u7528\u4e8e\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u6709\u81ea\u5df1\u7684Python\u89e3\u91ca\u5668\u548c\u5185\u5b58\u7a7a\u95f4\u3002\u591a\u8fdb\u7a0b\u53ef\u4ee5\u6709\u6548\u5229\u7528\u591a\u6838CPU\u7684\u8ba1\u7b97\u80fd\u529b\uff0c\u975e\u5e38\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>multiprocessing.Process<\/code>\u521b\u5efa\u8fdb\u7a0b\uff0c\u5e76\u5c06<code>daemon<\/code>\u5c5e\u6027\u8bbe\u4e3a<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def background_process():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Running background process&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b<\/strong><\/h2>\n<p>p = multiprocessing.Process(target=background_process)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u8fdb\u7a0b<\/strong><\/h2>\n<p>p.daemon = True<\/p>\n<h2><strong>\u542f\u52a8\u8fdb\u7a0b<\/strong><\/h2>\n<p>p.start()<\/p>\n<h2><strong>\u4e3b\u8fdb\u7a0b<\/strong><\/h2>\n<p>print(&quot;Main process is running&quot;)<\/p>\n<p>time.sleep(5)<\/p>\n<p>print(&quot;Main process is exiting&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e\u7ebf\u7a0b\u7c7b\u4f3c\uff0c\u5b88\u62a4\u8fdb\u7a0b\u4f1a\u5728\u4e3b\u8fdb\u7a0b\u7ed3\u675f\u65f6\u81ea\u52a8\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u8fdb\u7a0b\u7684\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u9002\u5408\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u6bd4\u5982\u590d\u6742\u8ba1\u7b97\u3001\u6570\u636e\u5904\u7406\u7b49\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684Python\u89e3\u91ca\u5668\uff0c\u4e0d\u53d7GIL\u9650\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 DAEMONIZE \u5e93\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p><code>daemonize<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7b80\u4fbf\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\u3002\u901a\u8fc7\u4f7f\u7528\u6b64\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u5b88\u62a4\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5 DAEMONIZE \u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5<code>daemonize<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install python-daemonize<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528 DAEMONIZE \u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p><code>daemonize<\/code>\u5e93\u901a\u8fc7\u5c01\u88c5\u5e95\u5c42\u7cfb\u7edf\u8c03\u7528\uff0c\u63d0\u4f9b\u4e86\u6613\u4e8e\u4f7f\u7528\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from daemonize import Daemonize<\/p>\n<p>import time<\/p>\n<p>def main():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Daemon is running&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u5b88\u62a4\u8fdb\u7a0b\u7684PID\u6587\u4ef6<\/strong><\/h2>\n<p>pid = &quot;\/tmp\/test_daemon.pid&quot;<\/p>\n<h2><strong>\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b<\/strong><\/h2>\n<p>daemon = Daemonize(app=&quot;test_app&quot;, pid=pid, action=main)<\/p>\n<h2><strong>\u542f\u52a8\u5b88\u62a4\u8fdb\u7a0b<\/strong><\/h2>\n<p>daemon.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>Daemonize<\/code>\u7c7b\u53ef\u4ee5\u5feb\u901f\u5c06\u4efb\u610fPython\u51fd\u6570\u8f6c\u6362\u4e3a\u5b88\u62a4\u8fdb\u7a0b\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b88\u62a4\u8fdb\u7a0b\u7684PID\u6587\u4ef6\u8def\u5f84\u9700\u8981\u53ef\u5199\uff0c\u4ee5\u4fbf\u5b88\u62a4\u8fdb\u7a0b\u8bb0\u5f55\u81ea\u8eabPID\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 SCHEDULER \u8ba1\u5212\u4efb\u52a1\u5b9e\u73b0\u5e38\u9a7b\u8fd0\u884c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u53ea\u9700\u8981\u5b9a\u671f\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\uff0c\u800c\u4e0d\u9700\u8981\u6301\u7eed\u8fd0\u884c\u4e00\u4e2a\u5b88\u62a4\u8fdb\u7a0b\u3002\u8fd9\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Python\u7684\u8ba1\u5212\u4efb\u52a1\u8c03\u5ea6\u5e93\uff0c\u5982<code>schedule<\/code>\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b89\u88c5 SCHEDULE \u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5<code>schedule<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install schedule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528 SCHEDULE \u5b9e\u73b0\u8ba1\u5212\u4efb\u52a1<\/h4>\n<\/p>\n<p><p><code>schedule<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u6d01\u7684\u63a5\u53e3\u6765\u5b9a\u4e49\u5b9a\u671f\u4efb\u52a1\u3002<\/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;Scheduled job is running&quot;)<\/p>\n<h2><strong>\u6bcf\u96941\u5206\u949f\u8fd0\u884c\u4e00\u6b21\u4efb\u52a1<\/strong><\/h2>\n<p>schedule.every(1).minutes.do(job)<\/p>\n<p>while True:<\/p>\n<p>    # \u68c0\u67e5\u5e76\u8fd0\u884c\u4efb\u52a1<\/p>\n<p>    schedule.run_pending()<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>job<\/code>\u51fd\u6570\u6bcf\u96941\u5206\u949f\u6267\u884c\u4e00\u6b21\u3002<code>schedule.run_pending()<\/code>\u4f1a\u68c0\u67e5\u5e76\u8fd0\u884c\u6240\u6709\u5f85\u6267\u884c\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u57fa\u4e8e\u670d\u52a1\u7ba1\u7406\u5668\u5b9e\u73b0\u5e38\u9a7b\u8fd0\u884c<\/h3>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>systemd<\/code>\u7b49\u670d\u52a1\u7ba1\u7406\u5668\u5c06Python\u811a\u672c\u4f5c\u4e3a\u670d\u52a1\u8fd0\u884c\uff0c\u5b9e\u73b0\u5e38\u9a7b\u8fd0\u884c\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u9700\u8981\u957f\u671f\u7a33\u5b9a\u8fd0\u884c\u7684\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u521b\u5efa SYSTEMD \u670d\u52a1\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728<code>\/etc\/systemd\/system\/<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u6587\u4ef6\uff0c\u5982<code>my_service.service<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code>[Unit]<\/p>\n<p>Description=My Python Service<\/p>\n<p>[Service]<\/p>\n<p>ExecStart=\/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p>Restart=always<\/p>\n<p>[Install]<\/p>\n<p>WantedBy=multi-user.target<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u7ba1\u7406\u670d\u52a1<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>systemctl<\/code>\u547d\u4ee4\u7ba1\u7406\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\"># \u542f\u52a8\u670d\u52a1<\/p>\n<p>sudo systemctl start my_service<\/p>\n<h2><strong>\u8bbe\u7f6e\u5f00\u673a\u81ea\u542f\u52a8<\/strong><\/h2>\n<p>sudo systemctl enable my_service<\/p>\n<h2><strong>\u67e5\u770b\u670d\u52a1\u72b6\u6001<\/strong><\/h2>\n<p>sudo systemctl status my_service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06Python\u811a\u672c\u4f5c\u4e3a\u7cfb\u7edf\u670d\u52a1\u8fd0\u884c\uff0c\u5b9e\u73b0\u5e38\u9a7b\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0Python\u811a\u672c\u5e38\u9a7b\u8fd0\u884c\u7684\u65b9\u5f0f\u591a\u79cd\u591a\u6837\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u3002<strong>\u5b88\u62a4\u8fdb\u7a0b\u9002\u5408\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u540e\u53f0\u4efb\u52a1\uff0c\u8ba1\u5212\u4efb\u52a1\u9002\u5408\u5b9a\u671f\u6267\u884c\u7684\u4efb\u52a1\uff0c\u7cfb\u7edf\u670d\u52a1\u9002\u5408\u9700\u8981\u7a33\u5b9a\u8fd0\u884c\u7684\u670d\u52a1<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u5f0f\uff0c\u5b9e\u73b0\u6700\u4f18\u7684\u8fd0\u884c\u6548\u679c\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u9700\u8981\u6ce8\u610f<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u548c\u5f02\u5e38\u5904\u7406\uff0c\u786e\u4fdd\u811a\u672c\u80fd\u591f\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u8ba9Python\u811a\u672c\u5728\u540e\u53f0\u8fd0\u884c\uff1f<\/strong><br \/>\u8981\u8ba9Python\u811a\u672c\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>nohup<\/code>\u547d\u4ee4\u6216<code>screen<\/code>\u5de5\u5177\u6765\u5b9e\u73b0\uff1b\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u201c\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u201d\u6765\u8bbe\u7f6e\u811a\u672c\u7684\u5b9a\u65f6\u8fd0\u884c\u3002\u786e\u4fdd\u811a\u672c\u6ca1\u6709\u4e0e\u7ec8\u7aef\u4ea4\u4e92\u7684\u90e8\u5206\uff0c\u4ee5\u907f\u514d\u5728\u540e\u53f0\u8fd0\u884c\u65f6\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u811a\u672c\u4e2d\u7684\u5f02\u5e38\u4ee5\u786e\u4fdd\u5176\u5e38\u9a7b\u8fd0\u884c\uff1f<\/strong><br \/>\u5728\u7f16\u5199\u5e38\u9a7b\u8fd0\u884c\u7684Python\u811a\u672c\u65f6\uff0c\u5f02\u5e38\u5904\u7406\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u5757\u6765\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u5e76\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u8bb0\u5f55\u65e5\u5fd7\u6216\u53d1\u9001\u901a\u77e5\u3002\u5229\u7528<code>while True<\/code>\u5faa\u73af\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u5f02\u5e38\u540e\u91cd\u65b0\u542f\u52a8\uff0c\u4fdd\u6301\u5176\u6301\u7eed\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0Python\u811a\u672c\u7684\u5e38\u9a7b\u8fd0\u884c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0Python\u811a\u672c\u7684\u5e38\u9a7b\u8fd0\u884c\u3002\u4f8b\u5982\uff0c<code>supervisor<\/code>\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u76d1\u63a7\u548c\u63a7\u5236Python\u811a\u672c\u7684\u8fd0\u884c\uff0c\u786e\u4fdd\u5b83\u4eec\u5728\u5d29\u6e83\u540e\u81ea\u52a8\u91cd\u542f\u3002\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528<code>systemd<\/code>\uff0c\u8fd9\u662fLinux\u7cfb\u7edf\u4e2d\u7684\u4e00\u79cd\u670d\u52a1\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5c06Python\u811a\u672c\u4f5c\u4e3a\u670d\u52a1\u8fd0\u884c\u5e76\u81ea\u52a8\u91cd\u542f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0\u5e38\u9a7b\u8fd0\u884c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b88\u62a4\u8fdb\u7a0b\u3001\u4f7f\u7528\u8ba1\u5212\u4efb\u52a1\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u5b88\u62a4 [&hellip;]","protected":false},"author":3,"featured_media":998373,"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\/998364"}],"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=998364"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998364\/revisions"}],"predecessor-version":[{"id":998374,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998364\/revisions\/998374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}