{"id":1055261,"date":"2024-12-31T14:49:11","date_gmt":"2024-12-31T06:49:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1055261.html"},"modified":"2024-12-31T14:49:13","modified_gmt":"2024-12-31T06:49:13","slug":"%e5%a6%82%e4%bd%95c-%e6%8e%a7%e4%bb%b6%e7%9a%84%e5%80%bc%e4%bc%a0%e9%80%92%e7%bb%99python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1055261.html","title":{"rendered":"\u5982\u4f55c \u63a7\u4ef6\u7684\u503c\u4f20\u9012\u7ed9python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0a78743e-6922-45c4-8c9e-b1863ec19a31.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55c \u63a7\u4ef6\u7684\u503c\u4f20\u9012\u7ed9python\" \/><\/p>\n<p><p> <strong>\u5728C\u8bed\u8a00\u4e2d\u5c06\u63a7\u4ef6\u7684\u503c\u4f20\u9012\u7ed9Python\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0<\/strong>\uff1a\u4f7f\u7528\u6587\u4ef6\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u3001\u4f7f\u7528\u7f51\u7edc\u901a\u4fe1\u3001\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u3002<strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u6587\u4ef6\u7cfb\u7edf\u5b9e\u73b0\u6570\u636e\u4f20\u9012\uff0c\u9002\u7528\u4e8e\u6570\u636e\u91cf\u4e0d\u5927\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001C\u4ee3\u7801\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728C\u4ee3\u7801\u4e2d\uff0c\u5c06\u63a7\u4ef6\u7684\u503c\u5199\u5165\u4e00\u4e2a\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>void write_value_to_file(const char* filename, const char* value) {<\/p>\n<p>    FILE* file = fopen(filename, &quot;w&quot;);<\/p>\n<p>    if (file == NULL) {<\/p>\n<p>        perror(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to open file&quot;);<\/p>\n<p>        return;<\/p>\n<p>    }<\/p>\n<p>    fprintf(file, &quot;%s&quot;, value);<\/p>\n<p>    fclose(file);<\/p>\n<p>}<\/p>\n<p>int main() {<\/p>\n<p>    const char* value = &quot;Hello, Python!&quot;;<\/p>\n<p>    write_value_to_file(&quot;value.txt&quot;, value);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Python\u4ee3\u7801\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u8bfb\u53d6\u8be5\u6587\u4ef6\u4ee5\u83b7\u53d6\u63a7\u4ef6\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_value_from_file(filename):<\/p>\n<p>    with open(filename, &quot;r&quot;) as file:<\/p>\n<p>        value = file.read()<\/p>\n<p>    return value<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    value = read_value_from_file(&quot;value.txt&quot;)<\/p>\n<p>    print(f&quot;The value from C is: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u6d41\u5b9e\u73b0\u6570\u636e\u4f20\u9012\uff0c\u9002\u7528\u4e8e\u5b9e\u65f6\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001C\u4ee3\u7801\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728C\u4ee3\u7801\u4e2d\uff0c\u5c06\u63a7\u4ef6\u7684\u503c\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    const char* value = &quot;Hello, Python!&quot;;<\/p>\n<p>    printf(&quot;%s&quot;, value);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Python\u4ee3\u7801\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5b50\u8fdb\u7a0b\u8bfb\u53d6\u6807\u51c6\u8f93\u51fa\u4ee5\u83b7\u53d6\u63a7\u4ef6\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def get_value_from_c():<\/p>\n<p>    result = subprocess.run([&quot;.\/your_c_program&quot;], capture_output=True, text=True)<\/p>\n<p>    return result.stdout.strip()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    value = get_value_from_c()<\/p>\n<p>    print(f&quot;The value from C is: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7f51\u7edc\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u7f51\u7edc\u901a\u4fe1\u5b9e\u73b0\u6570\u636e\u4f20\u9012\uff0c\u9002\u7528\u4e8e\u5206\u5e03\u5f0f\u7cfb\u7edf\u548c\u8de8\u5e73\u53f0\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001C\u4ee3\u7801\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728C\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5957\u63a5\u5b57\u5c06\u63a7\u4ef6\u7684\u503c\u53d1\u9001\u5230Python\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>#include &lt;string.h&gt;<\/p>\n<p>#include &lt;arpa\/inet.h&gt;<\/p>\n<p>void send_value(const char* value) {<\/p>\n<p>    int sockfd = socket(AF_INET, SOCK_STREAM, 0);<\/p>\n<p>    if (sockfd &lt; 0) {<\/p>\n<p>        perror(&quot;Socket creation failed&quot;);<\/p>\n<p>        return;<\/p>\n<p>    }<\/p>\n<p>    struct sockaddr_in serv_addr;<\/p>\n<p>    memset(&amp;serv_addr, 0, sizeof(serv_addr));<\/p>\n<p>    serv_addr.sin_family = AF_INET;<\/p>\n<p>    serv_addr.sin_port = htons(8080);<\/p>\n<p>    inet_pton(AF_INET, &quot;127.0.0.1&quot;, &amp;serv_addr.sin_addr);<\/p>\n<p>    if (connect(sockfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {<\/p>\n<p>        perror(&quot;Connection failed&quot;);<\/p>\n<p>        return;<\/p>\n<p>    }<\/p>\n<p>    send(sockfd, value, strlen(value), 0);<\/p>\n<p>    close(sockfd);<\/p>\n<p>}<\/p>\n<p>int main() {<\/p>\n<p>    const char* value = &quot;Hello, Python!&quot;;<\/p>\n<p>    send_value(value);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Python\u4ee3\u7801\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5957\u63a5\u5b57\u63a5\u6536\u63a7\u4ef6\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def receive_value():<\/p>\n<p>    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>    server_socket.bind((&quot;127.0.0.1&quot;, 8080))<\/p>\n<p>    server_socket.listen(1)<\/p>\n<p>    conn, addr = server_socket.accept()<\/p>\n<p>    value = conn.recv(1024).decode()<\/p>\n<p>    conn.close()<\/p>\n<p>    return value<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    value = receive_value()<\/p>\n<p>    print(f&quot;The value from C is: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5171\u4eab\u5185\u5b58<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u5b9e\u73b0\u6570\u636e\u4f20\u9012\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u6570\u636e\u4f20\u8f93\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001C\u4ee3\u7801\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728C\u4ee3\u7801\u4e2d\uff0c\u521b\u5efa\u5171\u4eab\u5185\u5b58\u5e76\u5199\u5165\u63a7\u4ef6\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>#include &lt;sys\/ipc.h&gt;<\/p>\n<p>#include &lt;sys\/shm.h&gt;<\/p>\n<p>#include &lt;string.h&gt;<\/p>\n<p>void write_value_to_shared_memory(const char* value) {<\/p>\n<p>    key_t key = 1234;<\/p>\n<p>    int shmid = shmget(key, 1024, 0666|IPC_CREAT);<\/p>\n<p>    if (shmid &lt; 0) {<\/p>\n<p>        perror(&quot;shmget failed&quot;);<\/p>\n<p>        return;<\/p>\n<p>    }<\/p>\n<p>    char* str = (char*) shmat(shmid, (void*)0, 0);<\/p>\n<p>    strcpy(str, value);<\/p>\n<p>    shmdt(str);<\/p>\n<p>}<\/p>\n<p>int main() {<\/p>\n<p>    const char* value = &quot;Hello, Python!&quot;;<\/p>\n<p>    write_value_to_shared_memory(value);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Python\u4ee3\u7801\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u8bfb\u53d6\u5171\u4eab\u5185\u5b58\u4ee5\u83b7\u53d6\u63a7\u4ef6\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sysv_ipc<\/p>\n<p>def read_value_from_shared_memory():<\/p>\n<p>    key = 1234<\/p>\n<p>    memory = sysv_ipc.SharedMemory(key)<\/p>\n<p>    value = memory.read().decode().strip(&#39;\\x00&#39;)<\/p>\n<p>    return value<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    value = read_value_from_shared_memory()<\/p>\n<p>    print(f&quot;The value from C is: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0C\u8bed\u8a00\u4e2d\u63a7\u4ef6\u503c\u4f20\u9012\u7ed9Python\u7684\u9700\u6c42\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5373\u53ef\u3002\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u548c\u6587\u4ef6\u7684\u65b9\u6cd5\u8f83\u4e3a\u7b80\u5355\uff0c\u9002\u7528\u4e8e\u4e00\u822c\u9700\u6c42\uff1b\u800c\u7f51\u7edc\u901a\u4fe1\u548c\u5171\u4eab\u5185\u5b58\u65b9\u6cd5\u9002\u7528\u4e8e\u66f4\u590d\u6742\u548c\u9ad8\u6548\u7684\u6570\u636e\u4f20\u8f93\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728C\u8bed\u8a00\u4e2d\u8c03\u7528Python\u4ee3\u7801\u4ee5\u4f20\u9012\u63a7\u4ef6\u7684\u503c\uff1f<\/strong><br \/>\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Python\/C API\u6765\u8c03\u7528Python\u4ee3\u7801\u5e76\u4f20\u9012\u63a7\u4ef6\u7684\u503c\u3002\u60a8\u9700\u8981\u5305\u542bPython.h\u5934\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Py_Initialize()\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528PyObject\u6765\u521b\u5efaPython\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7PyObject_SetAttrString()\u6216PyObject_CallObject()\u7b49\u65b9\u6cd5\u6765\u4f20\u9012\u503c\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528C\u8bed\u8a00\u4e0ePython\u4ea4\u4e92\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u786e\u4fddPython\u73af\u5883\u5df2\u7ecf\u6b63\u786e\u8bbe\u7f6e\uff0c\u5e76\u4e14C\u7a0b\u5e8f\u80fd\u591f\u627e\u5230Python\u7684\u5e93\u548c\u5934\u6587\u4ef6\u3002\u8981\u6ce8\u610f\u5185\u5b58\u7ba1\u7406\uff0c\u5c24\u5176\u662f\u5f53\u5728C\u4e2d\u521b\u5efaPython\u5bf9\u8c61\u65f6\uff0c\u9700\u8981\u9002\u65f6\u8c03\u7528Py_DECREF\u6765\u91ca\u653e\u5bf9\u8c61\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u9732\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u7684\u6b63\u786e\u8f6c\u6362\uff0c\u4ee5\u9632\u5728\u4f20\u9012\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528C\u8bed\u8a00\u4f20\u9012\u6570\u636e\u7ed9Python\u65f6\uff0c\u662f\u5426\u6709\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u53c2\u8003\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u53c2\u8003\u5982\u4e0b\u7b80\u5355\u793a\u4f8b\u3002\u5047\u8bbe\u60a8\u5df2\u7ecf\u521d\u59cb\u5316\u4e86Python\u73af\u5883\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u4e00\u4e2a\u6574\u6570\u503c\u4eceC\u4f20\u9012\u7ed9Python\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-c\">#include &lt;Python.h&gt;\n\nint main() {\n    Py_Initialize();\n    PyObject *<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/52627.html\" target=\"_blank\">PMO<\/a>dule = PyImport_ImportModule(&quot;your_python_script&quot;);\n    PyObject *pFunc = PyObject_GetAttrString(pModule, &quot;your_function&quot;);\n\n    int value = 42;\n    PyObject *pValue = PyLong_FromLong(value);\n    PyObject_CallObject(pFunc, pValue);\n\n    Py_DECREF(pValue);\n    Py_DECREF(pFunc);\n    Py_DECREF(pModule);\n    Py_Finalize();\n    return 0;\n}\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5c06C\u4e2d\u7684\u6574\u578b\u503c\u4f20\u9012\u7ed9Python\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C\u8bed\u8a00\u4e2d\u5c06\u63a7\u4ef6\u7684\u503c\u4f20\u9012\u7ed9Python\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528\u6587\u4ef6\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u3001\u4f7f\u7528\u7f51\u7edc\u901a\u4fe1\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1055269,"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\/1055261"}],"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=1055261"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1055261\/revisions"}],"predecessor-version":[{"id":1055272,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1055261\/revisions\/1055272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1055269"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1055261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1055261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1055261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}