{"id":763,"date":"2025-01-05T22:47:48","date_gmt":"2025-01-05T14:47:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=763"},"modified":"2025-01-11T16:09:21","modified_gmt":"2025-01-11T08:09:21","slug":"%e8%b6%85%e7%ba%a7%e5%a5%bd%e7%94%a8%e7%9a%84%e6%8f%90%e5%8d%87python%e6%95%88%e7%8e%87%e7%9a%84%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/05\/%e8%b6%85%e7%ba%a7%e5%a5%bd%e7%94%a8%e7%9a%84%e6%8f%90%e5%8d%87python%e6%95%88%e7%8e%87%e7%9a%84%e5%b7%a5%e5%85%b7\/","title":{"rendered":"\u8d85\u5b9e\u7528 Python \u5de5\u5177\u63a8\u8350\uff1a\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u7684\u6700\u4f73\u9009\u62e9"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u63d0\u5347 Python \u5f00\u53d1\u6548\u7387\u7684\u5de5\u5177\u6709\u5f88\u591a\uff0c\u6db5\u76d6\u4e86\u4ece\u4ee3\u7801\u7f16\u5199\u3001\u8c03\u8bd5\u3001\u6d4b\u8bd5\u3001\u4f18\u5316\u5230\u90e8\u7f72\u7b49\u5404\u4e2a\u65b9\u9762\u3002\u4e0b\u9762\u5c06\u4ece\u591a\u4e2a\u7ef4\u5ea6\u4e3a\u4f60\u63a8\u8350\u4e00\u4e9b\u8d85\u7ea7\u597d\u7528\u7684\u5de5\u5177\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4ee3\u7801\u7f16\u5199\u4e0e\u8c03\u8bd5\u5de5\u5177<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <strong>IPython &amp; Jupyter Notebook<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IPython<\/strong> \u662f Python \u7684\u4ea4\u4e92\u5f0f shell\uff0c\u5177\u6709\u6bd4\u6807\u51c6 Python shell \u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5982\u81ea\u52a8\u8865\u5168\u3001\u52a8\u6001\u8f93\u5165\u8f93\u51fa\u3001\u5386\u53f2\u8bb0\u5f55\u7b49\u3002<\/li>\n\n\n\n<li><strong>Jupyter Notebook<\/strong> \u662f\u4e00\u4e2a\u57fa\u4e8e Web \u7684\u5e94\u7528\uff0c\u53ef\u4ee5\u521b\u5efa\u548c\u5171\u4eab\u5305\u542b\u4ee3\u7801\u3001\u6587\u672c\u3001\u56fe\u8868\u7b49\u5185\u5bb9\u7684\u6587\u6863\u3002\u975e\u5e38\u9002\u5408\u6570\u636e\u79d1\u5b66\u3001\u673a\u5668\u5b66\u4e60\u548c\u5b9e\u9a8c\u6027\u9879\u76ee\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install ipython jupyter<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ipython  # \u542f\u52a8 IPython\njupyter notebook  # \u542f\u52a8 Jupyter Notebook<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 <strong>Visual Studio Code (VSCode)<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">VSCode \u662f\u4e00\u6b3e\u8f7b\u91cf\u7ea7\u3001\u529f\u80fd\u5f3a\u5927\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301 Python \u5f00\u53d1\u3002\u901a\u8fc7\u6269\u5c55\u63d2\u4ef6\uff0c\u652f\u6301\u8c03\u8bd5\u3001\u4ee3\u7801\u8865\u5168\u3001Linting\u3001\u7248\u672c\u63a7\u5236\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Python \u63d2\u4ef6<\/strong>\uff1a\u63d0\u4f9b\u4ee3\u7801\u8865\u5168\u3001Linting\uff08\u8bed\u6cd5\u68c0\u67e5\uff09\u3001\u8c03\u8bd5\u7b49\u529f\u80fd\u3002<\/li>\n\n\n\n<li><strong>Pylance<\/strong>\uff1a\u9ad8\u6548\u7684 Python \u7c7b\u578b\u68c0\u67e5\u548c\u5206\u6790\u5de5\u5177\uff0c\u63d0\u5347\u4ee3\u7801\u5206\u6790\u901f\u5ea6\u548c\u51c6\u786e\u6027\u3002<\/li>\n\n\n\n<li><strong>Live Share<\/strong>\uff1a\u652f\u6301\u591a\u4eba\u5b9e\u65f6\u534f\u4f5c\u7f16\u7a0b\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b89\u88c5 VSCode <a href=\"https:\/\/code.visualstudio.com\/\">\u5b98\u7f51<\/a><\/li>\n\n\n\n<li>\u5b89\u88c5 Python \u63d2\u4ef6\uff1a\u5728 VSCode \u7684\u6269\u5c55\u9762\u677f\u641c\u7d22 <code>Python<\/code>\uff0c\u5b89\u88c5\u7531 Microsoft \u63d0\u4f9b\u7684\u63d2\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.3 <strong>PyCharm<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">PyCharm \u662f JetBrains \u63d0\u4f9b\u7684\u4e00\u6b3e\u5f3a\u5927\u7684 Python IDE\uff0c\u9002\u5408\u5927\u578b\u9879\u76ee\u7684\u5f00\u53d1\uff0c\u652f\u6301 Python \u4ee3\u7801\u8865\u5168\u3001\u8c03\u8bd5\u3001\u6d4b\u8bd5\u3001\u91cd\u6784\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63d0\u4f9b\u4e13\u4e1a\u7248\u548c\u793e\u533a\u7248\uff08\u514d\u8d39\uff09\uff0c\u793e\u533a\u7248\u5df2\u7ecf\u652f\u6301\u57fa\u672c\u7684 Python \u5f00\u53d1\u529f\u80fd\u3002<\/li>\n\n\n\n<li>\u652f\u6301\u865a\u62df\u73af\u5883\u548c Docker \u96c6\u6210\uff0c\u4fbf\u4e8e\u7ba1\u7406\u4e0d\u540c\u7684 Python \u7248\u672c\u548c\u4f9d\u8d56\u5e93\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5<\/strong>\uff1a<a href=\"https:\/\/www.jetbrains.com\/pycharm\/\">PyCharm \u5b98\u7f51<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u4e0e\u6027\u80fd<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>Black<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Black<\/strong> \u662f\u4e00\u4e2a\u81ea\u52a8\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u5b83\u901a\u8fc7\u7edf\u4e00\u7684\u683c\u5f0f\u98ce\u683c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n\n\n\n<li>\u5b83\u4f1a\u81ea\u52a8\u683c\u5f0f\u5316\u4ee3\u7801\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u6574\u7f29\u8fdb\u3001\u7a7a\u683c\u7b49\u3002\u53ea\u9700\u8981\u8fd0\u884c\u4e00\u6b21\uff0c\u5b83\u5c31\u4f1a\u5bf9\u4ee3\u7801\u8fdb\u884c\u6807\u51c6\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install black<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>black your_script.py  # \u81ea\u52a8\u683c\u5f0f\u5316<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>Flake8<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Flake8<\/strong> \u662f\u4e00\u4e2a Python \u4ee3\u7801\u98ce\u683c\u68c0\u67e5\u5de5\u5177\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u68c0\u6d4b\u4ee3\u7801\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\u3001\u98ce\u683c\u4e0d\u7b26\u5408\u89c4\u8303\u7684\u5730\u65b9\u3002<\/li>\n\n\n\n<li>\u5b83\u53ef\u4ee5\u4e0e VSCode \u7b49\u7f16\u8f91\u5668\u96c6\u6210\uff0c\u63d0\u4f9b\u5b9e\u65f6\u53cd\u9988\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install flake8<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flake8 your_script.py  # \u68c0\u67e5\u4ee3\u7801\u98ce\u683c\u95ee\u9898<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong>mypy<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>mypy<\/strong> \u662f\u4e00\u4e2a\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff0c\u7528\u4e8e\u68c0\u67e5 Python \u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u95ee\u9898\u3002<\/li>\n\n\n\n<li>\u5b83\u652f\u6301 Python 3 \u7684\u7c7b\u578b\u6ce8\u89e3\uff0c\u5e76\u80fd\u591f\u53d1\u73b0\u7c7b\u578b\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install mypy<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mypy your_script.py  # \u68c0\u67e5\u7c7b\u578b\u9519\u8bef<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u9879\u76ee\u7ba1\u7406\u4e0e\u81ea\u52a8\u5316<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>Poetry<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Poetry<\/strong> \u662f\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684 Python \u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u7b80\u5316\u4e86\u5305\u7684\u5b89\u88c5\u3001\u4f9d\u8d56\u7ba1\u7406\u4ee5\u53ca\u53d1\u5e03\u8fc7\u7a0b\u3002<\/li>\n\n\n\n<li>\u5b83\u4f7f\u7528 <code>pyproject.toml<\/code> \u6587\u4ef6\u6765\u4ee3\u66ff\u4f20\u7edf\u7684 <code>setup.py<\/code> \u548c <code>requirements.txt<\/code>\uff0c\u63d0\u4f9b\u4e00\u81f4\u6027\u548c\u53ef\u7ba1\u7406\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install poetry<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>poetry new myproject  # \u521b\u5efa\u65b0\u9879\u76ee\npoetry add requests  # \u5b89\u88c5\u4f9d\u8d56\npoetry install  # \u5b89\u88c5\u9879\u76ee\u4f9d\u8d56<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong>pipenv<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>pipenv<\/strong> \u662f\u53e6\u4e00\u4e2a Python \u5305\u7ba1\u7406\u5de5\u5177\uff0c\u4e13\u6ce8\u4e8e\u7b80\u5316\u865a\u62df\u73af\u5883\u7684\u7ba1\u7406\u548c\u4f9d\u8d56\u7684\u9501\u5b9a\u3002<\/li>\n\n\n\n<li>\u5b83\u4f7f\u7528 <code>Pipfile<\/code> \u548c <code>Pipfile.lock<\/code> \u6587\u4ef6\u6765\u7ba1\u7406\u4f9d\u8d56\uff0c\u907f\u514d\u4e86 <code>requirements.txt<\/code> \u7684\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pipenv<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pipenv install  # \u5b89\u88c5\u4f9d\u8d56\npipenv shell  # \u542f\u52a8\u865a\u62df\u73af\u5883<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e0e\u8c03\u8bd5<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <strong>pytest<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>pytest<\/strong> \u662f Python \u4e2d\u6700\u6d41\u884c\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u5b83\u652f\u6301\u5355\u5143\u6d4b\u8bd5\u3001\u96c6\u6210\u6d4b\u8bd5\u548c\u529f\u80fd\u6d4b\u8bd5\u7b49\u3002<\/li>\n\n\n\n<li>\u8bed\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u80fd\u591f\u81ea\u52a8\u53d1\u73b0\u6d4b\u8bd5\u7528\u4f8b\u5e76\u751f\u6210\u6d4b\u8bd5\u62a5\u544a\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pytest<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pytest your_test.py  # \u8fd0\u884c\u6d4b\u8bd5<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <strong>pdb<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>pdb<\/strong> \u662f Python \u5185\u5efa\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u8bbe\u7f6e\u65ad\u70b9\uff0c\u68c0\u67e5\u53d8\u91cf\u72b6\u6001\u7b49\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u8c03\u8bd5\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pdb\npdb.set_trace()  # \u5728\u6b64\u5904\u6682\u505c\u7a0b\u5e8f\uff0c\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.3 <strong>unittest<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u81ea\u5e26\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u8fdb\u884c\u6a21\u5757\u7ea7\u522b\u7684\u6d4b\u8bd5\u3002\u867d\u7136\u529f\u80fd\u4e0d\u5982 pytest \u4e30\u5bcc\uff0c\u4f46\u5b83\u9002\u5408\u57fa\u7840\u6d4b\u8bd5\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m unittest discover  # \u53d1\u73b0\u5e76\u8fd0\u884c\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u6570\u636e\u5206\u6790\u4e0e\u53ef\u89c6\u5316<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">5.1 <strong>pandas<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>pandas<\/strong> \u662f Python \u4e2d\u6700\u6d41\u884c\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982 DataFrame\uff09\u6765\u5904\u7406\u8868\u683c\u578b\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pandas<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.2 <strong>matplotlib &amp; seaborn<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>matplotlib<\/strong> \u662f Python \u4e2d\u6700\u57fa\u7840\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u7ed8\u5236\u5404\u79cd 2D \u56fe\u8868\u3002<\/li>\n\n\n\n<li><strong>seaborn<\/strong> \u662f\u57fa\u4e8e matplotlib \u7684\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u66f4\u591a\u7684\u56fe\u8868\u7c7b\u578b\u548c\u6837\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install matplotlib seaborn<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import matplotlib.pyplot as plt\nimport seaborn as sns\n\nsns.set(style=\"darkgrid\")\ndata = &#91;1, 2, 3, 4, 5]\nplt.plot(data)\nplt.show()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u6027\u80fd\u4f18\u5316\u4e0e\u5e76\u53d1\u5904\u7406<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">6.1 <strong>cProfile<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>cProfile<\/strong> \u662f Python \u7684\u5185\u7f6e\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u627e\u5230\u7a0b\u5e8f\u4e2d\u7684\u74f6\u9888\u90e8\u5206\u3002<\/li>\n\n\n\n<li>\u5b83\u751f\u6210\u4e00\u4e2a\u6027\u80fd\u62a5\u544a\uff0c\u5e2e\u52a9\u4f60\u5206\u6790\u54ea\u4e9b\u51fd\u6570\u6267\u884c\u65f6\u95f4\u6700\u957f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m cProfile your_script.py<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.2 <strong>asyncio<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>asyncio<\/strong> \u662f Python 3.4 \u5f15\u5165\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u652f\u6301\u5f02\u6b65 I\/O \u7684\u534f\u7a0b\u673a\u5236\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\n\nasync def say_hello():\n    print('Hello')\n\nasync def main():\n    await say_hello()\n\nasyncio.run(main())  # \u8fd0\u884c\u5f02\u6b65\u4efb\u52a1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.3 <strong>multiprocessing<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>multiprocessing<\/strong> \u662f Python \u7684\u5185\u5efa\u5e93\uff0c\u9002\u7528\u4e8e\u591a\u6838 CPU \u7684\u5e76\u884c\u5904\u7406\u3002\u5b83\u901a\u8fc7\u591a\u8fdb\u7a0b\u6a21\u578b\u6765\u5b9e\u73b0\u5e76\u53d1\u8ba1\u7b97\uff0c\u907f\u514d\u4e86 GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09\u7684\u9650\u5236\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from multiprocessing import Pool\n\ndef f(x):\n    return x * x\n\nif __name__ == \"__main__\":\n    with Pool(4) as p:\n        print(p.map(f, &#91;1, 2, 3, 4]))  # \u901a\u8fc7\u591a\u4e2a\u8fdb\u7a0b\u5e76\u884c\u6267\u884c\u51fd\u6570<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u5347 Python \u5f00\u53d1\u6548\u7387\u3002\u4ece\u4ee3\u7801\u7f16\u5199\u5230\u8c03\u8bd5\uff0c\u518d\u5230\u6027\u80fd\u4f18\u5316\uff0c\u6bcf\u4e2a\u73af\u8282\u90fd\u6709\u4e00\u7cfb\u5217\u4f18\u79c0\u7684\u5de5\u5177\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff0c\u51cf\u5c11\u9519\u8bef\uff0c\u63d0\u5347\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347 Python \u5f00\u53d1\u6548\u7387\u7684\u5de5\u5177\u6709\u5f88\u591a\uff0c\u6db5\u76d6\u4e86\u4ece\u4ee3\u7801\u7f16\u5199\u3001\u8c03\u8bd5\u3001\u6d4b\u8bd5\u3001\u4f18\u5316\u5230\u90e8\u7f72\u7b49\u5404\u4e2a\u65b9\u9762\u3002\u4e0b\u9762\u5c06\u4ece\u591a\u4e2a\u7ef4\u5ea6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-763","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=763"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/763\/revisions"}],"predecessor-version":[{"id":893,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/763\/revisions\/893"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}