{"id":1069656,"date":"2025-01-08T10:51:03","date_gmt":"2025-01-08T02:51:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069656.html"},"modified":"2025-01-08T10:51:05","modified_gmt":"2025-01-08T02:51:05","slug":"%e5%a6%82%e4%bd%95%e7%94%a8c%e5%ae%9e%e7%8e%b0python%e7%9a%84%e5%a4%9a%e7%ba%bf%e7%a8%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069656.html","title":{"rendered":"\u5982\u4f55\u7528C\u5b9e\u73b0python\u7684\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\/25100623\/c6db8907-f545-4311-948b-a7070aeff27f.webp\" alt=\"\u5982\u4f55\u7528C\u5b9e\u73b0python\u7684\u591a\u7ebf\u7a0b\" \/><\/p>\n<p><p> \u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0Python\u7684\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u7684C API\u6765\u5d4c\u5165Python\u89e3\u91ca\u5668\uff0c\u5e76\u4f7f\u7528Python\u7684\u591a\u7ebf\u7a0b\u6a21\u5757\uff08\u5982<code>threading<\/code>\u6a21\u5757\uff09\u8fdb\u884c\u64cd\u4f5c\u3002<strong>\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3001\u7f16\u5199Python\u4ee3\u7801\u5e76\u6267\u884c\u3001\u7ba1\u7406GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09\u3001\u5904\u7406\u7ebf\u7a0b\u5b89\u5168\u7b49<\/strong>\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5728C\u4ee3\u7801\u4e2d\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3002\u8fd9\u4e2a\u6b65\u9aa4\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u4e3a\u63a5\u4e0b\u6765\u7684Python\u4ee3\u7801\u6267\u884c\u63d0\u4f9b\u4e86\u8fd0\u884c\u73af\u5883\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(int argc, char *argv[]) {<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ Your Python code will be executed here<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199Python\u4ee3\u7801\u5e76\u6267\u884c<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u7f16\u5199Python\u4ee3\u7801\uff0c\u5e76\u901a\u8fc7C\u4ee3\u7801\u6765\u6267\u884c\u8fd9\u4e9bPython\u4ee3\u7801\u3002\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u4ee3\u7801\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>const char *python_code = <\/p>\n<p>&quot;import threading\\n&quot;<\/p>\n<p>&quot;import time\\n&quot;<\/p>\n<p>&quot;\\n&quot;<\/p>\n<p>&quot;def thread_function(name):\\n&quot;<\/p>\n<p>&quot;    print(f&#39;Thread {name} starting&#39;)\\n&quot;<\/p>\n<p>&quot;    time.sleep(2)\\n&quot;<\/p>\n<p>&quot;    print(f&#39;Thread {name} finishing&#39;)\\n&quot;<\/p>\n<p>&quot;\\n&quot;<\/p>\n<p>&quot;threads = []\\n&quot;<\/p>\n<p>&quot;for i in range(3):\\n&quot;<\/p>\n<p>&quot;    thread = threading.Thread(target=thread_function, args=(i,))\\n&quot;<\/p>\n<p>&quot;    threads.append(thread)\\n&quot;<\/p>\n<p>&quot;    thread.start()\\n&quot;<\/p>\n<p>&quot;\\n&quot;<\/p>\n<p>&quot;for thread in threads:\\n&quot;<\/p>\n<p>&quot;    thread.join()\\n&quot;;<\/p>\n<p>int main(int argc, char *argv[]) {<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    PyRun_SimpleString(python_code);<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ba1\u7406GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u7684\u591a\u7ebf\u7a0b\u65f6\uff0c\u5fc5\u987b\u7ba1\u7406\u597dGIL\uff0c\u56e0\u4e3aPython\u7684C API\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002Python\u89e3\u91ca\u5668\u4f7f\u7528GIL\u6765\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u591f\u6267\u884cPython\u5b57\u8282\u7801\u3002\u4ee5\u4e0b\u662f\u5728C\u4ee3\u7801\u4e2d\u5904\u7406GIL\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>void *thread_function(void *arg) {<\/p>\n<p>    PyGILState_STATE gstate;<\/p>\n<p>    gstate = PyGILState_Ensure();<\/p>\n<p>    PyRun_SimpleString(&quot;print(&#39;Hello from thread!&#39;)&quot;);<\/p>\n<p>    PyGILState_Release(gstate);<\/p>\n<p>    return NULL;<\/p>\n<p>}<\/p>\n<p>int main(int argc, char *argv[]) {<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    PyEval_InitThreads();<\/p>\n<p>    PyThreadState *mainThreadState = PyEval_SaveThread();<\/p>\n<p>    pthread_t thread;<\/p>\n<p>    pthread_create(&amp;thread, NULL, thread_function, NULL);<\/p>\n<p>    pthread_join(thread, NULL);<\/p>\n<p>    PyEval_RestoreThread(mainThreadState);<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u7ebf\u7a0b\u5b89\u5168<\/h3>\n<\/p>\n<p><p>\u5728\u5d4c\u5165Python\u89e3\u91ca\u5668\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u6240\u6709\u5bf9Python\u5bf9\u8c61\u7684\u64cd\u4f5c\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u8fd9\u9700\u8981\u5728\u9002\u5f53\u7684\u5730\u65b9\u52a0\u9501\u548c\u89e3\u9501GIL\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u9501\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>#include &lt;pthread.h&gt;<\/p>\n<p>pthread_mutex_t lock;<\/p>\n<p>void *thread_function(void *arg) {<\/p>\n<p>    pthread_mutex_lock(&amp;lock);<\/p>\n<p>    PyGILState_STATE gstate;<\/p>\n<p>    gstate = PyGILState_Ensure();<\/p>\n<p>    PyRun_SimpleString(&quot;print(&#39;Hello from thread with lock!&#39;)&quot;);<\/p>\n<p>    PyGILState_Release(gstate);<\/p>\n<p>    pthread_mutex_unlock(&amp;lock);<\/p>\n<p>    return NULL;<\/p>\n<p>}<\/p>\n<p>int main(int argc, char *argv[]) {<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    PyEval_InitThreads();<\/p>\n<p>    PyThreadState *mainThreadState = PyEval_SaveThread();<\/p>\n<p>    pthread_mutex_init(&amp;lock, NULL);<\/p>\n<p>    pthread_t thread;<\/p>\n<p>    pthread_create(&amp;thread, NULL, thread_function, NULL);<\/p>\n<p>    pthread_join(thread, NULL);<\/p>\n<p>    pthread_mutex_destroy(&amp;lock);<\/p>\n<p>    PyEval_RestoreThread(mainThreadState);<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528C\u5b9e\u73b0Python\u591a\u7ebf\u7a0b\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7ba1\u7406GIL<\/strong>\uff1a\u4efb\u4f55\u65f6\u5019\u8c03\u7528Python C API\u65f6\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u6301\u6709GIL\u3002<\/li>\n<li><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u5bf9Python\u5bf9\u8c61\u7684\u64cd\u4f5c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li>\n<li><strong>\u8d44\u6e90\u91ca\u653e<\/strong>\uff1a\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u786e\u4fdd\u6b63\u786e\u91ca\u653e\u6240\u6709\u8d44\u6e90\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u8981\u7279\u522b\u6ce8\u610f\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u9632\u6b62\u672a\u5904\u7406\u7684\u5f02\u5e38\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u5b8c\u6574\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684C\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5d4c\u5165Python\u89e3\u91ca\u5668\u5e76\u4f7f\u7528Python\u7684\u591a\u7ebf\u7a0b\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>#include &lt;pthread.h&gt;<\/p>\n<p>void *thread_function(void *arg) {<\/p>\n<p>    PyGILState_STATE gstate;<\/p>\n<p>    gstate = PyGILState_Ensure();<\/p>\n<p>    PyRun_SimpleString(&quot;print(&#39;Hello from thread!&#39;)&quot;);<\/p>\n<p>    PyGILState_Release(gstate);<\/p>\n<p>    return NULL;<\/p>\n<p>}<\/p>\n<p>int main(int argc, char *argv[]) {<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    PyEval_InitThreads();<\/p>\n<p>    PyThreadState *mainThreadState = PyEval_SaveThread();<\/p>\n<p>    pthread_t threads[5];<\/p>\n<p>    for (int i = 0; i &lt; 5; i++) {<\/p>\n<p>        pthread_create(&amp;threads[i], NULL, thread_function, NULL);<\/p>\n<p>    }<\/p>\n<p>    for (int i = 0; i &lt; 5; i++) {<\/p>\n<p>        pthread_join(threads[i], NULL);<\/p>\n<p>    }<\/p>\n<p>    PyEval_RestoreThread(mainThreadState);<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728C\u8bed\u8a00\u4e2d\u6210\u529f\u5b9e\u73b0Python\u7684\u591a\u7ebf\u7a0b\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6d89\u53ca\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3001\u7f16\u5199\u5e76\u6267\u884cPython\u4ee3\u7801\u3001\u7ba1\u7406GIL\u548c\u5904\u7406\u7ebf\u7a0b\u5b89\u5168\u7b49\u591a\u4e2a\u65b9\u9762\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u591a\u7ebf\u7a0b\u5728C\u8bed\u8a00\u4e2d\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/strong><br \/>C\u8bed\u8a00\u7684\u591a\u7ebf\u7a0b\u5b9e\u73b0\u901a\u5e38\u4f9d\u8d56\u4e8epthread\u5e93\uff08POSIX\u7ebf\u7a0b\uff09\u3002\u901a\u8fc7\u521b\u5efa\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0ePython\u591a\u7ebf\u7a0b\u76f8\u4f3c\u7684\u5e76\u884c\u5904\u7406\u3002\u4f7f\u7528pthread_create\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\uff0c\u800cpthread_join\u51fd\u6570\u5219\u7528\u4e8e\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\uff0c\u786e\u4fdd\u4e3b\u7a0b\u5e8f\u5728\u6240\u6709\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\u518d\u7ec8\u6b62\u3002<\/p>\n<p><strong>C\u8bed\u8a00\u4e2d\u7684\u591a\u7ebf\u7a0b\u5982\u4f55\u4e0ePython\u4e2d\u7684\u591a\u7ebf\u7a0b\u8fdb\u884c\u6bd4\u8f83\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u591a\u7ebf\u7a0b\u901a\u5e38\u53d7\u9650\u4e8eGIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09\uff0c\u8fd9\u4f7f\u5f97\u591a\u7ebf\u7a0b\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u6548\u679c\u4e0d\u4f73\u3002\u800cC\u8bed\u8a00\u7684\u591a\u7ebf\u7a0b\u5b9e\u73b0\u5219\u4e0d\u53d7\u6b64\u9650\u5236\uff0c\u5141\u8bb8\u771f\u6b63\u7684\u5e76\u884c\u5904\u7406\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u9ad8\u6027\u80fd\u548c\u591a\u6838\u5229\u7528\u7684\u573a\u666f\u4e0b\uff0cC\u8bed\u8a00\u7684\u591a\u7ebf\u7a0b\u5b9e\u73b0\u901a\u5e38\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5728C\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u7ade\u4e89\u6761\u4ef6\u3001\u6b7b\u9501\u548c\u8d44\u6e90\u6cc4\u9732\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u5f00\u53d1\u8005\u5e94\u8be5\u4f7f\u7528\u9002\u5f53\u7684\u540c\u6b65\u673a\u5236\uff0c\u5982\u4e92\u65a5\u9501\uff08mutex\uff09\u548c\u6761\u4ef6\u53d8\u91cf\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u6b63\u786e\u91ca\u653e\u5176\u4f7f\u7528\u7684\u8d44\u6e90\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u9732\u548c\u5176\u4ed6\u6f5c\u5728\u95ee\u9898\uff0c\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0Python\u7684\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u7684C API\u6765\u5d4c\u5165Python\u89e3\u91ca\u5668\uff0c\u5e76\u4f7f\u7528Pytho [&hellip;]","protected":false},"author":3,"featured_media":1069666,"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\/1069656"}],"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=1069656"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069656\/revisions"}],"predecessor-version":[{"id":1069669,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069656\/revisions\/1069669"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069666"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}