{"id":6719,"date":"2018-09-12T11:09:04","date_gmt":"2018-09-12T03:09:04","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6719"},"modified":"2018-09-12T11:09:04","modified_gmt":"2018-09-12T03:09:04","slug":"python%e7%a8%8b%e5%ba%8f%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90%e5%b7%a5%e5%85%b7py-spy","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6719.html","title":{"rendered":"python\u7a0b\u5e8f\u6027\u80fd\u5206\u6790\u5de5\u5177py-spy"},"content":{"rendered":"<p>\u4e4b\u524d\u6709\u4ecb\u7ecd\u8fc7 Uber \u5f00\u53d1\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177 pyflame <a href=\"https:\/\/kyle.ai\/blog\/6347.html\">\u300a\u7528pyflame\u6765\u505apython\u7a0b\u5e8f\u7684\u6027\u80fd\u5206\u6790\u300b<\/a>\uff0c\u6700\u8fd1\u53c8\u53d1\u73b0\u4e00\u4e2a\u66f4\u597d\u7528\u7684\uff0chttps:\/\/github.com\/benfred\/py-spy<\/p>\n<p>pyflame\u662f\u7528c++\u7f16\u5199\uff0c\u800cpy-spy\u662f\u7528rust\u8bed\u8a00\u5199\u7684\uff0c\u90fd\u662f\u7f16\u8bd1\u578b\u8bed\u8a00\uff0c\u4e0d\u8fc7pyflame\u7684\u7f3a\u70b9\u662f\u73b0\u5728\u8fd8\u4e0d\u652f\u6301python 3.7\u7248\u672c\uff0c\u4ee5\u53ca\u53ea\u80fd\u5728linux\u73af\u5883\u4e0a\u8fd0\u884c\u3002<\/p>\n<p>\u800cpy-spy\u89e3\u51b3\u4e86pyflame\u7684\u95ee\u9898\uff0c\u5177\u4f53\u53ef\u4ee5\u770b\u5b83\u7684github\u4e3b\u9875\u8bf4\u660e\u3002<\/p>\n<p>\u5b89\u88c5\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u76f4\u63a5pip\u6e90\u5b89\u88c5\u5c31\u597d\u4e86\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install py-spy\r\n<\/pre>\n<p>\u4f7f\u7528\u65b9\u6cd5\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ py-spy  --help\r\npy-spy 0.1.5\r\nA sampling profiler for Python programs\r\n\r\nUSAGE:\r\n    py-spy &#x5B;FLAGS] &#x5B;OPTIONS] --pid &lt;pid&gt; &#x5B;python_program]...\r\n\r\nFLAGS:\r\n        --dump        Dump the current stack traces to stdout\r\n    -F, --function    Aggregate samples by function name instead of by line number\r\n    -h, --help        Prints help information\r\n    -V, --version     Prints version information\r\n\r\nOPTIONS:\r\n    -d, --duration &lt;duration&gt;    The number of seconds to sample for when generating a flame graph &#x5B;default: 2]\r\n    -f, --flame &lt;flamefile&gt;      Generate a flame graph and write to a file\r\n    -p, --pid &lt;pid&gt;              PID of a running python program to spy on\r\n    -r, --rate &lt;rate&gt;            The number of samples to collect per second &#x5B;default: 1000]\r\n\r\nARGS:\r\n    &lt;python_program&gt;...    commandline of a python program to run\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/09\/\u4f01\u4e1a\u5fae\u4fe1\u622a\u56fe_15367196397923.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6720\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/09\/\u4f01\u4e1a\u5fae\u4fe1\u622a\u56fe_15367196397923.png\" alt=\"\" width=\"803\" height=\"873\" \/><\/a><\/p>\n<p>\u7a0b\u5e8f\u53ef\u4ee5\u76f4\u63a5\u50cftop\u547d\u4ee4\u90a3\u6837\u4e0d\u65ad\u8f93\u51fa\uff0c\u4e5f\u53ef\u4ee5\u8fd0\u884c\u4e00\u65ad\u65f6\u95f4\u540e\u751f\u6210SVG\u7248\u672c\u7684\u706b\u7130\u56fe\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npy-spy --flame flame.svg -F -p 10650 -d 10\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/09\/\u4f01\u4e1a\u5fae\u4fe1\u622a\u56fe_15367214796019.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6721\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/09\/\u4f01\u4e1a\u5fae\u4fe1\u622a\u56fe_15367214796019.png\" alt=\"\" width=\"1197\" height=\"475\" \/><\/a><\/p>\n<p>\u8ddfpyflame\u76f8\u6bd4\uff0cpyflame\u751f\u6210\u7684\u662f\u706b\u7130\u56fe\u539f\u59cb\u6570\u636e\u6587\u4ef6\uff0c\u5177\u4f53svg\u6587\u4ef6\u8fd8\u9700\u8981\u81ea\u5df1\u518d\u624b\u52a8\u8c03\u7528\u7a0b\u5e8f\u751f\u6210\uff0cpy-spy\u5219\u662f\u4e00\u6b21\u6027\u5230\u4f4d\u76f4\u63a5\u8f93\u51fa svg\u56fe\u7247\uff0csvg\u6587\u4ef6\u53ef\u4ee5\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u4e0a\u6253\u5f00\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0cpy-spy\u8c03\u8bd5\u7684\u662f\u7a0b\u5e8f\u4f7f\u7528\u65f6\u95f4\u7684\u7edf\u8ba1\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u89c2\u5bdf\u54ea\u4e2a\u51fd\u6570\u6267\u884c\u5360\u7528\u7684\u65f6\u95f4\u957f\uff0c\u4f46\u5c31\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u597d\u50cf\u5c31\u4e0d\u80fd\u4f53\u73b0\uff0c\u6bd4\u5982\u60f3\u8981\u627e\u5185\u5b58\u6cc4\u6f0f\u7684\u6839\u6e90\uff0c\u8fd9\u4e2a\u5de5\u5177\u5c31\u7231\u83ab\u80fd\u52a9\u4e86\u3002<\/p>\n<p>\u5185\u5b58\u6cc4\u6f0f\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528 guppy\/heapy \uff0c\u4e4b\u524d\u4e5f\u6709\u53d1\u6587\u7ae0<a href=\"https:\/\/kyle.ai\/blog\/6077.html\">\u300apython\u5185\u5b58\u6cc4\u6f0f\u5206\u6790\u5de5\u5177\uff1aguppy\/heapy\u300b<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u6709\u4ecb\u7ecd\u8fc7 Uber \u5f00\u53d1\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177 pyflame \u300a\u7528pyflame\u6765\u505apython\u7a0b\u5e8f\u7684\u6027\u80fd\u5206\u6790\u300b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6719","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6719"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6719\/revisions"}],"predecessor-version":[{"id":6722,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6719\/revisions\/6722"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}