{"id":1187472,"date":"2025-01-15T20:05:59","date_gmt":"2025-01-15T12:05:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187472.html"},"modified":"2025-01-15T20:06:02","modified_gmt":"2025-01-15T12:06:02","slug":"python%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3gil%e9%94%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187472.html","title":{"rendered":"python\u5982\u4f55\u89e3\u51b3gil\u9501"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140036\/afefc998-b2bc-4a2d-ae7a-fa3adeeaefc2.webp\" alt=\"python\u5982\u4f55\u89e3\u51b3gil\u9501\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\u3001\u4f7f\u7528\u5176\u4ed6\u89e3\u91ca\u5668\u7b49\u65b9\u5f0f\u6765\u89e3\u51b3GIL\u9501\u95ee\u9898\u3002<\/strong>\u5176\u4e2d\uff0c\u591a\u8fdb\u7a0b\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\u6765\u7ed5\u8fc7GIL\u9501\uff0c\u4ece\u800c\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002\u591a\u8fdb\u7a0b\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u7684\u4f18\u52bf\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u591a\u8fdb\u7a0b\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u662f\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\u6765\u7ed5\u8fc7GIL\u9501\uff0c\u4ece\u800c\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>multiprocessing<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u591a\u8fdb\u7a0b\u6a21\u5757\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\uff1a<\/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\u51fd\u6570&quot;&quot;&quot;<\/p>\n<p>    print(f&#39;Worker: {num}, Process ID: {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\u72ec\u7acb\u7684\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u6267\u884c<code>worker<\/code>\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u4e00\u4e2a\u4e0d\u540c\u7684\u53c2\u6570\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u8fdb\u7a0bID\uff0c\u4e92\u4e0d\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5171\u4eab\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u5171\u4eab\u6570\u636e\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u8fdb\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u5171\u4eab\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u961f\u5217\uff08Queue\uff09<\/strong><\/li>\n<\/ul>\n<p><p>\u961f\u5217\u662f\u4e00\u79cd\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u5b89\u5168\u7684FIFO\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u961f\u5217\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def worker(queue, num):<\/p>\n<p>    queue.put(f&#39;Worker: {num}&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    queue = multiprocessing.Queue()<\/p>\n<p>    processes = []<\/p>\n<p>    for i in range(5):<\/p>\n<p>        p = multiprocessing.Process(target=worker, args=(queue, 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>    while not queue.empty():<\/p>\n<p>        print(queue.get())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u7ba1\u9053\uff08Pipe\uff09<\/strong><\/li>\n<\/ul>\n<p><p>\u7ba1\u9053\u662f\u53e6\u4e00\u79cd\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u4e24\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u7ba1\u9053\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def worker(conn, num):<\/p>\n<p>    conn.send(f&#39;Worker: {num}&#39;)<\/p>\n<p>    conn.close()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    parent_conn, child_conn = multiprocessing.Pipe()<\/p>\n<p>    processes = []<\/p>\n<p>    for i in range(5):<\/p>\n<p>        p = multiprocessing.Process(target=worker, args=(child_conn, 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>    while parent_conn.poll():<\/p>\n<p>        print(parent_conn.recv())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528C\u6269\u5c55\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u591a\u8fdb\u7a0b\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199C\u6269\u5c55\u6a21\u5757\u6765\u7ed5\u8fc7GIL\u9501\u3002C\u6269\u5c55\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u5728Python\u4e2d\u8c03\u7528C\u4ee3\u7801\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684C\u6269\u5c55\u6a21\u5757\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u7f16\u5199C\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2aC\u51fd\u6570\uff0c\u5b83\u5c06\u88abPython\u8c03\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684C\u51fd\u6570\u793a\u4f8b\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>static PyObject* add(PyObject* self, PyObject* args) {<\/p>\n<p>    int a, b;<\/p>\n<p>    if (!PyArg_ParseTuple(args, &quot;ii&quot;, &amp;a, &amp;b)) {<\/p>\n<p>        return NULL;<\/p>\n<p>    }<\/p>\n<p>    return Py_BuildValue(&quot;i&quot;, a + b);<\/p>\n<p>}<\/p>\n<p>static PyMethodDef myMethods[] = {<\/p>\n<p>    {&quot;add&quot;, add, METH_VARARGS, &quot;Add two integers&quot;},<\/p>\n<p>    {NULL, NULL, 0, NULL}<\/p>\n<p>};<\/p>\n<p>static struct PyModuleDef myModule = {<\/p>\n<p>    PyModuleDef_HEAD_INIT,<\/p>\n<p>    &quot;myModule&quot;,<\/p>\n<p>    &quot;My custom C extension module&quot;,<\/p>\n<p>    -1,<\/p>\n<p>    myMethods<\/p>\n<p>};<\/p>\n<p>PyMODINIT_FUNC PyInit_myModule(void) {<\/p>\n<p>    return PyModule_Create(&amp;myModule);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7f16\u8bd1C\u6269\u5c55\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u8bd1C\u4ee3\u7801\uff0c\u751f\u6210Python\u53ef\u8c03\u7528\u7684\u5171\u4eab\u5e93\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>setup.py<\/code>\u811a\u672c\u6765\u7f16\u8bd1C\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, Extension<\/p>\n<p>module = Extension(&#39;myModule&#39;, sources=[&#39;my_module.c&#39;])<\/p>\n<p>setup(name=&#39;myModule&#39;,<\/p>\n<p>      version=&#39;1.0&#39;,<\/p>\n<p>      description=&#39;My custom C extension module&#39;,<\/p>\n<p>      ext_modules=[module])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u7f16\u8bd1C\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5728Python\u4e2d\u8c03\u7528C\u6269\u5c55\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u8c03\u7528C\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import myModule<\/p>\n<p>result = myModule.add(3, 5)<\/p>\n<p>print(f&#39;Result: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u8ba1\u7b97\uff0c\u5e76\u4e14\u80fd\u591f\u7ed5\u8fc7GIL\u9501\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5176\u4ed6\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>\u9664\u4e86CPython\uff0cPython\u8fd8\u6709\u5176\u4ed6\u89e3\u91ca\u5668\uff0c\u5982Jython\u3001IronPython\u548cPyPy\u3002\u8fd9\u4e9b\u89e3\u91ca\u5668\u6709\u5404\u81ea\u7684\u7279\u70b9\u548c\u4f18\u52bf\uff0c\u5176\u4e2d\u4e00\u4e9b\u89e3\u91ca\u5668\u4e0d\u53d7GIL\u9501\u7684\u9650\u5236\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Jython<\/h4>\n<\/p>\n<p><p>Jython\u662fPython\u8bed\u8a00\u7684Java\u5b9e\u73b0\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728Java\u865a\u62df\u673a\uff08JVM\uff09\u4e0a\u8fd0\u884cPython\u4ee3\u7801\u3002\u7531\u4e8eJython\u6ca1\u6709GIL\u9501\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b9e\u73b0\u771f\u6b63\u7684\u591a\u7ebf\u7a0b\u5e76\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>2\u3001IronPython<\/h4>\n<\/p>\n<p><p>IronPython\u662fPython\u8bed\u8a00\u7684.NET\u5b9e\u73b0\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728.NET\u6846\u67b6\u4e0a\u8fd0\u884cPython\u4ee3\u7801\u3002IronPython\u4e5f\u6ca1\u6709GIL\u9501\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b9e\u73b0\u771f\u6b63\u7684\u591a\u7ebf\u7a0b\u5e76\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>3\u3001PyPy<\/h4>\n<\/p>\n<p><p>PyPy\u662fPython\u8bed\u8a00\u7684\u53e6\u4e00\u79cd\u5b9e\u73b0\uff0c\u5b83\u4f7f\u7528Just-In-Time\uff08JIT\uff09\u7f16\u8bd1\u6280\u672f\u6765\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002\u867d\u7136PyPy\u4ecd\u7136\u6709GIL\u9501\uff0c\u4f46\u5b83\u7684JIT\u7f16\u8bd1\u5668\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5355\u7ebf\u7a0b\u6027\u80fd\uff0c\u56e0\u6b64\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u51cf\u8f7bGIL\u9501\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5f02\u6b65\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u901a\u8fc7\u5f02\u6b65I\/O\u64cd\u4f5c\u548c\u4e8b\u4ef6\u5faa\u73af\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u3002Python\u7684<code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f02\u6b65\u7f16\u7a0b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>asyncio<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u5757\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u6b65\u7f16\u7a0b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def worker(num):<\/p>\n<p>    print(f&#39;Worker: {num} started&#39;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(f&#39;Worker: {num} finished&#39;)<\/p>\n<p>async def main():<\/p>\n<p>    tasks = [worker(i) for i in range(5)]<\/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\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570<code>worker<\/code>\uff0c\u5b83\u4f1a\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u6682\u505c1\u79d2\u949f\u3002\u6211\u4eec\u4f7f\u7528<code>asyncio.gather<\/code>\u6765\u5e76\u53d1\u5730\u8fd0\u884c\u591a\u4e2a<code>worker<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5f02\u6b65I\/O\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5f02\u6b65\u7f16\u7a0b\u7684\u4e00\u4e2a\u91cd\u8981\u5e94\u7528\u662f\u5f02\u6b65I\/O\u64cd\u4f5c\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u548c\u6587\u4ef6\u8bfb\u53d6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>aiohttp<\/code>\u5e93\u8fdb\u884c\u5f02\u6b65HTTP\u8bf7\u6c42\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import aiohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch(url):<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        async with session.get(url) as response:<\/p>\n<p>            return await response.text()<\/p>\n<p>async def main():<\/p>\n<p>    urls = [&#39;https:\/\/www.python.org&#39;, &#39;https:\/\/www.google.com&#39;, &#39;https:\/\/www.github.com&#39;]<\/p>\n<p>    tasks = [fetch(url) for url in urls]<\/p>\n<p>    responses = await asyncio.gather(*tasks)<\/p>\n<p>    for response in responses:<\/p>\n<p>        print(response[:100])<\/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<code>aiohttp<\/code>\u5e93\u6765\u8fdb\u884c\u5f02\u6b65HTTP\u8bf7\u6c42\uff0c\u5e76\u4f7f\u7528<code>asyncio.gather<\/code>\u6765\u5e76\u53d1\u5730\u6267\u884c\u591a\u4e2a\u8bf7\u6c42\u3002\u901a\u8fc7\u5f02\u6b65\u7f16\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u7684\u540c\u65f6\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e76\u884c\u8ba1\u7b97\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>multiprocessing<\/code>\u6a21\u5757\uff0cPython\u8fd8\u6709\u5176\u4ed6\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u5982<code>joblib<\/code>\u3001<code>dask<\/code>\u548c<code>concurrent.futures<\/code>\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u5e76\u884c\u8ba1\u7b97\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>concurrent.futures<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u5e76\u884c\u8ba1\u7b97\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u6c60\u548c\u8fdb\u7a0b\u6c60\u7684\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>def worker(num):<\/p>\n<p>    return f&#39;Worker: {num}&#39;<\/p>\n<p>with concurrent.futures.ProcessPoolExecutor() as executor:<\/p>\n<p>    results = list(executor.map(worker, range(5)))<\/p>\n<p>for result in results:<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ProcessPoolExecutor<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u8fdb\u7a0b\u6c60\uff0c\u5e76\u4f7f\u7528<code>executor.map<\/code>\u5e76\u884c\u5730\u6267\u884c<code>worker<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>joblib<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>joblib<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5e76\u884c\u8ba1\u7b97\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u5e76\u884c\u8ba1\u7b97\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>joblib<\/code>\u5e93\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from joblib import Parallel, delayed<\/p>\n<p>def worker(num):<\/p>\n<p>    return f&#39;Worker: {num}&#39;<\/p>\n<p>results = Parallel(n_jobs=5)(delayed(worker)(i) for i in range(5))<\/p>\n<p>for result in results:<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Parallel<\/code>\u5bf9\u8c61\u521b\u5efa\u4e86\u4e00\u4e2a\u5e76\u884c\u8ba1\u7b97\u4efb\u52a1\uff0c\u5e76\u4f7f\u7528<code>delayed<\/code>\u51fd\u6570\u5305\u88c5<code>worker<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u89e3\u51b3GIL\u9501\u95ee\u9898\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u591a\u8fdb\u7a0b\u3001\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\u3001\u4f7f\u7528\u5176\u4ed6\u89e3\u91ca\u5668\u3001\u5f02\u6b65\u7f16\u7a0b\u548c\u5e76\u884c\u8ba1\u7b97\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<strong>\u591a\u8fdb\u7a0b\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\u6765\u7ed5\u8fc7GIL\u9501\uff0c\u4ece\u800c\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u8ba1\u7b97\uff0c\u5e76\u4e14\u80fd\u591f\u7ed5\u8fc7GIL\u9501\u3002\u4f7f\u7528\u5176\u4ed6\u89e3\u91ca\u5668\u5982Jython\u548cIronPython\u53ef\u4ee5\u5b9e\u73b0\u771f\u6b63\u7684\u591a\u7ebf\u7a0b\u5e76\u884c\u8ba1\u7b97\u3002\u5f02\u6b65\u7f16\u7a0b\u901a\u8fc7\u5f02\u6b65I\/O\u64cd\u4f5c\u548c\u4e8b\u4ef6\u5faa\u73af\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u3002\u5e76\u884c\u8ba1\u7b97\u5e93\u5982<code>concurrent.futures<\/code>\u548c<code>joblib<\/code>\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u5e76\u884c\u8ba1\u7b97\u63a5\u53e3\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3Python\u4e2d\u7684GIL\u9501\u95ee\u9898\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\u7406\u89e3GIL\u9501\u5728Python\u4e2d\u7684\u4f5c\u7528\uff1f<\/strong><br \/>GIL\uff0c\u5168\u79f0\u4e3a\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff0c\u662fPython\u89e3\u91ca\u5668\u7684\u4e00\u9879\u673a\u5236\uff0c\u786e\u4fdd\u5728\u4efb\u610f\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884cPython\u5b57\u8282\u7801\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0cPython\u7a0b\u5e8f\u4ecd\u7136\u4f1a\u5728\u6267\u884c\u65f6\u53d7\u5230GIL\u7684\u9650\u5236\u3002\u8fd9\u79cd\u8bbe\u8ba1\u4e3b\u8981\u662f\u4e3a\u4e86\u7b80\u5316\u5185\u5b58\u7ba1\u7406\uff0c\u907f\u514d\u591a\u7ebf\u7a0b\u95f4\u7684\u7ade\u4e89\u6761\u4ef6\u3002\u7406\u89e3GIL\u7684\u4f5c\u7528\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u4f18\u5316\u591a\u7ebf\u7a0b\u5e94\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7ed5\u8fc7GIL\u9650\u5236\uff1f<\/strong><br \/>\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u7ed5\u8fc7GIL\u7684\u9650\u5236\u3002\u4f7f\u7528\u591a\u8fdb\u7a0b\u800c\u975e\u591a\u7ebf\u7a0b\u662f\u4e00\u79cd\u5e38\u89c1\u7b56\u7565\uff0cPython\u7684<code>multiprocessing<\/code>\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684Python\u89e3\u91ca\u5668\u548c\u5185\u5b58\u7a7a\u95f4\u3002\u6b64\u5916\uff0c\u4f7f\u7528C\u6269\u5c55\u6216\u7b2c\u4e09\u65b9\u5e93\uff08\u5982NumPy\uff09\u6765\u6267\u884c\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u5728C\u5c42\u9762\u8fdb\u884c\u4f18\u5316\uff0c\u4ece\u800c\u7ed5\u8fc7GIL\u9650\u5236\u3002\u8fd9\u6837\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u7684\u4f18\u52bf\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u591a\u7ebf\u7a0b\u65f6\uff0c\u5982\u4f55\u63d0\u9ad8Python\u7a0b\u5e8f\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u63d0\u9ad8Python\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6027\u80fd\u7684\u5173\u952e\u5728\u4e8e\u9009\u62e9\u5408\u9002\u7684\u4efb\u52a1\u3002\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff08\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u53d6\u7b49\uff09\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u6027\u80fd\uff0c\u56e0\u4e3a\u7ebf\u7a0b\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u65f6\u53ef\u4ee5\u91ca\u653eGIL\u3002\u7136\u800c\uff0c\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5219\u5efa\u8bae\u4f7f\u7528\u591a\u8fdb\u7a0b\u6216\u8fdb\u884c\u4ee3\u7801\u4f18\u5316\uff0c\u4ee5\u51cf\u5c0fGIL\u5e26\u6765\u7684\u5f71\u54cd\u3002\u6b64\u5916\uff0c\u5408\u7406\u4f7f\u7528\u7ebf\u7a0b\u6c60\u548c\u961f\u5217\u53ef\u4ee5\u63d0\u5347\u8d44\u6e90\u4f7f\u7528\u6548\u7387\uff0c\u907f\u514d\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u5e26\u6765\u7684\u5f00\u9500\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\u3001\u4f7f\u7528\u5176\u4ed6\u89e3\u91ca\u5668\u7b49\u65b9\u5f0f\u6765\u89e3\u51b3GIL\u9501\u95ee\u9898\u3002\u5176\u4e2d\uff0c\u591a\u8fdb\u7a0b\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1187478,"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\/1187472"}],"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=1187472"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187472\/revisions"}],"predecessor-version":[{"id":1187479,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187472\/revisions\/1187479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187478"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}