{"id":1078475,"date":"2025-01-08T12:11:57","date_gmt":"2025-01-08T04:11:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078475.html"},"modified":"2025-01-08T12:11:59","modified_gmt":"2025-01-08T04:11:59","slug":"python%e5%a6%82%e4%bd%95%e5%85%85%e5%88%86%e5%88%a9%e7%94%a8cpu%e8%b5%84%e6%ba%90-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1078475.html","title":{"rendered":"python\u5982\u4f55\u5145\u5206\u5229\u7528cpu\u8d44\u6e90"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182017\/76c1f849-34bc-4990-a2d8-a6185c0bb043.webp\" alt=\"python\u5982\u4f55\u5145\u5206\u5229\u7528cpu\u8d44\u6e90\" \/><\/p>\n<p><p> <strong>Python\u5145\u5206\u5229\u7528CPU\u8d44\u6e90\u7684\u65b9\u6cd5\u6709\uff1a\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u3001\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u5e93\u3002<\/strong> \u8fd9\u91cc\u5c06\u8be6\u7ec6\u63cf\u8ff0<strong>\u591a\u8fdb\u7a0b<\/strong>\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p><strong>\u591a\u8fdb\u7a0b<\/strong>\u662fPython\u4e2d\u5e38\u7528\u7684\u4e00\u79cd\u6280\u672f\u6765\u5145\u5206\u5229\u7528CPU\u8d44\u6e90\u3002Python\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u5728\u540c\u4e00\u65f6\u95f4\u5185\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\uff0c\u56e0\u6b64\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e0a\uff0c\u591a\u7ebf\u7a0b\u5e76\u4e0d\u80fd\u6709\u6548\u5229\u7528\u591a\u6838CPU\u3002\u800c\u591a\u8fdb\u7a0b\u5219\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u81ea\u5df1\u7684Python\u89e3\u91ca\u5668\u548cGIL\uff0c\u80fd\u591f\u771f\u6b63\u5e76\u884c\u8fd0\u884c\uff0c\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u591a\u8fdb\u7a0b\u7f16\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>Process<\/code>\u7c7b\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u548c\u542f\u52a8\u591a\u4e2a\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import os<\/p>\n<p>def worker(num):<\/p>\n<p>    &quot;&quot;&quot;\u8fdb\u7a0b\u8fd0\u884c\u7684\u51fd\u6570&quot;&quot;&quot;<\/p>\n<p>    print(f&#39;Worker: {num}, PID: {os.getpid()}&#39;)<\/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>    processes = []<\/p>\n<p>    for i in range(5):<\/p>\n<p>        p = multiprocessing.Process(target=worker, args=(i,))<\/p>\n<p>        processes.append(p)<\/p>\n<p>        p.start()<\/p>\n<p>    for p in processes:<\/p>\n<p>        p.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e865\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u8fd0\u884c<code>worker<\/code>\u51fd\u6570\u3002<code>os.getpid()<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684PID\uff0c\u4ee5\u65b9\u4fbf\u89c2\u5bdf\u8fdb\u7a0b\u7684\u521b\u5efa\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u8fdb\u7a0b\u6c60<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u521b\u5efa\u5927\u91cf\u8fdb\u7a0b\u65f6\uff0c\u624b\u52a8\u7ba1\u7406\u8fdb\u7a0b\u4f1a\u53d8\u5f97\u975e\u5e38\u590d\u6742\u4e14\u5bb9\u6613\u51fa\u9519\u3002<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>Pool<\/code>\u7c7b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u8fdb\u7a0b\u6c60\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def worker(num):<\/p>\n<p>    &quot;&quot;&quot;\u8fdb\u7a0b\u8fd0\u884c\u7684\u51fd\u6570&quot;&quot;&quot;<\/p>\n<p>    print(f&#39;Worker: {num}, PID: {os.getpid()}&#39;)<\/p>\n<p>    return num * num<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    with multiprocessing.Pool(processes=4) as pool:<\/p>\n<p>        results = pool.map(worker, range(10))<\/p>\n<p>        print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b4\u4e2a\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u6c60\uff0c\u4f7f\u7528<code>pool.map<\/code>\u65b9\u6cd5\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u8fdb\u7a0b\u6c60\u4e2d\u7684\u8fdb\u7a0b\u3002<code>pool.map<\/code>\u4f1a\u5c06\u4efb\u52a1\u51fd\u6570<code>worker<\/code>\u5e94\u7528\u5230\u7ed9\u5b9a\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61<code>range(10)<\/code>\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u90e8\u5206\u3002<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u65b9\u5f0f\uff0c\u5305\u62ec<code>Queue<\/code>\u3001<code>Pipe<\/code>\u3001<code>Value<\/code>\u548c<code>Array<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Queue\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h4>\n<\/p>\n<p><p><code>Queue<\/code>\u662f\u4e00\u4e2a\u591a\u751f\u4ea7\u8005\u3001\u591a\u6d88\u8d39\u8005\u961f\u5217\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def producer(queue):<\/p>\n<p>    for i in range(5):<\/p>\n<p>        item = f&#39;item-{i}&#39;<\/p>\n<p>        print(f&#39;Producing {item}&#39;)<\/p>\n<p>        queue.put(item)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def consumer(queue):<\/p>\n<p>    while True:<\/p>\n<p>        item = queue.get()<\/p>\n<p>        if item is None:<\/p>\n<p>            break<\/p>\n<p>        print(f&#39;Consuming {item}&#39;)<\/p>\n<p>        time.sleep(2)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    queue = multiprocessing.Queue()<\/p>\n<p>    p1 = multiprocessing.Process(target=producer, args=(queue,))<\/p>\n<p>    p2 = multiprocessing.Process(target=consumer, args=(queue,))<\/p>\n<p>    p1.start()<\/p>\n<p>    p2.start()<\/p>\n<p>    p1.join()<\/p>\n<p>    queue.put(None)  # Signal the consumer to exit<\/p>\n<p>    p2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u8fdb\u7a0b\uff0c\u4e00\u4e2a\u751f\u4ea7\u8005\u8fdb\u7a0b\u548c\u4e00\u4e2a\u6d88\u8d39\u8005\u8fdb\u7a0b\u3002\u751f\u4ea7\u8005\u8fdb\u7a0b\u5c06\u6d88\u606f\u653e\u5165\u961f\u5217\u4e2d\uff0c\u6d88\u8d39\u8005\u8fdb\u7a0b\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6d88\u606f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Pipe\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h4>\n<\/p>\n<p><p><code>Pipe<\/code>\u63d0\u4f9b\u4e86\u4e00\u5bf9\u8fde\u63a5\u7684\u7ba1\u9053\u5bf9\u8c61\uff0c\u5206\u522b\u8868\u793a\u7ba1\u9053\u7684\u4e24\u7aef\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def sender(pipe):<\/p>\n<p>    for i in range(5):<\/p>\n<p>        msg = f&#39;Message {i}&#39;<\/p>\n<p>        print(f&#39;Sending: {msg}&#39;)<\/p>\n<p>        pipe.send(msg)<\/p>\n<p>def receiver(pipe):<\/p>\n<p>    while True:<\/p>\n<p>        msg = pipe.recv()<\/p>\n<p>        if msg == &#39;END&#39;:<\/p>\n<p>            break<\/p>\n<p>        print(f&#39;Received: {msg}&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    parent_conn, child_conn = multiprocessing.Pipe()<\/p>\n<p>    p1 = multiprocessing.Process(target=sender, args=(child_conn,))<\/p>\n<p>    p2 = multiprocessing.Process(target=receiver, args=(parent_conn,))<\/p>\n<p>    p1.start()<\/p>\n<p>    p2.start()<\/p>\n<p>    p1.join()<\/p>\n<p>    child_conn.send(&#39;END&#39;)  # Signal the receiver to exit<\/p>\n<p>    p2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u5bf9\u8fde\u63a5\u7684\u7ba1\u9053\u5bf9\u8c61<code>parent_conn<\/code>\u548c<code>child_conn<\/code>\uff0c\u5206\u522b\u8868\u793a\u7ba1\u9053\u7684\u4e24\u7aef\u3002\u53d1\u9001\u8fdb\u7a0b\u5c06\u6d88\u606f\u901a\u8fc7\u7ba1\u9053\u53d1\u9001\u5230\u63a5\u6536\u8fdb\u7a0b\uff0c\u63a5\u6536\u8fdb\u7a0b\u4ece\u7ba1\u9053\u4e2d\u63a5\u6536\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5171\u4eab\u5185\u5b58<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5728\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>Value<\/code>\u548c<code>Array<\/code>\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Value\u5171\u4eab\u5355\u4e2a\u53d8\u91cf<\/h4>\n<\/p>\n<p><p><code>Value<\/code>\u662f\u4e00\u4e2a\u591a\u8fdb\u7a0b\u5171\u4eab\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u5355\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def increment(shared_value):<\/p>\n<p>    for _ in range(1000):<\/p>\n<p>        with shared_value.get_lock():<\/p>\n<p>            shared_value.value += 1<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    shared_value = multiprocessing.Value(&#39;i&#39;, 0)<\/p>\n<p>    processes = [multiprocessing.Process(target=increment, args=(shared_value,)) for _ in range(4)]<\/p>\n<p>    for p in processes:<\/p>\n<p>        p.start()<\/p>\n<p>    for p in processes:<\/p>\n<p>        p.join()<\/p>\n<p>    print(f&#39;Shared value: {shared_value.value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5171\u4eab\u7684\u6574\u6570\u53d8\u91cf<code>shared_value<\/code>\uff0c\u5e76\u542f\u52a8\u4e864\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u5bf9\u8be5\u53d8\u91cf\u8fdb\u884c1000\u6b21\u9012\u589e\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528\u5171\u4eab\u53d8\u91cf\u7684\u9501\u673a\u5236\uff0c\u786e\u4fdd\u4e86\u5bf9\u5171\u4eab\u53d8\u91cf\u7684\u64cd\u4f5c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Array\u5171\u4eab\u6570\u7ec4<\/h4>\n<\/p>\n<p><p><code>Array<\/code>\u662f\u4e00\u4e2a\u591a\u8fdb\u7a0b\u5171\u4eab\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def increment(shared_array):<\/p>\n<p>    for i in range(len(shared_array)):<\/p>\n<p>        with shared_array.get_lock():<\/p>\n<p>            shared_array[i] += 1<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    shared_array = multiprocessing.Array(&#39;i&#39;, [0, 0, 0, 0])<\/p>\n<p>    processes = [multiprocessing.Process(target=increment, args=(shared_array,)) for _ in range(4)]<\/p>\n<p>    for p in processes:<\/p>\n<p>        p.start()<\/p>\n<p>    for p in processes:<\/p>\n<p>        p.join()<\/p>\n<p>    print(f&#39;Shared array: {shared_array[:]}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5171\u4eab\u7684\u6574\u6570\u6570\u7ec4<code>shared_array<\/code>\uff0c\u5e76\u542f\u52a8\u4e864\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u5bf9\u8be5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u9012\u589e\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528\u5171\u4eab\u6570\u7ec4\u7684\u9501\u673a\u5236\uff0c\u786e\u4fdd\u4e86\u5bf9\u5171\u4eab\u6570\u7ec4\u7684\u64cd\u4f5c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u7f16\u7a0b\u867d\u7136\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\uff0c\u4f46\u4e5f\u5e26\u6765\u4e86\u4e00\u4e9b\u6027\u80fd\u4e0a\u7684\u5f00\u9500\uff0c\u5982\u8fdb\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500\u3001\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u5f00\u9500\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6027\u80fd\u4f18\u5316\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5408\u7406\u9009\u62e9\u8fdb\u7a0b\u6570\u91cf<\/strong>\uff1a\u8fdb\u7a0b\u6570\u91cf\u4e0d\u5b9c\u8fc7\u591a\uff0c\u4e00\u822c\u8bbe\u7f6e\u4e3aCPU\u6838\u5fc3\u6570\u6216\u7565\u591a\u4e8e\u6838\u5fc3\u6570\u3002<\/li>\n<li><strong>\u51cf\u5c11\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/strong>\uff1a\u5c3d\u91cf\u51cf\u5c11\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u9891\u7387\u548c\u6570\u636e\u91cf\uff0c\u4ee5\u964d\u4f4e\u901a\u4fe1\u5f00\u9500\u3002<\/li>\n<li><strong>\u907f\u514d\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u8fdb\u7a0b<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u8fdb\u7a0b\u6c60\u6765\u7ba1\u7406\u8fdb\u7a0b\uff0c\u907f\u514d\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u8fdb\u7a0b\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff0c\u5982<code>Queue<\/code>\u3001<code>Pipe<\/code>\u3001<code>Value<\/code>\u548c<code>Array<\/code>\u7b49\u3002<\/li>\n<li><strong>\u4f18\u5316\u4efb\u52a1\u5206\u914d<\/strong>\uff1a\u5408\u7406\u5206\u914d\u4efb\u52a1\uff0c\u5c3d\u91cf\u5747\u8861\u5404\u4e2a\u8fdb\u7a0b\u7684\u5de5\u4f5c\u91cf\uff0c\u907f\u514d\u67d0\u4e9b\u8fdb\u7a0b\u8fc7\u8f7d\u6216\u7a7a\u95f2\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u591a\u8fdb\u7a0b\u5728\u5b9e\u9645\u4e2d\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u4e24\u4e2a\u5b9e\u9645\u5e94\u7528\u6848\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1. \u5927\u89c4\u6a21\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u8fdb\u7a0b\u6765\u52a0\u901f\u6570\u636e\u7684\u8bfb\u53d6\u3001\u5904\u7406\u548c\u5b58\u50a8\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u5927\u6587\u4ef6\u5e76\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import pandas as pd<\/p>\n<p>def process_chunk(chunk):<\/p>\n<p>    # \u6570\u636e\u6e05\u6d17\u548c\u8f6c\u6362\u64cd\u4f5c<\/p>\n<p>    chunk[&#39;new_column&#39;] = chunk[&#39;existing_column&#39;] * 2<\/p>\n<p>    return chunk<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    filename = &#39;large_dataset.csv&#39;<\/p>\n<p>    chunksize = 100000<\/p>\n<p>    pool = multiprocessing.Pool(processes=4)<\/p>\n<p>    reader = pd.read_csv(filename, chunksize=chunksize)<\/p>\n<p>    results = pool.map(process_chunk, reader)<\/p>\n<p>    df = pd.concat(results)<\/p>\n<p>    df.to_csv(&#39;processed_dataset.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6\u5927\u6587\u4ef6\uff0c\u5e76\u5c06\u6570\u636e\u6309\u5757\u5206\u5272\u540e\u4ea4\u7ed9\u8fdb\u7a0b\u6c60\u4e2d\u7684\u8fdb\u7a0b\u8fdb\u884c\u5e76\u884c\u5904\u7406\u3002\u5904\u7406\u540e\u7684\u6570\u636e\u5757\u518d\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684DataFrame\uff0c\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u7f51\u7edc\u722c\u866b<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u722c\u866b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u8fdb\u7a0b\u6765\u52a0\u901f\u7f51\u9875\u7684\u6293\u53d6\u548c\u89e3\u6790\u3002\u4f8b\u5982\uff0c\u6293\u53d6\u591a\u4e2a\u7f51\u7ad9\u7684\u9875\u9762\u5185\u5bb9\u5e76\u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>def fetch_url(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>    title = soup.title.string if soup.title else &#39;No title&#39;<\/p>\n<p>    return (url, title)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    urls = [<\/p>\n<p>        &#39;https:\/\/www.example.com&#39;,<\/p>\n<p>        &#39;https:\/\/www.python.org&#39;,<\/p>\n<p>        &#39;https:\/\/www.github.com&#39;,<\/p>\n<p>        # \u66f4\u591aURL<\/p>\n<p>    ]<\/p>\n<p>    pool = multiprocessing.Pool(processes=4)<\/p>\n<p>    results = pool.map(fetch_url, urls)<\/p>\n<p>    for url, title in results:<\/p>\n<p>        print(f&#39;URL: {url}, Title: {title}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests<\/code>\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u89e3\u6790\u7f51\u9875\u7684\u6807\u9898\u3002\u901a\u8fc7\u591a\u8fdb\u7a0b\u5e76\u884c\u6293\u53d6\u591a\u4e2a\u7f51\u7ad9\u7684\u9875\u9762\u5185\u5bb9\uff0c\u52a0\u5feb\u4e86\u722c\u866b\u7684\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86Python\u4e2d\u5145\u5206\u5229\u7528CPU\u8d44\u6e90\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u7279\u522b\u662f<strong>\u591a\u8fdb\u7a0b\u7f16\u7a0b<\/strong>\u3002\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86\u591a\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u7ba1\u7406\u3001\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3001\u5171\u4eab\u5185\u5b58\u3001\u6027\u80fd\u4f18\u5316\u4ee5\u53ca\u5b9e\u9645\u5e94\u7528\u6848\u4f8b\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u591a\u8fdb\u7a0b\u7f16\u7a0b\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f18\u5316\u591a\u7ebf\u7a0b\u4ee5\u63d0\u9ad8CPU\u5229\u7528\u7387\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7531\u4e8e\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u7684\u5b58\u5728\uff0c\u591a\u7ebf\u7a0b\u53ef\u80fd\u65e0\u6cd5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3002\u4e0d\u8fc7\uff0c\u53ef\u4ee5\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u6216<code>threading<\/code>\u6a21\u5757\u5b9e\u73b0\u591a\u7ebf\u7a0b\u3002\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528\u591a\u8fdb\u7a0b\uff08<code>multiprocessing<\/code>\u6a21\u5757\uff09\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684Python\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684Python\u89e3\u91ca\u5668\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5229\u7528\u591a\u6838CPU\u8d44\u6e90\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c\u5e76\u884c\u5904\u7406\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\u5e76\u884c\u5904\u7406\uff0c\u5176\u4e2d<code>multiprocessing<\/code>\u662f\u6700\u5e38\u7528\u7684\u5e93\uff0c\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<code>joblib<\/code>\u548c<code>dask<\/code>\u4e5f\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u5b83\u4eec\u80fd\u591f\u7b80\u5316\u5e76\u884c\u8ba1\u7b97\u7684\u5b9e\u73b0\u3002\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>asyncio<\/code>\u5e93\uff0c\u901a\u8fc7\u5f02\u6b65\u7f16\u7a0b\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u54cd\u5e94\u6027\u548c\u6027\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u76d1\u6d4bPython\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u60c5\u51b5\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>psutil<\/code>\u5e93\u6765\u76d1\u6d4bPython\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u60c5\u51b5\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u83b7\u53d6\u7cfb\u7edf\u548c\u8fdb\u7a0b\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5305\u62ecCPU\u5229\u7528\u7387\u3001\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7b49\u3002\u901a\u8fc7\u5b9a\u671f\u8c03\u7528<code>psutil.cpu_percent()<\/code>\uff0c\u53ef\u4ee5\u5b9e\u65f6\u67e5\u770b\u5f53\u524d\u7a0b\u5e8f\u7684CPU\u5360\u7528\u60c5\u51b5\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6027\u80fd\u8c03\u4f18\u548c<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5145\u5206\u5229\u7528CPU\u8d44\u6e90\u7684\u65b9\u6cd5\u6709\uff1a\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u3001\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u5e93\u3002 \u8fd9\u91cc\u5c06\u8be6\u7ec6\u63cf\u8ff0\u591a\u8fdb\u7a0b\u8fd9\u4e00\u70b9 [&hellip;]","protected":false},"author":3,"featured_media":1078481,"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\/1078475"}],"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=1078475"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078475\/revisions"}],"predecessor-version":[{"id":1078482,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078475\/revisions\/1078482"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078481"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}