{"id":1178869,"date":"2025-01-15T18:16:14","date_gmt":"2025-01-15T10:16:14","guid":{"rendered":""},"modified":"2025-01-15T18:16:17","modified_gmt":"2025-01-15T10:16:17","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e8%bf%90%e8%a1%8c%e7%8a%b6%e6%80%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178869.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u8fd0\u884c\u72b6\u6001"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113424\/dd8d88b3-a78d-4f45-a457-33b2e14326dd.webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u8fd0\u884c\u72b6\u6001\" \/><\/p>\n<p><p> \u67e5\u770bPython\u8fd0\u884c\u72b6\u6001\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u5982<strong>\u4f7f\u7528<code>print<\/code>\u8bed\u53e5\u3001\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u3001\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\u3001\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90<\/strong>\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373<strong>\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u67e5\u770bPython\u8fd0\u884c\u72b6\u6001\u7684\u65b9\u6cd5\u3002\u4e0e\u7b80\u5355\u7684<code>print<\/code>\u8bed\u53e5\u4e0d\u540c\uff0c\u65e5\u5fd7\u6a21\u5757\u53ef\u4ee5\u63d0\u4f9b\u66f4\u4e3a\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u5e76\u4e14\u53ef\u4ee5\u8bb0\u5f55\u5230\u6587\u4ef6\u4e2d\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u3002Python\u5185\u7f6e\u7684<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff08\u5982DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\uff09\uff0c\u5e76\u652f\u6301\u65e5\u5fd7\u7684\u683c\u5f0f\u5316\u8f93\u51fa\u3001\u65e5\u5fd7\u7684\u6301\u4e45\u5316\u5b58\u50a8\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165<code>logging<\/code>\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\u3002<\/li>\n<li><strong>\u914d\u7f6e\u65e5\u5fd7<\/strong>\uff1a\u8bbe\u7f6e\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\u3001\u65e5\u5fd7\u7ea7\u522b\u3001\u8f93\u51fa\u4f4d\u7f6e\u7b49\u3002<\/li>\n<li><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong>\uff1a\u5728\u4ee3\u7801\u4e2d\u9700\u8981\u8bb0\u5f55\u65e5\u5fd7\u7684\u4f4d\u7f6e\u8c03\u7528\u76f8\u5e94\u7684<code>logging<\/code>\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG, <\/p>\n<p>                    format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;,<\/p>\n<p>                    filename=&#39;app.log&#39;,<\/p>\n<p>                    filemode=&#39;w&#39;)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logging.debug(&#39;\u8fd9\u662f\u4e00\u4e2a\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p>logging.info(&#39;\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u4fe1\u606f&#39;)<\/p>\n<p>logging.warning(&#39;\u8fd9\u662f\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f&#39;)<\/p>\n<p>logging.error(&#39;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p>logging.critical(&#39;\u8fd9\u662f\u4e00\u4e2a\u4e25\u91cd\u9519\u8bef\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u65e5\u5fd7\u5c06\u88ab\u8bb0\u5f55\u5230\u6587\u4ef6<code>app.log<\/code>\u4e2d\uff0c\u5e76\u4e14\u6bcf\u6761\u65e5\u5fd7\u90fd\u5305\u542b\u65f6\u95f4\u6233\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u6d88\u606f\u5185\u5bb9\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>print<\/code>\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>print<\/code>\u8bed\u53e5\u662f\u6700\u7b80\u5355\u7684\u67e5\u770bPython\u8fd0\u884c\u72b6\u6001\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>print<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u3001\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    result = a + b<\/p>\n<p>    print(f&quot;Adding {a} and {b} to get {result}&quot;)<\/p>\n<p>    return result<\/p>\n<p>add(2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>print<\/code>\u8bed\u53e5\u975e\u5e38\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5b83\u4e0d\u9002\u5408\u7528\u4e8e\u590d\u6742\u7684\u9879\u76ee\u4e2d\uff0c\u56e0\u4e3a\u5b83\u65e0\u6cd5\u63d0\u4f9b\u8db3\u591f\u7684\u65e5\u5fd7\u7ea7\u522b\u63a7\u5236\uff0c\u4e5f\u65e0\u6cd5\u65b9\u4fbf\u5730\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. \u65e5\u5fd7\u7ea7\u522b<\/h4>\n<\/p>\n<p><p><code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e94\u4e2a\u65e5\u5fd7\u7ea7\u522b\uff1a<\/p>\n<\/p>\n<ul>\n<li>DEBUG\uff1a\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u901a\u5e38\u7528\u4e8e\u8bca\u65ad\u95ee\u9898<\/li>\n<li>INFO\uff1a\u786e\u8ba4\u7a0b\u5e8f\u6309\u9884\u671f\u8fd0\u884c\u7684\u4fe1\u606f<\/li>\n<li>WARNING\uff1a\u8868\u793a\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u7684\u8b66\u544a\u4fe1\u606f<\/li>\n<li>ERROR\uff1a\u8868\u793a\u7531\u4e8e\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u7a0b\u5e8f\u7684\u4e00\u4e9b\u529f\u80fd\u5df2\u7ecf\u4e0d\u80fd\u6b63\u5e38\u6267\u884c<\/li>\n<li>CRITICAL\uff1a\u8868\u793a\u975e\u5e38\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u884c<\/li>\n<\/ul>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u6765\u63a7\u5236\u8f93\u51fa\u7684\u65e5\u5fd7\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>logging.debug(&#39;\u8fd9\u6761\u4fe1\u606f\u4e0d\u4f1a\u88ab\u8f93\u51fa&#39;)<\/p>\n<p>logging.info(&#39;\u8fd9\u6761\u4fe1\u606f\u4f1a\u88ab\u8f93\u51fa&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u65e5\u5fd7\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u65e5\u5fd7\u7684\u8f93\u51fa\u5185\u5bb9\u3002\u65e5\u5fd7\u683c\u5f0f\u5b57\u7b26\u4e32\u53ef\u4ee5\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>%(asctime)s<\/code>\uff1a\u65e5\u5fd7\u4e8b\u4ef6\u53d1\u751f\u7684\u65f6\u95f4<\/li>\n<li><code>%(levelname)s<\/code>\uff1a\u65e5\u5fd7\u7ea7\u522b<\/li>\n<li><code>%(message)s<\/code>\uff1a\u65e5\u5fd7\u6d88\u606f<\/li>\n<li><code>%(filename)s<\/code>\uff1a\u8c03\u7528\u65e5\u5fd7\u8f93\u51fa\u51fd\u6570\u7684\u6e90\u6587\u4ef6\u540d<\/li>\n<li><code>%(lineno)d<\/code>\uff1a\u8c03\u7528\u65e5\u5fd7\u8f93\u51fa\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u884c\u53f7<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>logging.warning(&#39;\u8fd9\u662f\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u65e5\u5fd7\u8f93\u51fa\u4f4d\u7f6e<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>logging<\/code>\u6a21\u5757\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>filename<\/code>\u53c2\u6570\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\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>logging.info(&#39;\u8fd9\u6761\u4fe1\u606f\u5c06\u88ab\u8bb0\u5f55\u5230app.log\u6587\u4ef6\u4e2d&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1. PDB\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>PDB\u662fPython\u5185\u7f6e\u7684\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\u4e2d\u8bbe\u7f6e\u65ad\u70b9\u3001\u5355\u6b65\u6267\u884c\u3001\u67e5\u770b\u53d8\u91cf\u503c\u7b49\u3002\u4f7f\u7528PDB\u8c03\u8bd5\u5668\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165<code>pdb<\/code>\u6a21\u5757\u3002<\/li>\n<li>\u8c03\u7528<code>pdb.set_trace()<\/code>\u51fd\u6570\u8bbe\u7f6e\u65ad\u70b9\u3002<\/li>\n<li>\u8fd0\u884c\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u5c06\u5728\u65ad\u70b9\u5904\u6682\u505c\uff0c\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def add(a, b):<\/p>\n<p>    pdb.set_trace()<\/p>\n<p>    result = a + b<\/p>\n<p>    return result<\/p>\n<p>add(2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u6a21\u5f0f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>n<\/code>\uff1a\u5355\u6b65\u6267\u884c<\/li>\n<li><code>c<\/code>\uff1a\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9<\/li>\n<li><code>q<\/code>\uff1a\u9000\u51fa\u8c03\u8bd5\u5668<\/li>\n<li><code>p &lt;\u53d8\u91cf\u540d&gt;<\/code>\uff1a\u6253\u5370\u53d8\u91cf\u7684\u503c<\/li>\n<\/ul>\n<p><h4>2. IDE\u96c6\u6210\u7684\u8c03\u8bd5\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u73b0\u4ee3\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VSCode\u7b49\uff0c\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\u3001\u5355\u6b65\u6267\u884c\u3001\u67e5\u770b\u53d8\u91cf\u503c\u3001\u76d1\u89c6\u8868\u8fbe\u5f0f\u7b49\uff0c\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u65b9\u4fbf\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1. cProfile\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>cProfile<\/code>\u662fPython\u5185\u7f6e\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u7edf\u8ba1\u51fd\u6570\u7684\u8c03\u7528\u6b21\u6570\u3001\u6267\u884c\u65f6\u95f4\u7b49\u4fe1\u606f\u3002\u4f7f\u7528<code>cProfile<\/code>\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>cProfile<\/code>\u6a21\u5757\u3002<\/li>\n<li>\u8c03\u7528<code>cProfile.run()<\/code>\u51fd\u6570\u8fd0\u884c\u5f85\u5206\u6790\u7684\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>def my_function():<\/p>\n<p>    total = 0<\/p>\n<p>    for i in range(10000):<\/p>\n<p>        total += i<\/p>\n<p>    return total<\/p>\n<p>cProfile.run(&#39;my_function()&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. line_profiler\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>line_profiler<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u7edf\u8ba1\u6bcf\u884c\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002\u4f7f\u7528<code>line_profiler<\/code>\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5<code>line_profiler<\/code>\u6a21\u5757\uff1a<code>pip install line_profiler<\/code><\/li>\n<li>\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>@profile<\/code>\u88c5\u9970\u5668\u6807\u8bb0\u5f85\u5206\u6790\u7684\u51fd\u6570\u3002<\/li>\n<li>\u4f7f\u7528<code>kernprof<\/code>\u547d\u4ee4\u8fd0\u884c\u4ee3\u7801\uff1a<code>kernprof -l -v my_script.py<\/code><\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@profile<\/p>\n<p>def my_function():<\/p>\n<p>    total = 0<\/p>\n<p>    for i in range(10000):<\/p>\n<p>        total += i<\/p>\n<p>    return total<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u547d\u4ee4\uff1a<code>kernprof -l -v my_script.py<\/code><\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90<\/h3>\n<\/p>\n<p><h4>1. psutil\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>psutil<\/code>\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u7cfb\u7edf\u76d1\u63a7\u6a21\u5757\uff0c\u53ef\u4ee5\u83b7\u53d6\u7cfb\u7edf\u7684CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u7b49\u8d44\u6e90\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u4f7f\u7528<code>psutil<\/code>\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5<code>psutil<\/code>\u6a21\u5757\uff1a<code>pip install psutil<\/code><\/li>\n<li>\u5bfc\u5165<code>psutil<\/code>\u6a21\u5757\uff0c\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u83b7\u53d6\u7cfb\u7edf\u8d44\u6e90\u7684\u4f7f\u7528\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<h2><strong>\u83b7\u53d6CPU\u4f7f\u7528\u7387<\/strong><\/h2>\n<p>cpu_usage = psutil.cpu_percent(interval=1)<\/p>\n<p>print(f&quot;CPU\u4f7f\u7528\u7387\uff1a{cpu_usage}%&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/strong><\/h2>\n<p>memory_info = psutil.virtual_memory()<\/p>\n<p>print(f&quot;\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a{memory_info}&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5<\/strong><\/h2>\n<p>disk_usage = psutil.disk_usage(&#39;\/&#39;)<\/p>\n<p>print(f&quot;\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\uff1a{disk_usage}&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u7edc\u4f7f\u7528\u60c5\u51b5<\/strong><\/h2>\n<p>net_info = psutil.net_io_counters()<\/p>\n<p>print(f&quot;\u7f51\u7edc\u4f7f\u7528\u60c5\u51b5\uff1a{net_info}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5916\u90e8\u76d1\u63a7\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>psutil<\/code>\u6a21\u5757\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5916\u90e8\u76d1\u63a7\u5de5\u5177\u5982<code>top<\/code>\u3001<code>htop<\/code>\u3001<code>nmon<\/code>\u7b49\uff0c\u5b9e\u65f6\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u63d0\u4f9b\u66f4\u4e3a\u8be6\u7ec6\u7684\u7cfb\u7edf\u8d44\u6e90\u4fe1\u606f\uff0c\u5e76\u652f\u6301\u56fe\u5f62\u5316\u754c\u9762\uff0c\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u8fdc\u7a0b\u76d1\u63a7\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528Prometheus\u548cGrafana<\/h4>\n<\/p>\n<p><p>Prometheus\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u7cfb\u7edf\u76d1\u63a7\u548c\u62a5\u8b66\u5de5\u5177\uff0c\u53ef\u4ee5\u91c7\u96c6\u3001\u5b58\u50a8\u548c\u67e5\u8be2\u7cfb\u7edf\u7684\u5404\u79cd\u5ea6\u91cf\u6570\u636e\u3002Grafana\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Prometheus\u91c7\u96c6\u5230\u7684\u6570\u636e\u5c55\u793a\u4e3a\u56fe\u8868\u3002\u4f7f\u7528Prometheus\u548cGrafana\u76d1\u63a7Python\u5e94\u7528\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Prometheus\u548cGrafana\u3002<\/li>\n<li>\u914d\u7f6ePrometheus\u91c7\u96c6Python\u5e94\u7528\u7684\u5ea6\u91cf\u6570\u636e\u3002<\/li>\n<li>\u4f7f\u7528Grafana\u521b\u5efa\u4eea\u8868\u76d8\uff0c\u5c55\u793aPrometheus\u91c7\u96c6\u5230\u7684\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><h4>2. \u4f7f\u7528New Relic<\/h4>\n<\/p>\n<p><p>New Relic\u662f\u4e00\u4e2a\u5546\u4e1a\u7684\u5e94\u7528\u6027\u80fd\u7ba1\u7406\uff08APM\uff09\u5de5\u5177\uff0c\u53ef\u4ee5\u76d1\u63a7\u5e94\u7528\u7684\u6027\u80fd\u3001\u9519\u8bef\u3001\u4ea4\u6613\u7b49\u4fe1\u606f\u3002\u4f7f\u7528New Relic\u76d1\u63a7Python\u5e94\u7528\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6ce8\u518cNew Relic\u8d26\u53f7\u3002<\/li>\n<li>\u5b89\u88c5New Relic Python\u4ee3\u7406\uff1a<code>pip install newrelic<\/code><\/li>\n<li>\u914d\u7f6eNew Relic Python\u4ee3\u7406\u3002<\/li>\n<li>\u542f\u52a8\u5e94\u7528\uff0cNew Relic\u5c06\u81ea\u52a8\u91c7\u96c6\u5e94\u7528\u7684\u6027\u80fd\u6570\u636e\uff0c\u5e76\u5c55\u793a\u5728New Relic\u4eea\u8868\u76d8\u4e0a\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u5206\u5e03\u5f0f\u8ddf\u8e2a<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528Jaeger<\/h4>\n<\/p>\n<p><p>Jaeger\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5206\u5e03\u5f0f\u8ddf\u8e2a\u7cfb\u7edf\uff0c\u53ef\u4ee5\u8ddf\u8e2a\u5e94\u7528\u7684\u8bf7\u6c42\uff0c\u5206\u6790\u8bf7\u6c42\u7684\u5ef6\u8fdf\u3001\u9519\u8bef\u7b49\u4fe1\u606f\u3002\u4f7f\u7528Jaeger\u76d1\u63a7Python\u5e94\u7528\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Jaeger\u5ba2\u6237\u7aef\uff1a<code>pip install jaeger-client<\/code><\/li>\n<li>\u5728\u4ee3\u7801\u4e2d\u914d\u7f6eJaeger\u5ba2\u6237\u7aef\uff0c\u8bb0\u5f55\u8bf7\u6c42\u7684\u8ddf\u8e2a\u4fe1\u606f\u3002<\/li>\n<li>\u4f7f\u7528Jaeger UI\u67e5\u770b\u8bf7\u6c42\u7684\u8ddf\u8e2a\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from jaeger_client import Config<\/p>\n<p>def init_jaeger_tracer(service_name=&#39;my_service&#39;):<\/p>\n<p>    config = Config(<\/p>\n<p>        config={<\/p>\n<p>            &#39;sampler&#39;: {&#39;type&#39;: &#39;const&#39;, &#39;param&#39;: 1},<\/p>\n<p>            &#39;logging&#39;: True,<\/p>\n<p>        },<\/p>\n<p>        service_name=service_name,<\/p>\n<p>    )<\/p>\n<p>    return config.initialize_tracer()<\/p>\n<p>tracer = init_jaeger_tracer()<\/p>\n<p>with tracer.start_span(&#39;my_operation&#39;) as span:<\/p>\n<p>    span.log_kv({&#39;event&#39;: &#39;my_event&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Zipkin<\/h4>\n<\/p>\n<p><p>Zipkin\u662f\u53e6\u4e00\u4e2a\u5f00\u6e90\u7684\u5206\u5e03\u5f0f\u8ddf\u8e2a\u7cfb\u7edf\uff0c\u529f\u80fd\u7c7b\u4f3c\u4e8eJaeger\u3002\u4f7f\u7528Zipkin\u76d1\u63a7Python\u5e94\u7528\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Zipkin\u5ba2\u6237\u7aef\uff1a<code>pip install py_zipkin<\/code><\/li>\n<li>\u5728\u4ee3\u7801\u4e2d\u914d\u7f6eZipkin\u5ba2\u6237\u7aef\uff0c\u8bb0\u5f55\u8bf7\u6c42\u7684\u8ddf\u8e2a\u4fe1\u606f\u3002<\/li>\n<li>\u4f7f\u7528Zipkin UI\u67e5\u770b\u8bf7\u6c42\u7684\u8ddf\u8e2a\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from py_zipkin.zipkin import zipkin_span, create_http_headers_for_new_span<\/p>\n<p>def http_transport(encoded_span):<\/p>\n<p>    # \u5c06\u7f16\u7801\u540e\u7684span\u53d1\u9001\u5230Zipkin\u670d\u52a1\u5668<\/p>\n<p>    pass<\/p>\n<p>with zipkin_span(<\/p>\n<p>    service_name=&#39;my_service&#39;,<\/p>\n<p>    span_name=&#39;my_operation&#39;,<\/p>\n<p>    transport_handler=http_transport,<\/p>\n<p>    sample_rate=100,<\/p>\n<p>):<\/p>\n<p>    # \u8bb0\u5f55\u8bf7\u6c42\u7684\u8ddf\u8e2a\u4fe1\u606f<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5206\u5e03\u5f0f\u65e5\u5fd7\u7cfb\u7edf<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528ELK Stack<\/h4>\n<\/p>\n<p><p>ELK Stack\u7531Elasticsearch\u3001Logstash\u548cKibana\u7ec4\u6210\uff0c\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5206\u5e03\u5f0f\u65e5\u5fd7\u7cfb\u7edf\u3002\u4f7f\u7528ELK Stack\u76d1\u63a7Python\u5e94\u7528\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Elasticsearch\u3001Logstash\u548cKibana\u3002<\/li>\n<li>\u914d\u7f6eLogstash\u91c7\u96c6Python\u5e94\u7528\u7684\u65e5\u5fd7\u3002<\/li>\n<li>\u4f7f\u7528Kibana\u521b\u5efa\u4eea\u8868\u76d8\uff0c\u5c55\u793aElasticsearch\u5b58\u50a8\u7684\u65e5\u5fd7\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><h4>2. \u4f7f\u7528Fluentd\u548cElasticsearch<\/h4>\n<\/p>\n<p><p>Fluentd\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6570\u636e\u6536\u96c6\u5668\uff0c\u53ef\u4ee5\u91c7\u96c6\u3001\u8f6c\u6362\u548c\u5b58\u50a8\u65e5\u5fd7\u6570\u636e\u3002\u5c06Fluentd\u4e0eElasticsearch\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u5206\u5e03\u5f0f\u65e5\u5fd7\u7cfb\u7edf\u3002\u4f7f\u7528Fluentd\u548cElasticsearch\u76d1\u63a7Python\u5e94\u7528\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Fluentd\u548cElasticsearch\u3002<\/li>\n<li>\u914d\u7f6eFluentd\u91c7\u96c6Python\u5e94\u7528\u7684\u65e5\u5fd7\uff0c\u5e76\u5c06\u65e5\u5fd7\u53d1\u9001\u5230Elasticsearch\u3002<\/li>\n<li>\u4f7f\u7528Kibana\u521b\u5efa\u4eea\u8868\u76d8\uff0c\u5c55\u793aElasticsearch\u5b58\u50a8\u7684\u65e5\u5fd7\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u67e5\u770bPython\u8fd0\u884c\u72b6\u6001\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u4f7f\u7528<code>print<\/code>\u8bed\u53e5\u548c\u65e5\u5fd7\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u9636\u6bb5\u63d0\u4f9b\u5373\u65f6\u7684\u53cd\u9988\u4fe1\u606f\uff1b\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u53ef\u4ee5\u5728\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u67e5\u770b\u53d8\u91cf\u7684\u503c\u548c\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\uff1b\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f18\u5316\u4ee3\u7801\u6027\u80fd\uff1b\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90\u548c\u4f7f\u7528\u8fdc\u7a0b\u76d1\u63a7\u5de5\u5177\u53ef\u4ee5\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u5b9e\u65f6\u76d1\u63a7\u5e94\u7528\u7684\u8fd0\u884c\u72b6\u6001\uff1b\u4f7f\u7528\u5206\u5e03\u5f0f\u8ddf\u8e2a\u548c\u5206\u5e03\u5f0f\u65e5\u5fd7\u7cfb\u7edf\u53ef\u4ee5\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u8ddf\u8e2a\u548c\u5206\u6790\u8bf7\u6c42\u7684\u8def\u5f84\u548c\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5168\u9762\u4e86\u89e3Python\u5e94\u7528\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u95ee\u9898\uff0c\u786e\u4fdd\u5e94\u7528\u7684\u7a33\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u76d1\u63a7Python\u7a0b\u5e8f\u7684\u8fd0\u884c\u6027\u80fd\uff1f<\/strong><br \/>\u76d1\u63a7Python\u7a0b\u5e8f\u7684\u6027\u80fd\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\u5b9e\u73b0\u3002\u5e38\u7528\u7684\u6709\u4f7f\u7528\u5185\u7f6e\u7684<code>time<\/code>\u6a21\u5757\u6765\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u7684\u65f6\u95f4\uff0c\u5229\u7528<code>cProfile<\/code>\u6a21\u5757\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>memory_profiler<\/code>\u6765\u76d1\u63a7\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u6b64\u5916\uff0c\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u4e5f\u63d0\u4f9b\u4e86\u6027\u80fd\u76d1\u63a7\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3\u4ee3\u7801\u7684\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u901a\u5e38\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u5b9e\u73b0\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\uff0c\u4ee5\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3001\u8bb0\u5f55\u65e5\u5fd7\u6216\u6267\u884c\u7279\u5b9a\u7684\u6062\u590d\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n<p><strong>Python\u7a0b\u5e8f\u8fd0\u884c\u7f13\u6162\u7684\u5e38\u89c1\u539f\u56e0\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>Python\u7a0b\u5e8f\u8fd0\u884c\u7f13\u6162\u53ef\u80fd\u7531\u591a\u79cd\u56e0\u7d20\u5f15\u8d77\uff0c\u4f8b\u5982\u7b97\u6cd5\u6548\u7387\u4f4e\u4e0b\u3001\u5185\u5b58\u6cc4\u6f0f\u3001\u8fc7\u591a\u7684I\/O\u64cd\u4f5c\u3001\u4f7f\u7528\u4e86\u4e0d\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u7b49\u3002\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\u5982<code>cProfile<\/code>\u6216<code>line_profiler<\/code>\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u6027\u80fd\u74f6\u9888\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5206\u6790\u7ed3\u679c\u4f18\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6574\u4f53\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u67e5\u770bPython\u8fd0\u884c\u72b6\u6001\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u5982\u4f7f\u7528print\u8bed\u53e5\u3001\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u3001\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5 [&hellip;]","protected":false},"author":3,"featured_media":1178876,"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\/1178869"}],"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=1178869"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178869\/revisions"}],"predecessor-version":[{"id":1178878,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178869\/revisions\/1178878"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178876"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}