{"id":1021399,"date":"2024-12-27T13:22:37","date_gmt":"2024-12-27T05:22:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1021399.html"},"modified":"2024-12-27T13:22:38","modified_gmt":"2024-12-27T05:22:38","slug":"python%e5%a6%82%e4%bd%95%e5%bc%80%e5%90%af%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1021399.html","title":{"rendered":"python\u5982\u4f55\u5f00\u542f\u591a\u7ebf\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25163648\/a4332a10-c521-4e86-9fbe-4e71f3563f81.webp\" alt=\"python\u5982\u4f55\u5f00\u542f\u591a\u7ebf\u7a0b\" \/><\/p>\n<p><p> Python\u5f00\u542f\u591a\u7ebf\u7a0b\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u3001<code>concurrent.futures<\/code>\u6a21\u5757\u548c<code>multiprocessing<\/code>\u6a21\u5757\u3002<strong>\u901a\u8fc7<code>threading<\/code>\u6a21\u5757\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u7ebf\u7a0b\u3001\u901a\u8fc7<code>concurrent.futures<\/code>\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u6c60\u8fdb\u884c\u7ba1\u7406\u3001\u800c\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u80fd\u6709\u6548\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u63d0\u5347\u6027\u80fd<\/strong>\u3002\u5176\u4e2d\uff0c<code>threading<\/code>\u6a21\u5757\u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u591a\u7ebf\u7a0b\u4efb\u52a1\u3002<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u9ad8\u7ea7\u522b\u7684\u63a5\u53e3\uff0c\u65b9\u4fbf\u5bf9\u591a\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u7ba1\u7406\u3002<code>multiprocessing<\/code>\u6a21\u5757\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u591a\u8fdb\u7a0b\uff0c\u4f46\u4e5f\u53ef\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\uff0c\u5c24\u5176\u5728\u9700\u8981\u5145\u5206\u5229\u7528\u591a\u6838 CPU \u7684\u60c5\u51b5\u4e0b\u8868\u73b0\u51fa\u8272\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6765\u5f00\u542f\u548c\u7ba1\u7406\u591a\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 THREADING \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>threading<\/code>\u6a21\u5757\u662f Python \u5185\u7f6e\u7684\u591a\u7ebf\u7a0b\u6a21\u5757\uff0c\u9002\u5408\u7528\u4e8e\u7b80\u5355\u7684\u591a\u7ebf\u7a0b\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u7684\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7ebf\u7a0b\u521b\u5efa<\/h4>\n<\/p>\n<p><p>\u5728<code>threading<\/code>\u6a21\u5757\u4e2d\uff0c\u6700\u57fa\u672c\u7684\u4f7f\u7528\u65b9\u5f0f\u662f\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u5e76\u542f\u52a8\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>threading.Thread<\/code>\u7c7b\u6765\u5b9a\u4e49\u7ebf\u7a0b\uff0c\u6216\u8005\u76f4\u63a5\u521b\u5efa<code>Thread<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(i)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=print_numbers)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<code>print_numbers<\/code>\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\u3002\u901a\u8fc7\u8c03\u7528<code>thread.start()<\/code>\u6765\u542f\u52a8\u7ebf\u7a0b\uff0c<code>thread.join()<\/code>\u5219\u7528\u4e8e\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h4>2. \u7ebf\u7a0b\u7c7b\u7684\u7ee7\u627f<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u7ee7\u627f<code>threading.Thread<\/code>\u7c7b\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u63a7\u5236\u7ebf\u7a0b\u7684\u884c\u4e3a\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u5728\u7ebf\u7a0b\u4e2d\u6267\u884c\u590d\u6742\u903b\u8f91\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class NumberPrinter(threading.Thread):<\/p>\n<p>    def run(self):<\/p>\n<p>        for i in range(5):<\/p>\n<p>            print(i)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = NumberPrinter()<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u7ee7\u627f<code>threading.Thread<\/code>\u7c7b\u5e76\u91cd\u5199<code>run<\/code>\u65b9\u6cd5\u6765\u5b9a\u4e49\u7ebf\u7a0b\u7684\u884c\u4e3a\uff0c\u7136\u540e\u50cf\u4f7f\u7528\u666e\u901a\u7c7b\u4e00\u6837\u521b\u5efa\u548c\u542f\u52a8\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>3. \u7ebf\u7a0b\u540c\u6b65<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u540c\u6b65\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u540c\u6b65\u539f\u8bed\uff0c\u5982\u9501\u3001\u4e8b\u4ef6\u3001\u6761\u4ef6\u53d8\u91cf\u7b49\u3002\u6700\u5e38\u7528\u7684\u662f\u9501\uff08Lock\uff09\uff0c\u7528\u4e8e\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u8bbf\u95ee\u67d0\u4e2a\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def critical_section():<\/p>\n<p>    with lock:<\/p>\n<p>        # \u8bbf\u95ee\u5171\u4eab\u8d44\u6e90<\/p>\n<p>        print(&quot;Accessing critical section&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = [threading.Thread(target=critical_section) for _ in range(5)]<\/p>\n<h2><strong>\u542f\u52a8\u6240\u6709\u7ebf\u7a0b<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>threading.Lock<\/code>\u6765\u4fdd\u62a4\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\uff0c\u786e\u4fdd\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8fdb\u5165\u4e34\u754c\u533a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 CONCURRENT.FUTURES \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u7ea7\u63a5\u53e3\u7528\u4e8e\u5f02\u6b65\u6267\u884c\u8c03\u7528\u3002\u5b83\u652f\u6301\u7ebf\u7a0b\u6c60\u548c\u8fdb\u7a0b\u6c60\uff0c\u9002\u7528\u4e8e\u9700\u8981\u540c\u65f6\u7ba1\u7406\u591a\u4e2a\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u7ebf\u7a0b\u6c60\u7684\u521b\u5efa\u548c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>concurrent.futures.ThreadPoolExecutor<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u6c60\u3002\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u4e00\u7ec4\u4efb\u52a1\uff0c\u5e76\u80fd\u6709\u6548\u5730\u7ba1\u7406\u7ebf\u7a0b\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def print_numbers(n):<\/p>\n<p>    for i in range(n):<\/p>\n<p>        print(i)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    futures = [executor.submit(print_numbers, 5) for _ in range(3)]<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210<\/strong><\/h2>\n<p>for future in futures:<\/p>\n<p>    future.result()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\uff0c\u5e76\u63d0\u4ea4\u4e86\u4e09\u4e2a\u4efb\u52a1\u3002\u7ebf\u7a0b\u6c60\u4f1a\u81ea\u52a8\u7ba1\u7406\u7ebf\u7a0b\u7684\u542f\u52a8\u548c\u9500\u6bc1\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528 Future \u5bf9\u8c61<\/h4>\n<\/p>\n<p><p><code>concurrent.futures<\/code>\u6a21\u5757\u4e2d\u7684<code>Future<\/code>\u5bf9\u8c61\u7528\u4e8e\u83b7\u53d6\u4efb\u52a1\u7684\u7ed3\u679c\u6216\u72b6\u6001\u3002\u901a\u8fc7<code>Future<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u67e5\u8be2\u4efb\u52a1\u662f\u5426\u5b8c\u6210\u3001\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\u6216\u68c0\u67e5\u4efb\u52a1\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def compute_square(n):<\/p>\n<p>    return n * n<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    future = executor.submit(compute_square, 5)<\/p>\n<h2><strong>\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c<\/strong><\/h2>\n<p>result = future.result()<\/p>\n<p>print(f&quot;Square: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u63d0\u4ea4\u4e86\u4e00\u4e2a\u8ba1\u7b97\u5e73\u65b9\u7684\u4efb\u52a1\uff0c\u5e76\u901a\u8fc7<code>Future.result()<\/code>\u65b9\u6cd5\u83b7\u53d6\u4e86\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 MULTIPROCESSING \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>multiprocessing<\/code>\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u591a\u8fdb\u7a0b\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u3002\u8fd9\u5728\u9700\u8981\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a Python \u7684 GIL \u9650\u5236\u4e86\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u591a\u7ebf\u7a0b\u7684\u5e76\u884c\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528 Process \u7c7b<\/h4>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e0e<code>threading<\/code>\u6a21\u5757\u7c7b\u4f3c\u7684\u63a5\u53e3\u6765\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Process<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(i)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u8fdb\u7a0b<\/strong><\/h2>\n<p>process = Process(target=print_numbers)<\/p>\n<p>process.start()<\/p>\n<p>process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e0e<code>threading<\/code>\u6a21\u5757\u7684\u7528\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u5728\u521b\u5efa\u548c\u542f\u52a8\u72ec\u7acb\u7684\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528 Pool \u5bf9\u8c61<\/h4>\n<\/p>\n<p><p><code>multiprocessing.Pool<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u7ba1\u7406\u591a\u4e2a\u8fdb\u7a0b\u3002\u5b83\u7c7b\u4f3c\u4e8e<code>ThreadPoolExecutor<\/code>\uff0c\u53ef\u4ee5\u7528\u4e8e\u5e76\u884c\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>def compute_square(n):<\/p>\n<p>    return n * n<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b\u6c60<\/strong><\/h2>\n<p>with Pool(3) as pool:<\/p>\n<p>    results = pool.map(compute_square, [1, 2, 3, 4, 5])<\/p>\n<p>print(f&quot;Squares: {results}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>Pool.map()<\/code>\u65b9\u6cd5\u6765\u5e76\u884c\u8ba1\u7b97\u4e00\u7ec4\u6570\u5b57\u7684\u5e73\u65b9\u3002<code>Pool<\/code>\u4f1a\u81ea\u52a8\u7ba1\u7406\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u4f7f\u7528\u591a\u7ebf\u7a0b\u8fd8\u662f\u591a\u8fdb\u7a0b\u65f6\uff0c\u9700\u8981\u8003\u8651\u4efb\u52a1\u7684\u6027\u8d28\u548c Python \u7684\u7279\u6027\u3002<strong>\u591a\u7ebf\u7a0b\u9002\u7528\u4e8e I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u800c\u591a\u8fdb\u7a0b\u9002\u7528\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\u3002\u8fd9\u662f\u56e0\u4e3a Python \u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u4f1a\u9650\u5236\u591a\u7ebf\u7a0b\u7684\u5e76\u884c\u6267\u884c\uff0c\u4f46\u8fdb\u7a0b\u95f4\u4e0d\u53d7\u6b64\u9650\u5236\u3002<\/p>\n<\/p>\n<p><h4>1. I\/O \u5bc6\u96c6\u578b\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u4e3b\u8981\u82b1\u8d39\u65f6\u95f4\u5728\u7b49\u5f85 I\/O \u64cd\u4f5c\u5b8c\u6210\uff0c\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u541e\u5410\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import requests<\/p>\n<p>def fetch_url(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    print(f&quot;{url}: {response.status_code}&quot;)<\/p>\n<p>urls = [&quot;http:\/\/example.com&quot; for _ in range(10)]<\/p>\n<p>threads = [threading.Thread(target=fetch_url, args=(url,)) for url in urls]<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u5e76\u53d1\u5730\u83b7\u53d6\u591a\u4e2a URL \u7684\u5185\u5bb9\uff0c\u51cf\u5c11\u4e86\u603b\u7684\u7b49\u5f85\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>2. CPU \u5bc6\u96c6\u578b\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4e3b\u8981\u82b1\u8d39\u65f6\u95f4\u5728\u8ba1\u7b97\u4e0a\uff0c\u5982\u77e9\u9635\u8fd0\u7b97\u3001\u56fe\u50cf\u5904\u7406\u7b49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u591a\u8fdb\u7a0b\u80fd\u591f\u66f4\u597d\u5730\u5229\u7528\u591a\u6838 CPU \u7684\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>def compute_factorial(n):<\/p>\n<p>    if n == 0:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * compute_factorial(n-1)<\/p>\n<p>numbers = [5, 6, 7, 8, 9]<\/p>\n<p>with Pool(5) as pool:<\/p>\n<p>    factorials = pool.map(compute_factorial, numbers)<\/p>\n<p>print(f&quot;Factorials: {factorials}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u591a\u8fdb\u7a0b\u6765\u5e76\u884c\u8ba1\u7b97\u591a\u4e2a\u6570\u5b57\u7684\u9636\u4e58\uff0c\u4ece\u800c\u5145\u5206\u5229\u7528\u4e86\u591a\u6838\u5904\u7406\u5668\u7684\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ebf\u7a0b\u5b89\u5168\u6027\u548c\u6b7b\u9501\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u5b89\u5168\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\u3002<strong>\u7ebf\u7a0b\u5b89\u5168\u6027\u4e3b\u8981\u6d89\u53ca\u5230\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u5171\u4eab\u6570\u636e\u7684\u8bbf\u95ee\u548c\u4fee\u6539<\/strong>\u3002\u4e3a\u4e86\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u548c\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\uff0c\u9700\u8981\u4f7f\u7528\u540c\u6b65\u539f\u8bed\u6765\u4fdd\u62a4\u5171\u4eab\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u9501\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168<\/h4>\n<\/p>\n<p><p>\u9501\uff08Lock\uff09\u662f\u6700\u57fa\u672c\u7684\u540c\u6b65\u539f\u8bed\uff0c\u7528\u4e8e\u786e\u4fdd\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>balance = 0<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def deposit(amount):<\/p>\n<p>    global balance<\/p>\n<p>    with lock:<\/p>\n<p>        balance += amount<\/p>\n<p>threads = [threading.Thread(target=deposit, args=(100,)) for _ in range(10)]<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>print(f&quot;Final balance: {balance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u9501\u6765\u4fdd\u62a4\u5bf9<code>balance<\/code>\u53d8\u91cf\u7684\u8bbf\u95ee\uff0c\u786e\u4fdd\u6bcf\u6b21\u66f4\u65b0\u90fd\u662f\u539f\u5b50\u7684\u3002<\/p>\n<\/p>\n<p><h4>2. \u907f\u514d\u6b7b\u9501<\/h4>\n<\/p>\n<p><p>\u6b7b\u9501\u662f\u6307\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u8d44\u6e90\uff0c\u4ece\u800c\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u3002\u4e3a\u4e86\u907f\u514d\u6b7b\u9501\uff0c\u9700\u8981\u5c0f\u5fc3\u8bbe\u8ba1\u9501\u7684\u83b7\u53d6\u987a\u5e8f\u548c\u4f7f\u7528\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock1 = threading.Lock()<\/p>\n<p>lock2 = threading.Lock()<\/p>\n<p>def thread1():<\/p>\n<p>    with lock1:<\/p>\n<p>        print(&quot;Thread 1 acquired lock1&quot;)<\/p>\n<p>        with lock2:<\/p>\n<p>            print(&quot;Thread 1 acquired lock2&quot;)<\/p>\n<p>def thread2():<\/p>\n<p>    with lock2:<\/p>\n<p>        print(&quot;Thread 2 acquired lock2&quot;)<\/p>\n<p>        with lock1:<\/p>\n<p>            print(&quot;Thread 2 acquired lock1&quot;)<\/p>\n<p>t1 = threading.Thread(target=thread1)<\/p>\n<p>t2 = threading.Thread(target=thread2)<\/p>\n<p>t1.start()<\/p>\n<p>t2.start()<\/p>\n<p>t1.join()<\/p>\n<p>t2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u6b7b\u9501\uff0c\u56e0\u4e3a<code>thread1<\/code>\u548c<code>thread2<\/code>\u5bf9\u9501\u7684\u83b7\u53d6\u987a\u5e8f\u4e0d\u4e00\u81f4\u3002\u53ef\u4ee5\u901a\u8fc7\u4e00\u81f4\u7684\u9501\u83b7\u53d6\u987a\u5e8f\u6216\u4f7f\u7528\u8d85\u65f6\u673a\u5236\u6765\u907f\u514d\u6b7b\u9501\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u6a21\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u80fd\u591f\u964d\u4f4e\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u4f38\u7f29\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. \u964d\u4f4e\u7ebf\u7a0b\u7ba1\u7406\u5f00\u9500<\/h4>\n<\/p>\n<p><p>\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u662f\u4e00\u4e2a\u6602\u8d35\u7684\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u573a\u666f\u4e2d\u3002\u7ebf\u7a0b\u6c60\u901a\u8fc7\u590d\u7528\u7ebf\u7a0b\uff0c\u51cf\u5c11\u4e86\u8fd9\u4e9b\u5f00\u9500\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def process_task(task):<\/p>\n<p>    print(f&quot;Processing task: {task}&quot;)<\/p>\n<p>tasks = range(10)<\/p>\n<p>with ThreadPoolExecutor(max_workers=3) as executor:<\/p>\n<p>    executor.map(process_task, tasks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6765\u5904\u7406\u4e00\u7ec4\u4efb\u52a1\uff0c\u907f\u514d\u4e86\u9891\u7e41\u7684\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u3002<\/p>\n<\/p>\n<p><h4>2. \u63d0\u9ad8\u53ef\u4f38\u7f29\u6027<\/h4>\n<\/p>\n<p><p>\u7ebf\u7a0b\u6c60\u80fd\u591f\u9650\u5236\u5e76\u53d1\u7ebf\u7a0b\u7684\u6570\u91cf\uff0c\u4ece\u800c\u907f\u514d\u7cfb\u7edf\u8d44\u6e90\u88ab\u8017\u5c3d\u3002\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u7cfb\u7edf\u8d44\u6e90\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u4f38\u7f29\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def compute_heavy_task(task):<\/p>\n<p>    print(f&quot;Computing heavy task: {task}&quot;)<\/p>\n<p>tasks = range(20)<\/p>\n<p>with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    executor.map(compute_heavy_task, tasks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7ebf\u7a0b\u6c60\u9650\u5236\u4e86\u540c\u65f6\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\uff0c\u9632\u6b62\u7cfb\u7edf\u8d44\u6e90\u88ab\u8fc7\u5ea6\u6d88\u8017\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ebf\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b\u7684\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u7ebf\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u90fd\u662f\u5b9e\u73b0\u5e76\u53d1\u7684\u624b\u6bb5\uff0c\u5404\u6709\u4f18\u7f3a\u70b9\u3002<strong>\u7ebf\u7a0b\u9002\u5408\u9700\u8981\u5e76\u884c\u5904\u7406\u7684\u573a\u666f\uff0c\u800c\u5f02\u6b65\u7f16\u7a0b\u5219\u66f4\u9002\u5408 I\/O \u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<p><h4>1. \u7ebf\u7a0b\u7684\u4f18\u52bf\u548c\u52a3\u52bf<\/h4>\n<\/p>\n<p><p>\u7ebf\u7a0b\u53ef\u4ee5\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u4f18\u52bf\u6765\u5e76\u884c\u5904\u7406\u4efb\u52a1\uff0c\u4f46\u9700\u8981\u5904\u7406\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u7ade\u4e89\u95ee\u9898\u3002\u6b64\u5916\uff0cPython \u7684 GIL \u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u7684\u5e76\u884c\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def compute_task(task):<\/p>\n<p>    print(f&quot;Computing task: {task}&quot;)<\/p>\n<p>tasks = range(10)<\/p>\n<p>threads = [threading.Thread(target=compute_task, args=(task,)) for task in tasks]<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u5e76\u884c\u5904\u7406\u4e00\u7ec4\u4efb\u52a1\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u6027\u548c\u8d44\u6e90\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h4>2. \u5f02\u6b65\u7f16\u7a0b\u7684\u4f18\u52bf\u548c\u52a3\u52bf<\/h4>\n<\/p>\n<p><p>\u5f02\u6b65\u7f16\u7a0b\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af\u548c\u56de\u8c03\u673a\u5236\uff0c\u5b9e\u73b0\u4e86\u975e\u963b\u585e\u7684 I\/O \u64cd\u4f5c\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u5e76\u53d1 I\/O \u8bf7\u6c42\u3002\u7136\u800c\uff0c\u5f02\u6b65\u7f16\u7a0b\u7684\u4ee3\u7801\u7ed3\u6784\u8f83\u4e3a\u590d\u6742\uff0c\u4e0d\u6613\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def fetch_data(task):<\/p>\n<p>    print(f&quot;Fetching data for task: {task}&quot;)<\/p>\n<p>    aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t asyncio.sleep(1)<\/p>\n<p>async def main():<\/p>\n<p>    tasks = [fetch_data(task) for task in range(10)]<\/p>\n<p>    await asyncio.gather(*tasks)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\u6765\u5e76\u53d1\u5904\u7406\u591a\u4e2a I\/O \u8bf7\u6c42\uff0c\u907f\u514d\u4e86\u963b\u585e\u7b49\u5f85\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5e76\u53d1\u6a21\u578b\u65f6\uff0c\u9700\u8981\u7efc\u5408\u8003\u8651\u4efb\u52a1\u7684\u6027\u8d28\u3001\u7cfb\u7edf\u8d44\u6e90\u548c\u5f00\u53d1\u96be\u5ea6\u3002<strong>\u5bf9\u4e8e I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5f02\u6b65\u7f16\u7a0b\u548c\u591a\u7ebf\u7a0b\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u591a\u8fdb\u7a0b\u66f4\u80fd\u53d1\u6325\u591a\u6838\u7684\u4f18\u52bf<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>1. \u7efc\u5408\u8003\u8651\u4efb\u52a1\u6027\u8d28<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u660e\u786e\u4efb\u52a1\u7684\u6027\u8d28\uff0c\u662f I\/O \u5bc6\u96c6\u578b\u8fd8\u662f CPU \u5bc6\u96c6\u578b\u3002I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u7f16\u7a0b\u6765\u63d0\u9ad8\u5e76\u53d1\u6027\u80fd\uff0c\u800c CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u66f4\u9002\u5408\u4f7f\u7528\u591a\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u793a\u4f8b<\/p>\n<p>import threading<\/p>\n<p>import requests<\/p>\n<p>def fetch_url(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    print(f&quot;{url}: {response.status_code}&quot;)<\/p>\n<p>urls = [&quot;http:\/\/example.com&quot; for _ in range(10)]<\/p>\n<p>threads = [threading.Thread(target=fetch_url, args=(url,)) for url in urls]<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u793a\u4f8b<\/p>\n<p>from multiprocessing import Pool<\/p>\n<p>def compute_factorial(n):<\/p>\n<p>    if n == 0:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * compute_factorial(n-1)<\/p>\n<p>numbers = [5, 6, 7, 8, 9]<\/p>\n<p>with Pool(5) as pool:<\/p>\n<p>    factorials = pool.map(compute_factorial, numbers)<\/p>\n<p>print(f&quot;Factorials: {factorials}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8003\u8651\u7cfb\u7edf\u8d44\u6e90\u548c\u5f00\u53d1\u96be\u5ea6<\/h4>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5e76\u53d1\u6a21\u578b\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u7cfb\u7edf\u8d44\u6e90\u7684\u9650\u5236\u548c\u5f00\u53d1\u7684\u96be\u5ea6\u3002\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u90fd\u9700\u8981\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90\uff0c\u5982\u5185\u5b58\u548c CPU \u65f6\u95f4\u3002\u800c\u5f02\u6b65\u7f16\u7a0b\u867d\u7136\u5bf9\u7cfb\u7edf\u8d44\u6e90\u7684\u6d88\u8017\u8f83\u5c0f\uff0c\u4f46\u4ee3\u7801\u7684\u590d\u6742\u6027\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5f02\u6b65\u7f16\u7a0b\u793a\u4f8b<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch_data(task):<\/p>\n<p>    print(f&quot;Fetching data for task: {task}&quot;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>async def main():<\/p>\n<p>    tasks = [fetch_data(task) for task in range(10)]<\/p>\n<p>    await asyncio.gather(*tasks)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b\uff0c\u9700\u8981\u5728\u6027\u80fd\u548c\u5f00\u53d1\u6210\u672c\u4e4b\u95f4\u627e\u5230\u5e73\u8861\u3002\u901a\u8fc7\u5bf9\u4efb\u52a1\u7684\u6df1\u5165\u5206\u6790\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u9009\u62e9\u9002\u5408\u7684\u5e76\u53d1\u7b56\u7565\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>threading<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u5e76\u5b9a\u4e49\u8981\u6267\u884c\u7684\u4efb\u52a1\u3002\u4f7f\u7528<code>start()<\/code>\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\uff0c\u7ebf\u7a0b\u4f1a\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u5728Python\u4e2d\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u591a\u7ebf\u7a0b\u9002\u5408\u4e8e\u9700\u8981\u5e76\u53d1\u5904\u7406\u7684\u573a\u666f\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6I\/O\u64cd\u4f5c\u7b49\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u7684\u540c\u65f6\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<p><strong>Python\u7684GIL\u5bf9\u591a\u7ebf\u7a0b\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><br \/>Python\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u9650\u5236\u4e86\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cPython\u5b57\u8282\u7801\uff0c\u8fd9\u610f\u5473\u7740\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u591a\u7ebf\u7a0b\u53ef\u80fd\u4e0d\u4f1a\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u4efb\u52a1\uff0c\u4f7f\u7528\u591a\u8fdb\u7a0b\uff08\u5982<code>multiprocessing<\/code>\u6a21\u5757\uff09\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u591a\u7ebf\u7a0b\u4e2d\u7684\u5171\u4eab\u8d44\u6e90\uff1f<\/strong><br \/>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Lock<\/code>\u3001<code>RLock<\/code>\u7b49\u673a\u5236\u6765\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5f00\u542f\u591a\u7ebf\u7a0b\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528threading\u6a21\u5757\u3001concurrent.futures\u6a21\u5757\u548cmult [&hellip;]","protected":false},"author":3,"featured_media":1021407,"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\/1021399"}],"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=1021399"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1021399\/revisions"}],"predecessor-version":[{"id":1021409,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1021399\/revisions\/1021409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1021407"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1021399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1021399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1021399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}