{"id":1073103,"date":"2025-01-08T11:22:11","date_gmt":"2025-01-08T03:22:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073103.html"},"modified":"2025-01-08T11:22:14","modified_gmt":"2025-01-08T03:22:14","slug":"%e5%a6%82%e4%bd%95%e6%98%afpython%e7%a8%8b%e5%ba%8f%e4%b8%80%e7%9b%b4%e8%bf%90%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073103.html","title":{"rendered":"\u5982\u4f55\u662fpython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103119\/fa7e9cad-5f02-478c-9186-1772e3e484f7.webp\" alt=\"\u5982\u4f55\u662fpython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>\u8981\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u3001\u9012\u5f52\u8c03\u7528\u3001\u540e\u53f0\u5b88\u62a4\u8fdb\u7a0b\u7b49\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528while\u5faa\u73af\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u660e\u4e86\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528while\u5faa\u73af\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\uff0c\u5e76\u63a2\u8ba8\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528while\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528while\u5faa\u73af\u662f\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u53ea\u9700\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\uff0c\u4f7f\u5176\u6c38\u8fdc\u4e3a\u771f\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u4e00\u76f4\u6267\u884c\u4e0b\u53bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    # \u4f60\u7684\u4ee3\u7801<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>while True:<\/code>\u8868\u793a\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u4e00\u76f4\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\u3002\u7531\u4e8e\u6761\u4ef6\u201cTrue\u201d\u6c38\u8fdc\u4e3a\u771f\uff0c\u56e0\u6b64\u7a0b\u5e8f\u4f1a\u65e0\u9650\u671f\u5730\u8fd0\u884c\u3002\u4f60\u53ef\u4ee5\u5728\u5faa\u73af\u4f53\u5185\u6dfb\u52a0\u4efb\u4f55\u4f60\u9700\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528while\u5faa\u73af\u4f7f\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u5e76\u6253\u5370\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>while True:<\/p>\n<p>    print(&quot;Current time:&quot;, time.ctime())<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u6bcf\u79d2\u6253\u5370\u4e00\u6b21\u5f53\u524d\u65f6\u95f4\u3002<code>time.sleep(1)<\/code>\u4f7f\u7a0b\u5e8f\u6682\u505c1\u79d2\uff0c\u4ee5\u907f\u514d\u6253\u5370\u8fc7\u5feb\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9012\u5f52\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u8c03\u7528\u4e5f\u662f\u4e00\u79cd\u4f7f\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u7684\u65b9\u6cd5\uff0c\u4e0d\u8fc7\u5b83\u4e0d\u5982while\u5faa\u73af\u90a3\u6837\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9012\u5f52\u8c03\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def run_forever():<\/p>\n<p>    print(&quot;This function calls itself!&quot;)<\/p>\n<p>    run_forever()<\/p>\n<p>run_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>run_forever<\/code>\u51fd\u6570\u5728\u5176\u5185\u90e8\u8c03\u7528\u81ea\u8eab\uff0c\u4ece\u800c\u5bfc\u81f4\u65e0\u9650\u9012\u5f52\u8c03\u7528\u3002\u8fd9\u5c06\u4f7f\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\u9519\u8bef\uff08RecursionError\uff09\uff0c\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u5360\u7528\u6808\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u540e\u53f0\u5b88\u62a4\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u540e\u53f0\u5b88\u62a4\u8fdb\u7a0b\u662f\u4e00\u79cd\u4f7f\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u7684\u9ad8\u7ea7\u65b9\u6cd5\uff0c\u901a\u5e38\u7528\u4e8e\u670d\u52a1\u5668\u6216\u540e\u53f0\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528threading\u6a21\u5757\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b<\/h4>\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 in the background&quot;)<\/p>\n<p>        time.sleep(2)<\/p>\n<p>thread = threading.Thread(target=background_task, daemon=True)<\/p>\n<p>thread.start()<\/p>\n<p>while True:<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>threading.Thread<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u540e\u53f0\u7ebf\u7a0b\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u8fdb\u7a0b\uff08daemon=True\uff09\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u4e3b\u7ebf\u7a0b\u7ed3\u675f\uff0c\u540e\u53f0\u7ebf\u7a0b\u4e5f\u4f1a\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b9a\u65f6\u5668<\/h3>\n<\/p>\n<p><p>\u5b9a\u65f6\u5668\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5185\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528sched\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sched<\/p>\n<p>import time<\/p>\n<p>scheduler = sched.scheduler(time.time, time.sleep)<\/p>\n<p>def periodic_task():<\/p>\n<p>    print(&quot;Running periodic task&quot;)<\/p>\n<p>    scheduler.enter(2, 1, periodic_task)<\/p>\n<p>scheduler.enter(2, 1, periodic_task)<\/p>\n<p>scheduler.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>sched.scheduler<\/code>\u7528\u4e8e\u8c03\u5ea6\u4efb\u52a1\uff0c\u6bcf\u96942\u79d2\u6267\u884c\u4e00\u6b21<code>periodic_task<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u7f16\u7a0b\u662f\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u7684\u4e00\u79cd\u6709\u6548\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528asyncio\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Running async task&quot;)<\/p>\n<p>        await asyncio.sleep(2)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757\u521b\u5efa\u4e86\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u7a0b\u5e8f\u6bcf\u96942\u79d2\u6253\u5370\u4e00\u6b21\u6d88\u606f\u3002<code>asyncio.run(main())<\/code>\u7528\u4e8e\u8fd0\u884c\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u9519\u8bef\u5904\u7406\u4e0e<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u4e0e\u8d44\u6e90\u7ba1\u7406\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9700\u8981\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5904\u7406\u5f02\u5e38\uff0c\u5e76\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528try-except\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        print(&quot;Running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    except KeyboardInterrupt:<\/p>\n<p>        print(&quot;Program interrupted by user&quot;)<\/p>\n<p>        break<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u5e76\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u7ec8\u6b62\u7a0b\u5e8f\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u7a0b\u5e8f\u4e2d\uff0c\u65e5\u5fd7\u8bb0\u5f55\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u8bb0\u5f55\u65e5\u5fd7\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8c03\u8bd5\u548c\u76d1\u63a7\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528logging\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import time<\/p>\n<p>logging.basicConfig(level=logging.INFO, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        logging.info(&quot;Running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    except KeyboardInterrupt:<\/p>\n<p>        logging.info(&quot;Program interrupted by user&quot;)<\/p>\n<p>        break<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u548c\u9519\u8bef\u4fe1\u606f\u3002\u901a\u8fc7\u65e5\u5fd7\u8bb0\u5f55\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u76d1\u63a7\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\uff0c\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u7a0b\u5e8f\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u6267\u884c\u590d\u6742\u4efb\u52a1\u65f6\u3002\u901a\u8fc7\u4f18\u5316\u4ee3\u7801\u548c\u7b97\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def optimized_task():<\/p>\n<p>    for _ in range(1000000):<\/p>\n<p>        pass<\/p>\n<p>while True:<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    optimized_task()<\/p>\n<p>    end_time = time.time()<\/p>\n<p>    print(f&quot;Task completed in {end_time - start_time} seconds&quot;)<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5bf9\u4efb\u52a1\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u5e76\u8bb0\u5f55\u4e86\u4efb\u52a1\u7684\u6267\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8bc4\u4f30\u548c\u4f18\u5316\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u521b\u5efa\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u5e76\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\u548c\u4fbf\u5229\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Celery<\/h4>\n<\/p>\n<p><p>Celery\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5206\u5e03\u5f0f\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u540e\u53f0\u4efb\u52a1\u548c\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from celery import Celery<\/p>\n<p>app = Celery(&#39;tasks&#39;, broker=&#39;pyamqp:\/\/guest@localhost\/\/&#39;)<\/p>\n<p>@app.task<\/p>\n<p>def add(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    while True:<\/p>\n<p>        result = add.delay(4, 6)<\/p>\n<p>        print(&quot;Task result:&quot;, result.get(timeout=10))<\/p>\n<p>        time.sleep(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Celery\u521b\u5efa\u4e86\u4e00\u4e2a\u5206\u5e03\u5f0f\u4efb\u52a1\u961f\u5217\uff0c\u5e76\u5b9a\u671f\u6267\u884c\u4efb\u52a1\u3002Celery\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4efb\u52a1\u8c03\u5ea6\u548c\u7ba1\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u540e\u53f0\u4efb\u52a1\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u8d44\u6e90\u76d1\u63a7<\/h3>\n<\/p>\n<p><p>\u5728\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u7a0b\u5e8f\u4e2d\uff0c\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90\uff08\u5982CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u7b49\uff09\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u76d1\u63a7\u8d44\u6e90\uff0c\u53ef\u4ee5\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528psutil\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>import time<\/p>\n<p>while True:<\/p>\n<p>    cpu_usage = psutil.cpu_percent(interval=1)<\/p>\n<p>    memory_info = psutil.virtual_memory()<\/p>\n<p>    print(f&quot;CPU usage: {cpu_usage}%&quot;)<\/p>\n<p>    print(f&quot;Memory usage: {memory_info.percent}%&quot;)<\/p>\n<p>    time.sleep(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>psutil<\/code>\u6a21\u5757\u76d1\u63a7\u7cfb\u7edf\u7684CPU\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u53ca\u65f6\u4e86\u89e3\u7cfb\u7edf\u8d44\u6e90\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u786e\u4fdd\u7a0b\u5e8f\u8fd0\u884c\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fwhile\u5faa\u73af\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u8fd8\u53ef\u4ee5\u9009\u62e9\u9012\u5f52\u8c03\u7528\u3001\u540e\u53f0\u5b88\u62a4\u8fdb\u7a0b\u3001\u5b9a\u65f6\u5668\u3001\u5f02\u6b65\u7f16\u7a0b\u7b49\u65b9\u6cd5\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u8003\u8651\u9519\u8bef\u5904\u7406\u3001\u8d44\u6e90\u7ba1\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u4f18\u5316\u548c\u8d44\u6e90\u76d1\u63a7\u7b49\u95ee\u9898\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\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\u540e\u53f0\u6301\u7eed\u8fd0\u884c\uff1f<\/strong><br \/>\u8981\u8ba9Python\u7a0b\u5e8f\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u4efb\u52a1\u7ba1\u7406\u5de5\u5177\uff0c\u6bd4\u5982\u5728Linux\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>nohup<\/code>\u547d\u4ee4\u6216\u5c06\u7a0b\u5e8f\u4f5c\u4e3a\u670d\u52a1\u8fd0\u884c\u3002\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528Task Scheduler\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\u6765\u6267\u884cPython\u811a\u672c\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u50cf<code>screen<\/code>\u6216<code>tmux<\/code>\u8fd9\u6837\u7684\u7ec8\u7aef\u590d\u7528\u5668\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u5173\u95ed\u7ec8\u7aef\u540e\u7ee7\u7eed\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u7684Python\u7a0b\u5e8f\u5d29\u6e83\u4e86\uff0c\u5982\u4f55\u81ea\u52a8\u91cd\u542f\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fddPython\u7a0b\u5e8f\u5728\u5d29\u6e83\u540e\u81ea\u52a8\u91cd\u542f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>supervisor<\/code>\u6216<code>systemd<\/code>\u7b49\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u76d1\u63a7\u4f60\u7684Python\u811a\u672c\uff0c\u5e76\u5728\u5b83\u610f\u5916\u9000\u51fa\u65f6\u81ea\u52a8\u91cd\u65b0\u542f\u52a8\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5b9e\u73b0\u5f02\u5e38\u5904\u7406\uff0c\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5728\u6355\u83b7\u5230\u7279\u5b9a\u5f02\u5e38\u65f6\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u6211\u7684Python\u7a0b\u5e8f\u4ee5\u63d0\u9ad8\u5176\u6301\u7eed\u8fd0\u884c\u7684\u7a33\u5b9a\u6027\uff1f<\/strong><br \/>\u4f18\u5316Python\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u53ef\u4ee5\u4ece\u591a\u4e2a\u65b9\u9762\u5165\u624b\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\uff0c\u907f\u514d\u5e38\u89c1\u7684\u5185\u5b58\u6cc4\u6f0f\u548c\u65e0\u9650\u5faa\u73af\u95ee\u9898\u3002\u5176\u6b21\uff0c\u5b9a\u671f\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\uff0c\u8bc6\u522b\u5e76\u4fee\u590d\u74f6\u9888\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u76d1\u63a7\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u53ca\u65f6\u53d1\u73b0\u6f5c\u5728\u95ee\u9898\u3002\u9002\u5f53\u7684\u8d44\u6e90\u7ba1\u7406\uff0c\u5982\u5173\u95ed\u4e0d\u518d\u4f7f\u7528\u7684\u6587\u4ef6\u548c\u8fde\u63a5\uff0c\u4e5f\u6709\u52a9\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u3001\u9012\u5f52\u8c03\u7528\u3001\u540e\u53f0\u5b88\u62a4\u8fdb\u7a0b\u7b49\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528w [&hellip;]","protected":false},"author":3,"featured_media":1073112,"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\/1073103"}],"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=1073103"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073103\/revisions"}],"predecessor-version":[{"id":1073114,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073103\/revisions\/1073114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073112"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}