{"id":959918,"date":"2024-12-27T03:45:07","date_gmt":"2024-12-26T19:45:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959918.html"},"modified":"2024-12-27T03:45:09","modified_gmt":"2024-12-26T19:45:09","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85parmap","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959918.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b89\u88c5parmap"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102610\/8257b556-7cb6-48a0-b7ca-7cc2d49fc6b2.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b89\u88c5parmap\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5b89\u88c5parmap\u7684\u6b65\u9aa4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\uff1a\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3001\u89e3\u51b3\u53ef\u80fd\u7684\u4f9d\u8d56\u95ee\u9898\u3001\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u662f\u6700\u5e38\u7528\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u5185\u5bb9\u5c06\u8be6\u7ec6\u63cf\u8ff0\u6bcf\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PIP\u8fdb\u884c\u5b89\u88c5<\/p>\n<\/p>\n<p><p>PIP\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002\u8981\u5b89\u88c5parmap\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86PIP\u3002\u5927\u591a\u6570Python\u53d1\u884c\u7248\u90fd\u81ea\u5e26PIP\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff08Windows\u7528\u6237\u53ef\u4ee5\u4f7f\u7528cmd\uff0cmacOS\u548cLinux\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u7ec8\u7aef\uff09\u3002<\/li>\n<li>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u786e\u4fddPIP\u662f\u6700\u65b0\u7248\u672c\uff1a\n<pre><code class=\"language-bash\">python -m pip install --upgrade pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5parmap\uff1a\n<pre><code class=\"language-bash\">pip install parmap<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86\u591a\u4e2aPython\u7248\u672c\uff0c\u5e76\u4e14\u9ed8\u8ba4\u7684pip\u4e0d\u662f\u4f60\u9700\u8981\u7684\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528pip3\u6765\u5b89\u88c5\uff1a\n<pre><code class=\"language-bash\">pip3 install parmap<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u89e3\u51b3\u53ef\u80fd\u7684\u4f9d\u8d56\u95ee\u9898<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\u4f9d\u8d56\u95ee\u9898\uff0c\u8fd9\u53ef\u80fd\u662f\u7531\u4e8eparmap\u4f9d\u8d56\u7684\u5176\u4ed6\u5e93\u7248\u672c\u4e0d\u517c\u5bb9\u6216\u672a\u5b89\u88c5\u6240\u81f4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u68c0\u67e5PIP\u662f\u5426\u5b89\u88c5\u4e86\u6240\u6709\u5fc5\u9700\u7684\u4f9d\u8d56\uff1a\n<pre><code class=\"language-bash\">pip check<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5982\u679c\u53d1\u73b0\u6709\u4efb\u4f55\u4e0d\u517c\u5bb9\u7684\u5e93\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u66f4\u65b0\u8fd9\u4e9b\u5e93\uff1a\n<pre><code class=\"language-bash\">pip install --upgrade &lt;library_name&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5982\u679c\u66f4\u65b0\u65e0\u6548\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5378\u8f7d\u5e76\u91cd\u65b0\u5b89\u88c5\u4e0d\u517c\u5bb9\u7684\u5e93\uff1a\n<pre><code class=\"language-bash\">pip uninstall &lt;library_name&gt;<\/p>\n<p>pip install &lt;library_name&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728Python\u73af\u5883\u4e2d\u5bfc\u5165parmap\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00Python\u89e3\u91ca\u5668\uff1a\n<pre><code class=\"language-bash\">python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5c1d\u8bd5\u5bfc\u5165parmap\uff1a\n<pre><code class=\"language-python\">import parmap<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u9519\u8bef\u6d88\u606f\uff0c\u90a3\u4e48parmap\u5df2\u7ecf\u6210\u529f\u5b89\u88c5\u3002<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u4e0e\u5176\u4ed6\u9879\u76ee\u7684\u4f9d\u8d56\u51b2\u7a81\uff0c\u5efa\u8bae\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5parmap\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff1a\n<pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a\n<ul>\n<li>\u5728Windows\u4e0a\uff1a\n<pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728macOS\u548cLinux\u4e0a\uff1a\n<pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5parmap\uff1a\n<pre><code class=\"language-bash\">pip install parmap<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5f53\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a\n<pre><code class=\"language-bash\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u901f\u5ea6\u6162\u6216\u5b89\u88c5\u5931\u8d25\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9047\u5230\u901f\u5ea6\u6162\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u56fd\u5185\u7684\u955c\u50cf\u6e90\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple parmap<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6743\u9650\u95ee\u9898\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u3002\u53ef\u4ee5\u5c1d\u8bd5\u5728\u547d\u4ee4\u524d\u52a0\u4e0a<code>sudo<\/code>\uff08macOS\/Linux\uff09\u6216\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\uff08Windows\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\u3002\u5982\u679c\u4f60\u5728\u516c\u53f8\u6216\u5b66\u6821\u7f51\u7edc\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u4ee3\u7406\u670d\u52a1\u5668\u8fde\u63a5\u4e92\u8054\u7f51\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001parmap\u7684\u57fa\u672c\u4f7f\u7528<\/p>\n<\/p>\n<p><p>parmap\u662f\u4e00\u4e2a\u7528\u4e8e\u5728Python\u4e2d\u8fdb\u884c\u5e76\u884c\u6620\u5c04\u64cd\u4f5c\u7684\u5e93\uff0c\u5b83\u53ef\u4ee5\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><p>parmap\u7684\u57fa\u672c\u7528\u6cd5\u4e0ePython\u5185\u7f6e\u7684map\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u5b83\u53ef\u4ee5\u540c\u65f6\u5728\u591a\u4e2a\u8fdb\u7a0b\u4e2d\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import parmap<\/p>\n<p>def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>results = parmap.map(square, range(10))<\/p>\n<p>print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6307\u5b9a\u8fdb\u7a0b\u6570\u91cf\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012<code>pm_processes<\/code>\u53c2\u6570\u6765\u63a7\u5236\u5e76\u884c\u6267\u884c\u65f6\u7684\u8fdb\u7a0b\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = parmap.map(square, range(10), pm_processes=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u989d\u5916\u53c2\u6570\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u51fd\u6570\u9700\u8981\u989d\u5916\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pm_pfunc<\/code>\u53c2\u6570\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def power(x, y):<\/p>\n<p>    return x  y<\/p>\n<p>results = parmap.map(power, range(10), y=3)<\/p>\n<p>print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728Python\u4e2d\u6210\u529f\u5b89\u88c5\u548c\u4f7f\u7528parmap\u5e93\u3002\u786e\u4fdd\u5728\u4f7f\u7528parmap\u65f6\uff0c\u7cfb\u7edf\u6709\u8db3\u591f\u7684\u5185\u5b58\u548cCPU\u8d44\u6e90\uff0c\u4ee5\u4fbf\u5145\u5206\u5229\u7528\u591a\u8fdb\u7a0b\u5e76\u884c\u8ba1\u7b97\u7684\u4f18\u52bf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5parmap\u5e93\uff1f<\/strong><br \/>\u8981\u5b89\u88c5parmap\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>pip install parmap\n<\/code><\/pre>\n<p>\u786e\u4fdd\u60a8\u7684pip\u7248\u672c\u662f\u6700\u65b0\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pip install --upgrade pip<\/code>\u6765\u66f4\u65b0pip\u3002<\/p>\n<p><strong>parmap\u5e93\u6709\u4ec0\u4e48\u4e3b\u8981\u529f\u80fd\uff1f<\/strong><br \/>parmap\u5e93\u4e3b\u8981\u7528\u4e8e\u5e76\u884c\u5904\u7406\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u51fd\u6570\u5e94\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u5b83\u652f\u6301\u591a\u79cd\u5e76\u884c\u65b9\u5f0f\uff0c\u5305\u62ec\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u5904\u7406\uff0c\u9002\u5408\u5904\u7406\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528parmap\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u4f7f\u7528parmap\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6240\u5904\u7406\u7684\u51fd\u6570\u662f\u53ef\u5e8f\u5217\u5316\u7684\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528\u591a\u8fdb\u7a0b\u65f6\u3002\u6b64\u5916\uff0c\u5408\u7406\u8bbe\u7f6e\u8fdb\u7a0b\u6570\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u8fc7\u591a\u7684\u8fdb\u7a0b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0a\u4e0b\u6587\u5207\u6362\u5e26\u6765\u7684\u6027\u80fd\u635f\u5931\u3002\u5efa\u8bae\u5728\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u65f6\uff0c\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\uff0c\u4ee5\u9009\u62e9\u6700\u4f73\u7684\u5e76\u884c\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5b89\u88c5parmap\u7684\u6b65\u9aa4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\uff1a\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3001\u89e3\u51b3\u53ef\u80fd\u7684\u4f9d\u8d56\u95ee\u9898\u3001\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f [&hellip;]","protected":false},"author":3,"featured_media":959922,"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\/959918"}],"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=959918"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959918\/revisions"}],"predecessor-version":[{"id":959924,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959918\/revisions\/959924"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959922"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}