{"id":1163393,"date":"2025-01-15T14:54:47","date_gmt":"2025-01-15T06:54:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163393.html"},"modified":"2025-01-15T14:54:50","modified_gmt":"2025-01-15T06:54:50","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%96%ad%e7%82%b9%e9%9c%80%e4%bc%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1163393.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u65ad\u70b9\u9700\u4f20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204148\/03874544-020b-489a-9a54-3e0561740ea9.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u65ad\u70b9\u9700\u4f20\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\uff08\u5982PDB\uff09\u3001\u8bbe\u7f6e\u6761\u4ef6\u65ad\u70b9\u3001\u6355\u83b7\u5f02\u5e38\u5e76\u8bb0\u5f55\u72b6\u6001\uff0c\u53ef\u4ee5\u5b9e\u73b0 Python \u7a0b\u5e8f\u7684\u65ad\u70b9\u7eed\u4f20\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u6355\u83b7\u5f02\u5e38\u5e76\u8bb0\u5f55\u72b6\u6001<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u5b9e\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u7a0b\u5e8f\u5d29\u6e83\u6216\u4e2d\u65ad\u540e\u91cd\u65b0\u542f\u52a8\u65f6\uff0c\u80fd\u591f\u4ece\u4e2d\u65ad\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u6355\u83b7\u5f02\u5e38\u5e76\u8bb0\u5f55\u72b6\u6001\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528 try-except \u5757\u6355\u83b7\u53ef\u80fd\u5bfc\u81f4\u4e2d\u65ad\u7684\u5f02\u5e38\u3002<\/li>\n<li>\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\uff0c\u5c06\u5f53\u524d\u7684\u72b6\u6001\uff08\u5982\u53d8\u91cf\u503c\u3001\u8fdb\u5ea6\u7b49\uff09\u8bb0\u5f55\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u3002<\/li>\n<li>\u5728\u7a0b\u5e8f\u91cd\u65b0\u542f\u52a8\u65f6\uff0c\u68c0\u67e5\u662f\u5426\u5b58\u5728\u8bb0\u5f55\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u5e76\u4ece\u8be5\u72b6\u6001\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>def save_state(state, filename=&#39;state.pkl&#39;):<\/p>\n<p>    with open(filename, &#39;wb&#39;) as f:<\/p>\n<p>        pickle.dump(state, f)<\/p>\n<p>def load_state(filename=&#39;state.pkl&#39;):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;rb&#39;) as f:<\/p>\n<p>            return pickle.load(f)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return None<\/p>\n<p>def process_data(data, start_index=0):<\/p>\n<p>    try:<\/p>\n<p>        for i in range(start_index, len(data)):<\/p>\n<p>            # \u6a21\u62df\u5904\u7406\u6570\u636e<\/p>\n<p>            print(f&quot;Processing {data[i]}&quot;)<\/p>\n<p>            if i == 3:  # \u6a21\u62df\u4e2d\u65ad<\/p>\n<p>                r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se Exception(&quot;Simulated exception&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Exception occurred: {e}&quot;)<\/p>\n<p>        save_state({&#39;index&#39;: i})<\/p>\n<p>        raise<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    data = [1, 2, 3, 4, 5]<\/p>\n<p>    state = load_state()<\/p>\n<p>    start_index = state[&#39;index&#39;] + 1 if state else 0<\/p>\n<p>    process_data(data, start_index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u5728\u5904\u7406\u5230\u7b2c 3 \u4e2a\u6570\u636e\u65f6\u4f1a\u6a21\u62df\u4e00\u6b21\u5f02\u5e38\uff0c\u5e76\u5c06\u5f53\u524d\u7684\u7d22\u5f15\u72b6\u6001\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u65f6\uff0c\u4f1a\u4ece\u4fdd\u5b58\u7684\u72b6\u6001\u7ee7\u7eed\u5904\u7406\u5269\u4f59\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 PDB \u8fdb\u884c\u65ad\u70b9\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>PDB (Python Debugger) \u662f Python \u81ea\u5e26\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u9010\u884c\u6267\u884c\u4ee3\u7801\u5e76\u68c0\u67e5\u53d8\u91cf\u503c\uff0c\u4ece\u800c\u627e\u51fa\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u65ad\u70b9<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u4ee5\u4e0b\u4ee3\u7801\u884c\uff0c\u5373\u53ef\u8bbe\u7f6e\u4e00\u4e2a\u65ad\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb; pdb.set_trace()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u7a0b\u5e8f\u6267\u884c\u5230\u8fd9\u4e00\u884c\u65f6\uff0c\u4f1a\u6682\u505c\u6267\u884c\uff0c\u5e76\u8fdb\u5165\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u6a21\u5f0f\u3002\u6b64\u65f6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f93\u5165\u8c03\u8bd5\u547d\u4ee4\u6765\u68c0\u67e5\u53d8\u91cf\u503c\u3001\u9010\u884c\u6267\u884c\u4ee3\u7801\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def example_function():<\/p>\n<p>    x = 10<\/p>\n<p>    y = 20<\/p>\n<p>    import pdb; pdb.set_trace()<\/p>\n<p>    z = x + y<\/p>\n<p>    print(z)<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5e38\u7528\u8c03\u8bd5\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684 PDB \u8c03\u8bd5\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>n<\/code> \u6216 <code>next<\/code>\uff1a\u6267\u884c\u4e0b\u4e00\u884c\u4ee3\u7801<\/li>\n<li><code>c<\/code> \u6216 <code>continue<\/code>\uff1a\u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f\uff0c\u76f4\u5230\u9047\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9<\/li>\n<li><code>s<\/code> \u6216 <code>step<\/code>\uff1a\u8fdb\u5165\u51fd\u6570\u5185\u90e8\u6267\u884c<\/li>\n<li><code>p<\/code> \u6216 <code>print<\/code>\uff1a\u6253\u5370\u53d8\u91cf\u503c\uff0c\u5982 <code>p x<\/code><\/li>\n<li><code>q<\/code> \u6216 <code>quit<\/code>\uff1a\u9000\u51fa\u8c03\u8bd5\u6a21\u5f0f<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u547d\u4ee4\uff0c\u53ef\u4ee5\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u503c\uff0c\u4ece\u800c\u627e\u51fa\u95ee\u9898\u6240\u5728\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6761\u4ef6\u65ad\u70b9<\/h3>\n<\/p>\n<p><p>\u6761\u4ef6\u65ad\u70b9\u662f\u4e00\u79cd\u53ea\u5728\u7279\u5b9a\u6761\u4ef6\u6ee1\u8db3\u65f6\u624d\u4f1a\u89e6\u53d1\u7684\u65ad\u70b9\u3002\u8fd9\u79cd\u65ad\u70b9\u975e\u5e38\u9002\u7528\u4e8e\u90a3\u4e9b\u53ea\u6709\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u624d\u4f1a\u51fa\u73b0\u95ee\u9898\u7684\u4ee3\u7801\u6bb5\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u6761\u4ef6\u65ad\u70b9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>pdb.set_trace()<\/code> \u65b9\u6cd5\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u6765\u8bbe\u7f6e\u6761\u4ef6\u65ad\u70b9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def example_function():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        if i == 5:<\/p>\n<p>            import pdb; pdb.set_trace()<\/p>\n<p>        print(i)<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u65ad\u70b9\u53ea\u6709\u5728 <code>i<\/code> \u7b49\u4e8e 5 \u65f6\u624d\u4f1a\u89e6\u53d1\u3002<\/p>\n<\/p>\n<p><h4>2. \u52a8\u6001\u6761\u4ef6\u65ad\u70b9<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u8fd8\u53ef\u4ee5\u52a8\u6001\u8bbe\u7f6e\u6761\u4ef6\u65ad\u70b9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>pdb.run(&#39;for i in range(10): print(i)&#39;, globals(), locals())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u540e\uff0c\u53ef\u4ee5\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u8bbe\u7f6e\u6761\u4ef6\u65ad\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code>(Pdb) break 1 if i == 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65f6\uff0c\u65ad\u70b9\u53ea\u6709\u5728 <code>i<\/code> \u7b49\u4e8e 5 \u65f6\u624d\u4f1a\u89e6\u53d1\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6355\u83b7\u5f02\u5e38\u5e76\u8bb0\u5f55\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6709\u65f6\u7a0b\u5e8f\u4f1a\u56e0\u4e3a\u610f\u5916\u60c5\u51b5\uff08\u5982\u7f51\u7edc\u9519\u8bef\u3001\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\u7b49\uff09\u800c\u4e2d\u65ad\u3002\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u5728\u4e2d\u65ad\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u53ef\u4ee5\u5728\u6355\u83b7\u5f02\u5e38\u65f6\u8bb0\u5f55\u5f53\u524d\u7684\u72b6\u6001\uff0c\u5e76\u5728\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u65f6\u6062\u590d\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h4>1. \u6355\u83b7\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 try-except \u5757\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    print(f&quot;Exception occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bb0\u5f55\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u5c06\u5f53\u524d\u7684\u72b6\u6001\uff08\u5982\u53d8\u91cf\u503c\u3001\u8fdb\u5ea6\u7b49\uff09\u8bb0\u5f55\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>def save_state(state, filename=&#39;state.pkl&#39;):<\/p>\n<p>    with open(filename, &#39;wb&#39;) as f:<\/p>\n<p>        pickle.dump(state, f)<\/p>\n<p>def load_state(filename=&#39;state.pkl&#39;):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;rb&#39;) as f:<\/p>\n<p>            return pickle.load(f)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return None<\/p>\n<p>def process_data(data, start_index=0):<\/p>\n<p>    try:<\/p>\n<p>        for i in range(start_index, len(data)):<\/p>\n<p>            # \u6a21\u62df\u5904\u7406\u6570\u636e<\/p>\n<p>            print(f&quot;Processing {data[i]}&quot;)<\/p>\n<p>            if i == 3:  # \u6a21\u62df\u4e2d\u65ad<\/p>\n<p>                raise Exception(&quot;Simulated exception&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Exception occurred: {e}&quot;)<\/p>\n<p>        save_state({&#39;index&#39;: i})<\/p>\n<p>        raise<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    data = [1, 2, 3, 4, 5]<\/p>\n<p>    state = load_state()<\/p>\n<p>    start_index = state[&#39;index&#39;] + 1 if state else 0<\/p>\n<p>    process_data(data, start_index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u5728\u5904\u7406\u5230\u7b2c 3 \u4e2a\u6570\u636e\u65f6\u4f1a\u6a21\u62df\u4e00\u6b21\u5f02\u5e38\uff0c\u5e76\u5c06\u5f53\u524d\u7684\u7d22\u5f15\u72b6\u6001\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u91cd\u65b0\u542f\u52a8\u7a0b\u5e8f\u65f6\uff0c\u4f1a\u4ece\u4fdd\u5b58\u7684\u72b6\u6001\u7ee7\u7eed\u5904\u7406\u5269\u4f59\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u8bb0\u5f55\u72b6\u6001\u7684\u65b9\u5f0f\u53ef\u4ee5\u591a\u79cd\u591a\u6837\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u72b6\u6001\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u5b9e\u7528\u7684\u65b9\u6cd5\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528 Python \u5185\u7f6e\u7684 logging \u6a21\u5757\u6765\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u5305\u62ec\u53d8\u91cf\u503c\u3001\u6267\u884c\u8fdb\u5ea6\u3001\u9519\u8bef\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u683c\u5f0f\u548c\u7ea7\u522b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO, <\/p>\n<p>                    format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bb0\u5f55\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u4e2d\u9700\u8981\u8bb0\u5f55\u72b6\u6001\u7684\u5730\u65b9\uff0c\u4f7f\u7528 logging \u6a21\u5757\u8bb0\u5f55\u76f8\u5173\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>def process_data(data):<\/p>\n<p>    try:<\/p>\n<p>        for i, item in enumerate(data):<\/p>\n<p>            logging.info(f&quot;Processing item {i}: {item}&quot;)<\/p>\n<p>            # \u6a21\u62df\u5904\u7406\u6570\u636e<\/p>\n<p>            if i == 3:  # \u6a21\u62df\u4e2d\u65ad<\/p>\n<p>                raise Exception(&quot;Simulated exception&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&quot;Exception occurred: {e}&quot;)<\/p>\n<p>        raise<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    data = [1, 2, 3, 4, 5]<\/p>\n<p>    process_data(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5c06\u5904\u7406\u6bcf\u4e2a\u6570\u636e\u9879\u7684\u8fdb\u5ea6\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3002\u901a\u8fc7\u67e5\u770b\u65e5\u5fd7\u6587\u4ef6\uff0c\u53ef\u4ee5\u4e86\u89e3\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u548c\u4e2d\u65ad\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u88c5\u9970\u5668\u5b9e\u73b0\u65ad\u70b9\u7eed\u4f20<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u6269\u5c55\u51fd\u6570\u529f\u80fd\u7684\u9ad8\u7ea7\u7279\u6027\u3002\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u5b9e\u73b0\u65ad\u70b9\u7eed\u4f20\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9a\u4e49\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u7528\u4e8e\u5728\u51fd\u6570\u6267\u884c\u524d\u540e\u8bb0\u5f55\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>import functools<\/p>\n<p>def save_state(state, filename=&#39;state.pkl&#39;):<\/p>\n<p>    with open(filename, &#39;wb&#39;) as f:<\/p>\n<p>        pickle.dump(state, f)<\/p>\n<p>def load_state(filename=&#39;state.pkl&#39;):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;rb&#39;) as f:<\/p>\n<p>            return pickle.load(f)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return None<\/p>\n<p>def resumeable(func):<\/p>\n<p>    @functools.wraps(func)<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        state = load_state()<\/p>\n<p>        start_index = state[&#39;index&#39;] + 1 if state else 0<\/p>\n<p>        try:<\/p>\n<p>            func(*args, start_index=start_index, kwargs)<\/p>\n<p>        except Exception as e:<\/p>\n<p>            save_state({&#39;index&#39;: start_index})<\/p>\n<p>            raise<\/p>\n<p>    return wrapper<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u65ad\u70b9\u7eed\u4f20\u7684\u51fd\u6570\u4e0a\u4f7f\u7528\u88c5\u9970\u5668\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@resumeable<\/p>\n<p>def process_data(data, start_index=0):<\/p>\n<p>    for i in range(start_index, len(data)):<\/p>\n<p>        print(f&quot;Processing {data[i]}&quot;)<\/p>\n<p>        if i == 3:  # \u6a21\u62df\u4e2d\u65ad<\/p>\n<p>            raise Exception(&quot;Simulated exception&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    data = [1, 2, 3, 4, 5]<\/p>\n<p>    process_data(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>process_data<\/code> \u51fd\u6570\u4f7f\u7528\u4e86 <code>resumeable<\/code> \u88c5\u9970\u5668\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u65ad\u70b9\u7eed\u4f20\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u6570\u636e\u5e93\u8bb0\u5f55\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528\u6570\u636e\u5e93\u8bb0\u5f55\u72b6\u6001\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u53ef\u9760\u7684\u65b9\u6cd5\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff08\u5982\u53d8\u91cf\u503c\u3001\u6267\u884c\u8fdb\u5ea6\u7b49\uff09\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u4e2d\u65ad\u540e\u6062\u590d\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 SQLite \u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def get_db_connection():<\/p>\n<p>    conn = sqlite3.connect(&#39;app.db&#39;)<\/p>\n<p>    return conn<\/p>\n<p>def initialize_db():<\/p>\n<p>    conn = get_db_connection()<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;&#39;&#39;<\/p>\n<p>        CREATE TABLE IF NOT EXISTS state (<\/p>\n<p>            id INTEGER PRIMARY KEY,<\/p>\n<p>            index INTEGER<\/p>\n<p>        )<\/p>\n<p>    &#39;&#39;&#39;)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>initialize_db()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u7528\u4e8e\u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def save_state(index):<\/p>\n<p>    conn = get_db_connection()<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;DELETE FROM state&#39;)<\/p>\n<p>    cursor.execute(&#39;INSERT INTO state (index) VALUES (?)&#39;, (index,))<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>def load_state():<\/p>\n<p>    conn = get_db_connection()<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;SELECT index FROM state&#39;)<\/p>\n<p>    row = cursor.fetchone()<\/p>\n<p>    conn.close()<\/p>\n<p>    return row[0] if row else None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u6570\u636e\u5e93\u8bb0\u5f55\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data):<\/p>\n<p>    start_index = load_state() or 0<\/p>\n<p>    try:<\/p>\n<p>        for i in range(start_index, len(data)):<\/p>\n<p>            print(f&quot;Processing {data[i]}&quot;)<\/p>\n<p>            if i == 3:  # \u6a21\u62df\u4e2d\u65ad<\/p>\n<p>                raise Exception(&quot;Simulated exception&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Exception occurred: {e}&quot;)<\/p>\n<p>        save_state(i)<\/p>\n<p>        raise<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    data = [1, 2, 3, 4, 5]<\/p>\n<p>    process_data(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5c06\u5904\u7406\u5230\u7684\u7d22\u5f15\u4fdd\u5b58\u5230 SQLite \u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u5728\u91cd\u65b0\u542f\u52a8\u65f6\u4ece\u6570\u636e\u5e93\u4e2d\u52a0\u8f7d\u72b6\u6001\uff0c\u4ece\u800c\u5b9e\u73b0\u65ad\u70b9\u7eed\u4f20\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528 Redis \u8bb0\u5f55\u72b6\u6001<\/h3>\n<\/p>\n<p><p>Redis \u662f\u4e00\u79cd\u9ad8\u6027\u80fd\u7684\u952e\u503c\u5b58\u50a8\u7cfb\u7edf\uff0c\u9002\u7528\u4e8e\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Redis \u8bb0\u5f55\u7a0b\u5e8f\u7684\u53d8\u91cf\u503c\u3001\u6267\u884c\u8fdb\u5ea6\u7b49\u72b6\u6001\u4fe1\u606f\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u4e2d\u65ad\u540e\u6062\u590d\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6e Redis<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86 Redis \u5e76\u542f\u52a8 Redis \u670d\u52a1\u5668\u3002\u7136\u540e\uff0c\u5b89\u88c5 Redis \u7684 Python \u5ba2\u6237\u7aef\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install redis<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u7528\u4e8e\u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import redis<\/p>\n<p>redis_client = redis.StrictRedis(host=&#39;localhost&#39;, port=6379, db=0)<\/p>\n<p>def save_state(index):<\/p>\n<p>    redis_client.set(&#39;index&#39;, index)<\/p>\n<p>def load_state():<\/p>\n<p>    index = redis_client.get(&#39;index&#39;)<\/p>\n<p>    return int(index) if index else None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528 Redis \u8bb0\u5f55\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data):<\/p>\n<p>    start_index = load_state() or 0<\/p>\n<p>    try:<\/p>\n<p>        for i in range(start_index, len(data)):<\/p>\n<p>            print(f&quot;Processing {data[i]}&quot;)<\/p>\n<p>            if i == 3:  # \u6a21\u62df\u4e2d\u65ad<\/p>\n<p>                raise Exception(&quot;Simulated exception&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Exception occurred: {e}&quot;)<\/p>\n<p>        save_state(i)<\/p>\n<p>        raise<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    data = [1, 2, 3, 4, 5]<\/p>\n<p>    process_data(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5c06\u5904\u7406\u5230\u7684\u7d22\u5f15\u4fdd\u5b58\u5230 Redis \u4e2d\uff0c\u5e76\u5728\u91cd\u65b0\u542f\u52a8\u65f6\u4ece Redis \u4e2d\u52a0\u8f7d\u72b6\u6001\uff0c\u4ece\u800c\u5b9e\u73b0\u65ad\u70b9\u7eed\u4f20\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u4e91\u5b58\u50a8\u8bb0\u5f55\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u4f7f\u7528\u4e91\u5b58\u50a8\uff08\u5982 AWS S3\u3001Google Cloud Storage \u7b49\uff09\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u7684\u53d8\u91cf\u503c\u3001\u6267\u884c\u8fdb\u5ea6\u7b49\u72b6\u6001\u4fe1\u606f\u5b58\u50a8\u5230\u4e91\u5b58\u50a8\u4e2d\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u4e2d\u65ad\u540e\u6062\u590d\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u4e91\u5b58\u50a8<\/h4>\n<\/p>\n<p><p>\u4ee5 AWS S3 \u4e3a\u4f8b\uff0c\u9996\u5148\u5b89\u88c5 AWS \u7684 Python \u5ba2\u6237\u7aef\u5e93 Boto3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install boto3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u914d\u7f6e AWS \u8bbf\u95ee\u51ed\u8bc1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">aws configure<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u7528\u4e8e\u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import boto3<\/p>\n<p>import pickle<\/p>\n<p>s3_client = boto3.client(&#39;s3&#39;)<\/p>\n<p>bucket_name = &#39;your-bucket-name&#39;<\/p>\n<p>state_key = &#39;state.pkl&#39;<\/p>\n<p>def save_state(state):<\/p>\n<p>    with open(state_key, &#39;wb&#39;) as f:<\/p>\n<p>        pickle.dump(state, f)<\/p>\n<p>    s3_client.upload_file(state_key, bucket_name, state_key)<\/p>\n<p>def load_state():<\/p>\n<p>    try:<\/p>\n<p>        s3_client.download_file(bucket_name, state_key, state_key)<\/p>\n<p>        with open(state_key, &#39;rb&#39;) as f:<\/p>\n<p>            return pickle.load(f)<\/p>\n<p>    except s3_client.exceptions.NoSuchKey:<\/p>\n<p>        return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u4e91\u5b58\u50a8\u8bb0\u5f55\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data):<\/p>\n<p>    state = load_state()<\/p>\n<p>    start_index = state[&#39;index&#39;] + 1 if state else 0<\/p>\n<p>    try:<\/p>\n<p>        for i in range(start_index, len(data)):<\/p>\n<p>            print(f&quot;Processing {data[i]}&quot;)<\/p>\n<p>            if i == 3:  # \u6a21\u62df\u4e2d\u65ad<\/p>\n<p>                raise Exception(&quot;Simulated exception&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Exception occurred: {e}&quot;)<\/p>\n<p>        save_state({&#39;index&#39;: i})<\/p>\n<p>        raise<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    data = [1, 2, 3, 4, 5]<\/p>\n<p>    process_data(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5c06\u5904\u7406\u5230\u7684\u7d22\u5f15\u4fdd\u5b58\u5230 AWS S3 \u4e2d\uff0c\u5e76\u5728\u91cd\u65b0\u542f\u52a8\u65f6\u4ece S3 \u4e2d\u52a0\u8f7d\u72b6\u6001\uff0c\u4ece\u800c\u5b9e\u73b0\u65ad\u70b9\u7eed\u4f20\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u6d88\u606f\u961f\u5217\u8bb0\u5f55\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u4f7f\u7528\u6d88\u606f\u961f\u5217\uff08\u5982 RabbitMQ\u3001Kafka \u7b49\uff09\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u7684\u53d8\u91cf\u503c\u3001\u6267\u884c\u8fdb\u5ea6\u7b49\u72b6\u6001\u4fe1\u606f\u53d1\u9001\u5230\u6d88\u606f\u961f\u5217\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u4e2d\u65ad\u540e\u6062\u590d\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u6d88\u606f\u961f\u5217<\/h4>\n<\/p>\n<p><p>\u4ee5 RabbitMQ \u4e3a\u4f8b\uff0c\u9996\u5148\u5b89\u88c5 RabbitMQ \u7684 Python \u5ba2\u6237\u7aef\u5e93 Pika\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pika<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u914d\u7f6e RabbitMQ \u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<p>connection = pika.BlockingConnection(pika.ConnectionParameters(&#39;localhost&#39;))<\/p>\n<p>channel = connection.channel()<\/p>\n<p>channel.queue_declare(queue=&#39;state_queue&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u7528\u4e8e\u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>def save_state(state):<\/p>\n<p>    state_data = pickle.dumps(state)<\/p>\n<p>    channel.basic_publish(exchange=&#39;&#39;, routing_key=&#39;state_queue&#39;, body=state_data)<\/p>\n<p>def load_state():<\/p>\n<p>    method_frame, header_frame, body = channel.basic_get(queue=&#39;state_queue&#39;, auto_ack=True)<\/p>\n<p>    if body:<\/p>\n<p>        return pickle.loads(body)<\/p>\n<p>    return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u6d88\u606f\u961f\u5217\u8bb0\u5f55\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\u8bb0\u5f55\u548c\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data):<\/p>\n<p>    state = load_state()<\/p>\n<p>    start_index = state[&#39;index&#39;] + 1 if state else 0<\/p>\n<p>    try:<\/p>\n<p>        for i in range(start_index, len(data)):<\/p>\n<p>            print(f&quot;Processing {data[i]}&quot;)<\/p>\n<p>            if i == 3:  # \u6a21\u62df\u4e2d\u65ad<\/p>\n<p>                raise Exception(&quot;Simulated exception&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Exception occurred: {e}&quot;)<\/p>\n<p>        save_state({&#39;index&#39;: i<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>pdb<\/code>\u6a21\u5757\u8fdb\u884c\u65ad\u70b9\u8c03\u8bd5\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>import pdb; pdb.set_trace()<\/code>\uff0c\u60a8\u53ef\u4ee5\u5728\u6267\u884c\u5230\u6b64\u884c\u65f6\u6682\u505c\u7a0b\u5e8f\uff0c\u5e76\u8fdb\u5165\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u73af\u5883\u3002\u5728\u8fd9\u4e2a\u73af\u5883\u4e2d\uff0c\u60a8\u53ef\u4ee5\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\uff0c\u751a\u81f3\u4fee\u6539\u5b83\u4eec\uff0c\u8fd9\u5bf9\u67e5\u627e\u548c\u4fee\u590d\u9519\u8bef\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<p><strong>Python\u65ad\u70b9\u8c03\u8bd5\u662f\u5426\u652f\u6301\u56fe\u5f62\u754c\u9762\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684\u8c03\u8bd5\u5de5\u5177\u4e0d\u4ec5\u9650\u4e8e\u547d\u4ee4\u884c\u3002\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001Visual Studio Code\u7b49\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u5f62\u754c\u9762\u8c03\u8bd5\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u70b9\u51fb\u6765\u8bbe\u7f6e\u65ad\u70b9\uff0c\u67e5\u770b\u53d8\u91cf\uff0c\u8c03\u7528\u5806\u6808\u7b49\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u8c03\u8bd5\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u7684\u5f02\u5e38\u5904\u7406\u4e2d\u4f7f\u7528\u65ad\u70b9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5728\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u5757\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u6355\u83b7\u5f02\u5e38\u65f6\u6682\u505c\u7a0b\u5e8f\u3002\u901a\u8fc7\u5728<code>except<\/code>\u8bed\u53e5\u4e2d\u52a0\u5165<code>import pdb; pdb.set_trace()<\/code>\uff0c\u60a8\u53ef\u4ee5\u5728\u5f02\u5e38\u53d1\u751f\u65f6\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u6709\u52a9\u4e8e\u60a8\u5728\u8c03\u8bd5\u5f02\u5e38\u65f6\uff0c\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u7a0b\u5e8f\u7684\u72b6\u6001\u548c\u53d8\u91cf\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u65f6\uff0c\u6709\u54ea\u4e9b\u6700\u4f73\u5b9e\u8df5\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u65f6\uff0c\u5efa\u8bae\u60a8\u660e\u786e\u8bbe\u7f6e\u65ad\u70b9\u7684\u4f4d\u7f6e\uff0c\u907f\u514d\u5728\u4ee3\u7801\u7684\u6bcf\u4e00\u884c\u90fd\u8bbe\u7f6e\u65ad\u70b9\u3002\u4f18\u5148\u9009\u62e9\u90a3\u4e9b\u60a8\u6000\u7591\u53ef\u80fd\u51fa\u9519\u7684\u884c\u3002\u6b64\u5916\uff0c\u4fdd\u6301\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u4e5f\u6709\u52a9\u4e8e\u5728\u8c03\u8bd5\u65f6\u66f4\u5feb\u5730\u5b9a\u4f4d\u95ee\u9898\u3002\u540c\u65f6\uff0c\u5728\u8c03\u8bd5\u5b8c\u6210\u540e\uff0c\u53ca\u65f6\u6e05\u7406\u4ee3\u7801\u4e2d\u7684\u8c03\u8bd5\u8bed\u53e5\uff0c\u4fdd\u6301\u4ee3\u7801\u6574\u6d01\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\uff08\u5982PDB\uff09\u3001\u8bbe\u7f6e\u6761\u4ef6\u65ad\u70b9\u3001\u6355\u83b7\u5f02\u5e38\u5e76\u8bb0\u5f55\u72b6\u6001\uff0c\u53ef\u4ee5\u5b9e\u73b0 Python \u7a0b\u5e8f\u7684\u65ad\u70b9\u7eed\u4f20\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1163404,"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\/1163393"}],"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=1163393"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163393\/revisions"}],"predecessor-version":[{"id":1163406,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163393\/revisions\/1163406"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1163404"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1163393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1163393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1163393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}