{"id":922332,"date":"2024-12-26T14:29:31","date_gmt":"2024-12-26T06:29:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/922332.html"},"modified":"2024-12-26T14:29:33","modified_gmt":"2024-12-26T06:29:33","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8c%e8%af%ad%e8%a8%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/922332.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528c\u8bed\u8a00"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210853\/1161a711-41b1-42df-b7e4-1bc1d5fcf2ae.webp\" alt=\"python\u5982\u4f55\u8c03\u7528c\u8bed\u8a00\" \/><\/p>\n<p><p> Python\u8c03\u7528C\u8bed\u8a00\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a<strong>\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\u3001\u5229\u7528ctypes\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7Cython\u5de5\u5177<\/strong>\u3002\u5176\u4e2d\uff0cC\u6269\u5c55\u6a21\u5757\u901a\u5e38\u7528\u4e8e\u6027\u80fd\u4f18\u5316\u548c\u4e0e\u73b0\u6709C\u4ee3\u7801\u5e93\u7684\u96c6\u6210\uff0cctypes\u5e93\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u52a8\u6001\u8c03\u7528C\u5e93\u7684\u65b9\u5f0f\uff0c\u800cCython\u662f\u4e00\u79cd\u7ed3\u5408\u4e86C\u548cPython\u4f18\u52bf\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u80fd\u591f\u7f16\u8bd1\u4e3aC\u4ee3\u7801\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528C\u6269\u5c55\u6a21\u5757<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n<\/p>\n<p><p>C\u6269\u5c55\u6a21\u5757\u662fPython\u4e0eC\u8bed\u8a00\u4ea4\u4e92\u7684\u4f20\u7edf\u65b9\u5f0f\u3002\u901a\u8fc7\u7f16\u5199C\u4ee3\u7801\u5e76\u7f16\u8bd1\u6210\u5171\u4eab\u5e93\uff0cPython\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199C\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u7f16\u5199\u4e00\u4e2aC\u8bed\u8a00\u6587\u4ef6\uff0c\u5982<code>mymodule.c<\/code>\uff0c\u5e76\u5b9e\u73b0\u4f60\u5e0c\u671bPython\u8c03\u7528\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684C\u51fd\u6570<\/p>\n<p>static PyObject* my_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 PyLong_FromLong(a + b);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49\u6a21\u5757\u7684\u65b9\u6cd5\u8868<\/p>\n<p>static PyMethodDef MyMethods[] = {<\/p>\n<p>    {&quot;add&quot;, my_add, METH_VARARGS, &quot;Add two numbers&quot;},<\/p>\n<p>    {NULL, NULL, 0, NULL}<\/p>\n<p>};<\/p>\n<p>\/\/ \u5b9a\u4e49\u6a21\u5757<\/p>\n<p>static struct PyModuleDef mymodule = {<\/p>\n<p>    PyModuleDef_HEAD_INIT,<\/p>\n<p>    &quot;mymodule&quot;,  \/\/ \u6a21\u5757\u540d<\/p>\n<p>    NULL,        \/\/ \u6a21\u5757\u6587\u6863<\/p>\n<p>    -1,<\/p>\n<p>    MyMethods<\/p>\n<p>};<\/p>\n<p>\/\/ \u521d\u59cb\u5316\u6a21\u5757<\/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<\/li>\n<li>\n<p><strong>\u7f16\u8bd1C\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684<code>distutils<\/code>\u6216<code>setuptools<\/code>\u5de5\u5177\u6765\u7f16\u8bd1C\u4ee3\u7801\u4e3a\u5171\u4eab\u5e93\u3002\u5728\u540c\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\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;mymodule.c&#39;])<\/p>\n<p>setup(name=&#39;MyModule&#39;,<\/p>\n<p>      version=&#39;1.0&#39;,<\/p>\n<p>      description=&#39;This is a demo package&#39;,<\/p>\n<p>      ext_modules=[module])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python setup.py build<\/p>\n<p>python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>result = mymodule.add(1, 2)<\/p>\n<p>print(result)  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528ctypes\u5e93<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n<\/p>\n<p><p><code>ctypes<\/code>\u662fPython\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u5141\u8bb8\u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\uff09\u6216\u5171\u4eab\u5e93\u4e2d\u7684C\u51fd\u6570\uff0c\u65e0\u9700\u7f16\u5199C\u6269\u5c55\u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u548c\u7f16\u8bd1C\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aC\u6587\u4ef6\uff0c\u5982<code>myfunctions.c<\/code>\uff0c\u7f16\u5199\u5e76\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ myfunctions.c<\/p>\n<p>int add(int a, int b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">gcc -shared -o myfunctions.so -fPIC myfunctions.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528ctypes<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>ctypes<\/code>\u5e93\u52a0\u8f7d\u5e76\u8c03\u7528\u5171\u4eab\u5e93\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>\u52a0\u8f7d\u5171\u4eab\u5e93<\/strong><\/h2>\n<p>myfunctions = ctypes.CDLL(&#39;.\/myfunctions.so&#39;)<\/p>\n<h2><strong>\u8c03\u7528add\u51fd\u6570<\/strong><\/h2>\n<p>result = myfunctions.add(3, 4)<\/p>\n<p>print(result)  # \u8f93\u51fa: 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8c03\u7528C\u51fd\u6570\u4e4b\u524d\uff0c\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">myfunctions.add.argtypes = (ctypes.c_int, ctypes.c_int)<\/p>\n<p>myfunctions.add.restype = ctypes.c_int<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528Cython<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n<\/p>\n<p><p>Cython\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u65e8\u5728\u4f7f\u7f16\u5199C\u6269\u5c55\u6a21\u5757\u7684\u8fc7\u7a0b\u66f4\u7b80\u5355\u3002Cython\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u4e3aC\u4ee3\u7801\uff0c\u4ece\u800c\u63d0\u4f9bPython\u548cC\u4ee3\u7801\u7684\u65e0\u7f1d\u96c6\u6210\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Cython\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a<code>.pyx<\/code>\u6587\u4ef6\uff0c\u5982<code>mymodule.pyx<\/code>\uff0c\u5e76\u7f16\u5199Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cython\">def add(int a, int b):<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u8bd1Cython\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>setuptools<\/code>\u548c<code>Cython<\/code>\u6269\u5c55\u5de5\u5177\u6765\u7f16\u8bd1Cython\u4ee3\u7801\u3002\u5728\u540c\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup<\/p>\n<p>from Cython.Build import cythonize<\/p>\n<p>setup(<\/p>\n<p>    ext_modules=cythonize(&quot;mymodule.pyx&quot;)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\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<\/li>\n<li>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528Cython\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>result = mymodule.add(5, 6)<\/p>\n<p>print(result)  # \u8f93\u51fa: 11<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4e0eC\u8bed\u8a00\u7684\u7ed3\u5408\u80fd\u591f\u5e26\u6765\u6027\u80fd\u4e0a\u7684\u663e\u8457\u63d0\u5347\uff0c\u540c\u65f6\u4e5f\u80fd\u5145\u5206\u5229\u7528\u73b0\u6709\u7684C\u4ee3\u7801\u5e93\u3002<strong>\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684C\u4ee3\u7801\u96c6\u6210\uff0c<code>ctypes<\/code>\u63d0\u4f9b\u4e86\u52a8\u6001\u8c03\u7528\u7684\u7075\u6d3b\u6027\uff0c\u800cCython\u5219\u7b80\u5316\u4e86\u7f16\u5199C\u6269\u5c55\u6a21\u5757\u7684\u6d41\u7a0b<\/strong>\u3002\u9009\u62e9\u4f55\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u5e94\u7528\u7684\u9700\u6c42\u3001\u6027\u80fd\u8981\u6c42\u4ee5\u53ca\u5f00\u53d1\u8005\u7684\u719f\u6089\u7a0b\u5ea6\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u7406\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u52a3\u52bf\u90fd\u6709\u52a9\u4e8e\u5728\u9879\u76ee\u4e2d\u505a\u51fa\u6700\u4f73\u51b3\u7b56\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0C\u8bed\u8a00\u7684\u51fd\u6570\u8c03\u7528\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8c03\u7528C\u8bed\u8a00\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684C\u6269\u5c55\u6216\u4f7f\u7528ctypes\u5e93\u6765\u5b9e\u73b0\u3002C\u6269\u5c55\u9700\u8981\u7f16\u5199C\u4ee3\u7801\u5e76\u5c06\u5176\u7f16\u8bd1\u4e3aPython\u6a21\u5757\uff0c\u800cctypes\u5e93\u53ef\u4ee5\u76f4\u63a5\u52a0\u8f7d\u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\u6216so\u6587\u4ef6\uff09\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u9879\u76ee\u7ed3\u6784\u3002<\/p>\n<p><strong>\u4f7f\u7528ctypes\u8c03\u7528C\u51fd\u6570\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528ctypes\u5e93\u65f6\uff0c\u9996\u5148\u9700\u8981\u7f16\u5199C\u4ee3\u7801\u5e76\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\u3002\u7136\u540e\u5728Python\u4e2d\u5bfc\u5165ctypes\uff0c\u4f7f\u7528<code>ctypes.CDLL<\/code>\u52a0\u8f7d\u5171\u4eab\u5e93\uff0c\u63a5\u7740\u53ef\u4ee5\u4f7f\u7528\u5e93\u4e2d\u7684\u51fd\u6570\u3002\u9700\u6ce8\u610f\u7684\u662f\uff0cC\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u9700\u8981\u6b63\u786e\u58f0\u660e\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5728Python\u548cC\u4e4b\u95f4\u6b63\u786e\u4f20\u9012\u3002<\/p>\n<p><strong>Python\u8c03\u7528C\u8bed\u8a00\u7684\u6027\u80fd\u4f18\u52bf\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u901a\u8fc7\u8c03\u7528C\u8bed\u8a00\u4ee3\u7801\uff0cPython\u53ef\u4ee5\u5728\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002C\u8bed\u8a00\u901a\u5e38\u6267\u884c\u901f\u5ea6\u66f4\u5feb\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5927\u91cf\u5faa\u73af\u6216\u590d\u6742\u8ba1\u7b97\u65f6\u3002\u5c06\u6027\u80fd\u654f\u611f\u7684\u90e8\u5206\u7528C\u5b9e\u73b0\uff0c\u53ef\u4ee5\u51cf\u5c11\u6574\u4f53\u8fd0\u884c\u65f6\u95f4\uff0c\u540c\u65f6\u4ecd\u7136\u80fd\u591f\u5229\u7528Python\u7684\u6613\u7528\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528C\u8bed\u8a00\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\u3001\u5229\u7528ctypes\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7Cyth [&hellip;]","protected":false},"author":3,"featured_media":922337,"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\/922332"}],"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=922332"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922332\/revisions"}],"predecessor-version":[{"id":922338,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922332\/revisions\/922338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/922337"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=922332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=922332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=922332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}