{"id":1065656,"date":"2024-12-31T16:19:27","date_gmt":"2024-12-31T08:19:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1065656.html"},"modified":"2024-12-31T16:19:30","modified_gmt":"2024-12-31T08:19:30","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9python%e6%96%87%e4%bb%b6%e4%b8%80%e7%9b%b4%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1065656.html","title":{"rendered":"\u5982\u4f55\u8ba9Python\u6587\u4ef6\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\/caaf50fe-cdf0-448a-8529-369567a0f242.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982<strong>\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u3001\u4f7f\u7528\u8c03\u5ea6\u5668\u3001\u5b88\u62a4\u8fdb\u7a0b<\/strong>\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u9010\u4e00\u5c55\u5f00\u8ba8\u8bba\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u65e0\u9650\u5faa\u73af<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u65e0\u9650\u5faa\u73af<\/strong>\u662f\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u52a0\u5165\u4e00\u4e2a<code>while True<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u6301\u7eed\u8fd0\u884c\uff0c\u76f4\u5230\u624b\u52a8\u7ec8\u6b62\u7a0b\u5e8f\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b9e\u73b0\u65e0\u9650\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while True<\/code>\u8bed\u53e5\u6765\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    # \u4f60\u7684\u4ee3\u7801<\/p>\n<p>    print(&quot;\u7a0b\u5e8f\u8fd0\u884c\u4e2d...&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u4e0d\u65ad\u6253\u5370\u201c\u7a0b\u5e8f\u8fd0\u884c\u4e2d&#8230;\u201d\uff0c\u76f4\u5230\u624b\u52a8\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u63a7\u5236\u5faa\u73af\u95f4\u9694<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u9632\u6b62\u65e0\u9650\u5faa\u73af\u5360\u7528\u8fc7\u591a\u7684CPU\u8d44\u6e90\uff0c\u53ef\u4ee5\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u52a0\u5165\u4e00\u4e2a\u5ef6\u8fdf\u3002\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u7684<code>sleep<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>while True:<\/p>\n<p>    # \u4f60\u7684\u4ee3\u7801<\/p>\n<p>    print(&quot;\u7a0b\u5e8f\u8fd0\u884c\u4e2d...&quot;)<\/p>\n<p>    time.sleep(1)  # \u5ef6\u8fdf1\u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u5faa\u73af\u4e2d\u52a0\u5165<code>time.sleep(1)<\/code>\uff0c\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u6bcf1\u79d2\u949f\u6267\u884c\u4e00\u6b21\uff0c\u4ece\u800c\u51cf\u5c11\u5bf9\u7cfb\u7edf\u8d44\u6e90\u7684\u5360\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u8c03\u5ea6\u5668<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u8c03\u5ea6\u5668<\/strong>\uff08\u5982<code>schedule<\/code>\u6a21\u5757\uff09\u53ef\u4ee5\u5728\u7279\u5b9a\u65f6\u95f4\u95f4\u9694\u5185\u6267\u884c\u4efb\u52a1\uff0c\u4ece\u800c\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5<code>schedule<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>schedule<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install schedule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528<code>schedule<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>schedule<\/code>\u6a21\u5757\u7684\u4f8b\u5b50\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;\u6267\u884c\u4efb\u52a1...&quot;)<\/p>\n<p>schedule.every(1).minutes.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><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>job<\/code>\u51fd\u6570\u5c06\u5728\u6bcf\u5206\u949f\u6267\u884c\u4e00\u6b21\uff0c\u7a0b\u5e8f\u5c06\u4e00\u76f4\u8fd0\u884c\u5e76\u7b49\u5f85\u4efb\u52a1\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b88\u62a4\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5b88\u62a4\u8fdb\u7a0b<\/strong>\u53ef\u4ee5\u8ba9Python\u6587\u4ef6\u5728\u540e\u53f0\u4e00\u76f4\u8fd0\u884c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6301\u7eed\u8fd0\u884c\u7684\u670d\u52a1\u6216\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528<code>threading<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def daemon_task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u5b88\u62a4\u8fdb\u7a0b\u8fd0\u884c\u4e2d...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>daemon_thread = threading.Thread(target=daemon_task)<\/p>\n<p>daemon_thread.setDaemon(True)<\/p>\n<p>daemon_thread.start()<\/p>\n<p>while True:<\/p>\n<p>    # \u4e3b\u7ebf\u7a0b\u7684\u5176\u4ed6\u4efb\u52a1<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>daemon_task<\/code>\u51fd\u6570\u5c06\u5728\u5b88\u62a4\u7ebf\u7a0b\u4e2d\u8fd0\u884c\uff0c\u4e3b\u7ebf\u7a0b\u5c06\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u6765\u521b\u5efa\u4e00\u4e2a\u5b88\u62a4\u8fdb\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def daemon_task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u5b88\u62a4\u8fdb\u7a0b\u8fd0\u884c\u4e2d...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>daemon_process = multiprocessing.Process(target=daemon_task)<\/p>\n<p>daemon_process.daemon = True<\/p>\n<p>daemon_process.start()<\/p>\n<p>while True:<\/p>\n<p>    # \u4e3b\u8fdb\u7a0b\u7684\u5176\u4ed6\u4efb\u52a1<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>daemon_task<\/code>\u51fd\u6570\u5c06\u5728\u5b88\u62a4\u8fdb\u7a0b\u4e2d\u8fd0\u884c\uff0c\u4e3b\u8fdb\u7a0b\u5c06\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u670d\u52a1<\/h3>\n<\/p>\n<p><p><strong>\u5c06Python\u811a\u672c\u4f5c\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u670d\u52a1<\/strong>\u8fd0\u884c\uff0c\u53ef\u4ee5\u786e\u4fdd\u811a\u672c\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u81ea\u52a8\u542f\u52a8\uff0c\u5e76\u4e14\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5728Windows\u4e0a\u521b\u5efa\u670d\u52a1<\/h4>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pywin32<\/code>\u6a21\u5757\u521b\u5efa\u4e00\u4e2aWindows\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Windows\u670d\u52a1\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32serviceutil<\/p>\n<p>import win32service<\/p>\n<p>import win32event<\/p>\n<p>class MyService(win32serviceutil.ServiceFramework):<\/p>\n<p>    _svc_name_ = &quot;MyService&quot;<\/p>\n<p>    _svc_display_name_ = &quot;My Python Service&quot;<\/p>\n<p>    def __init__(self, args):<\/p>\n<p>        win32serviceutil.ServiceFramework.__init__(self, args)<\/p>\n<p>        self.hW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tStop = win32event.CreateEvent(None, 0, 0, None)<\/p>\n<p>    def SvcStop(self):<\/p>\n<p>        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)<\/p>\n<p>        win32event.SetEvent(self.hWaitStop)<\/p>\n<p>    def SvcDoRun(self):<\/p>\n<p>        import servicemanager<\/p>\n<p>        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,<\/p>\n<p>                              servicemanager.PYS_SERVICE_STARTED,<\/p>\n<p>                              (self._svc_name_, &#39;&#39;))<\/p>\n<p>        self.main()<\/p>\n<p>    def main(self):<\/p>\n<p>        while True:<\/p>\n<p>            # \u4f60\u7684\u4ee3\u7801<\/p>\n<p>            print(&quot;\u670d\u52a1\u8fd0\u884c\u4e2d...&quot;)<\/p>\n<p>            time.sleep(1)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    win32serviceutil.HandleCommandLine(MyService)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u5728Linux\u4e0a\u521b\u5efa\u670d\u52a1<\/h4>\n<\/p>\n<p><p>\u5728Linux\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>systemd<\/code>\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u6587\u4ef6\uff0c\u4f8b\u5982<code>\/etc\/systemd\/system\/myservice.service<\/code>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-ini\">[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<ol start=\"2\">\n<li>\u542f\u52a8\u5e76\u542f\u7528\u670d\u52a1\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">sudo systemctl start myservice<\/p>\n<p>sudo systemctl enable myservice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cPython\u811a\u672c\u5c06\u4f5c\u4e3a\u7cfb\u7edf\u670d\u52a1\u8fd0\u884c\uff0c\u5e76\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u81ea\u52a8\u542f\u52a8\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u5668<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u5668<\/strong>\u53ef\u4ee5\u5728\u7279\u5b9a\u65f6\u95f4\u95f4\u9694\u5185\u81ea\u52a8\u8fd0\u884cPython\u811a\u672c\uff0c\u4ece\u800c\u5b9e\u73b0\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5728Windows\u4e0a\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u8ba1\u5212\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\uff0c\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u4efb\u52a1\u3002<\/li>\n<li>\u8bbe\u7f6e\u89e6\u53d1\u5668\u4e3a\u201c\u6bcf\u65e5\u201d\u3001\u201c\u6bcf\u5c0f\u65f6\u201d\u6216\u5176\u4ed6\u65f6\u95f4\u95f4\u9694\u3002<\/li>\n<li>\u8bbe\u7f6e\u64cd\u4f5c\u4e3a\u201c\u542f\u52a8\u7a0b\u5e8f\u201d\uff0c\u5e76\u9009\u62e9Python\u89e3\u91ca\u5668\u548c\u811a\u672c\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h4>5.2 \u5728Linux\u4e0a\u4f7f\u7528cron<\/h4>\n<\/p>\n<p><p>\u5728Linux\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cron<\/code>\u6765\u8c03\u5ea6\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u7f16\u8f91<code>crontab<\/code>\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">crontab -e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6dfb\u52a0\u4e00\u4e2a\u65b0\u6761\u76ee\uff0c\u4f8b\u5982\u6bcf\u5206\u949f\u8fd0\u884c\u4e00\u6b21\u811a\u672c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">* * * * * \/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cPython\u811a\u672c\u5c06\u6309\u7167\u8bbe\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u81ea\u52a8\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Docker<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528Docker<\/strong>\u53ef\u4ee5\u5c06Python\u811a\u672c\u5c01\u88c5\u5728\u4e00\u4e2a\u5bb9\u5668\u4e2d\uff0c\u5e76\u786e\u4fdd\u5176\u6301\u7eed\u8fd0\u884c\u3002Docker\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9694\u79bb\u7684\u8fd0\u884c\u73af\u5883\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u8de8\u5e73\u53f0\u90e8\u7f72\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u521b\u5efaDockerfile<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a<code>Dockerfile<\/code>\uff0c\u5b9a\u4e49\u8fd0\u884c\u73af\u5883\u548c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-Dockerfile\">FROM python:3.9-slim<\/p>\n<p>WORKDIR \/app<\/p>\n<p>COPY your_script.py .<\/p>\n<p>CMD [&quot;python&quot;, &quot;your_script.py&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u6784\u5efaDocker\u955c\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6784\u5efaDocker\u955c\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker build -t my-python-app .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3 \u8fd0\u884cDocker\u5bb9\u5668<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cDocker\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker run -d --name my-python-container my-python-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cPython\u811a\u672c\u5c06\u6301\u7eed\u8fd0\u884c\u5728Docker\u5bb9\u5668\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8f7b\u677e\u90e8\u7f72\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c\u65f6\uff0c<strong>\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/strong>\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u786e\u4fdd\u7a0b\u5e8f\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u8bb0\u5f55\u65e5\u5fd7\u4ee5\u4fbf\u8c03\u8bd5\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        # \u4f60\u7684\u4ee3\u7801<\/p>\n<p>        print(&quot;\u7a0b\u5e8f\u8fd0\u884c\u4e2d...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;\u53d1\u751f\u9519\u8bef: {e}&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u6765\u8bb0\u5f55\u65e5\u5fd7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import time<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO)<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        # \u4f60\u7684\u4ee3\u7801<\/p>\n<p>        logging.info(&quot;\u7a0b\u5e8f\u8fd0\u884c\u4e2d...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&quot;\u53d1\u751f\u9519\u8bef: {e}&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\u548c\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u548c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u65e0\u9650\u5faa\u73af\u3001\u4f7f\u7528\u8c03\u5ea6\u5668\u3001\u5b88\u62a4\u8fdb\u7a0b\u3001\u64cd\u4f5c\u7cfb\u7edf\u670d\u52a1\u3001\u4efb\u52a1\u8c03\u5ea6\u5668\u3001Docker<\/strong>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u8fd0\u884c\u73af\u5883\u6765\u51b3\u5b9a\u3002\u540c\u65f6\uff0c<strong>\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/strong>\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5b9a\u8fd0\u884c\u7684\u91cd\u8981\u624b\u6bb5\u3002\u901a\u8fc7\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u5b9e\u73b0\uff0c\u53ef\u4ee5\u8ba9Python\u6587\u4ef6\u6301\u7eed\u7a33\u5b9a\u5730\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4fdd\u6301Python\u811a\u672c\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\uff1f<\/strong><br \/>\u8981\u8ba9Python\u811a\u672c\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u6bd4\u5982\u4f7f\u7528nohup\u547d\u4ee4\u3001screen\u6216tmux\u7b49\u7ec8\u7aef\u590d\u7528\u5de5\u5177\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>nohup python your_script.py &amp;<\/code>\uff0c\u8fd9\u6837\u5373\u4f7f\u5173\u95ed\u7ec8\u7aef\uff0c\u811a\u672c\u4e5f\u4f1a\u7ee7\u7eed\u8fd0\u884c\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>screen<\/code>\u6216<code>tmux<\/code>\u53ef\u4ee5\u8ba9\u4f60\u5728\u591a\u4e2a\u4f1a\u8bdd\u4e2d\u7ba1\u7406\u548c\u6062\u590d\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5728Linux\u4e0a\u5982\u4f55\u8bbe\u7f6ePython\u7a0b\u5e8f\u81ea\u52a8\u91cd\u542f\uff1f<\/strong><br \/>\u5bf9\u4e8e\u9700\u8981\u957f\u65f6\u95f4\u8fd0\u884c\u7684Python\u7a0b\u5e8f\uff0c\u8bbe\u7f6e\u81ea\u52a8\u91cd\u542f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u53ef\u4ee5\u4f7f\u7528\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\u5982supervisor\u3001systemd\u6216pm2\u7b49\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u914d\u7f6e\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u5d29\u6e83\u540e\u81ea\u52a8\u91cd\u542f\uff0c\u786e\u4fdd\u670d\u52a1\u7684\u9ad8\u53ef\u7528\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316Python\u7a0b\u5e8f\u4ee5\u63d0\u9ad8\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fddPython\u7a0b\u5e8f\u5728\u957f\u65f6\u95f4\u8fd0\u884c\u65f6\u4e0d\u4f1a\u51fa\u73b0\u5185\u5b58\u6cc4\u6f0f\u6216\u6027\u80fd\u4e0b\u964d\uff0c\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u4f18\u5316\u63aa\u65bd\u3002\u5b9a\u671f\u6e05\u7406\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\u3001\u4f7f\u7528\u751f\u6210\u5668\u4ee3\u66ff\u5217\u8868\u4ee5\u53ca\u5b9a\u671f\u8fdb\u884c\u5783\u573e\u56de\u6536\u90fd\u662f\u6709\u6548\u7684\u65b9\u5f0f\u3002\u6b64\u5916\uff0c\u5408\u7406\u8bbe\u8ba1\u7a0b\u5e8f\u7684\u903b\u8f91\uff0c\u907f\u514d\u65e0\u6548\u7684\u5faa\u73af\u548c\u8fc7\u591a\u7684\u8d44\u6e90\u5360\u7528\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c \u8981\u8ba9Python\u6587\u4ef6\u4e00\u76f4\u8fd0\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u3001\u4f7f\u7528\u8c03\u5ea6\u5668 [&hellip;]","protected":false},"author":3,"featured_media":1065665,"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\/1065656"}],"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=1065656"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065656\/revisions"}],"predecessor-version":[{"id":1065666,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065656\/revisions\/1065666"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1065665"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1065656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1065656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1065656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}