[英]executing c program from a python program
I'm new to programming and was wondering how I can have a python program execute and communicate with ac program. 我是编程新手,我想知道如何让python程序执行并与ac程序通信。 I am doing a mathematical computation in python, and was wondering if I could write up the main computation in C, that way the computation runs faster.
我正在用python进行数学计算,想知道是否可以用C编写主要的计算,那样计算就可以运行得更快。 I've been reading about "calling c functions from python", "including C or C++ code directly in your Python code", and "using c libraries from python".
我一直在阅读有关“从python调用c函数”,“在Python代码中直接包含C或C ++代码”和“从python使用c库”的内容。 Is this the same thing?
这是同一件事吗? I want a python program to execute ac program and receive the results.
我希望python程序执行ac程序并接收结果。
What does it mean to "call C library functions" from python? 从python“调用C库函数”是什么意思? Would it allow the python script to use c libraries or allow the script to execute code within ac compiler?
它会允许python脚本使用c库还是允许该脚本在ac编译器中执行代码?
thanks 谢谢
One of the easiest ways to do this is with Cython . 最简单的方法之一是使用Cython 。 You can write code that is very nearly Python, but it compiles down to C. This makes some code (especially numerical computations) much faster.
您可以编写几乎类似于Python的代码,但可以将其编译为C。这使某些代码(尤其是数值计算)的速度大大提高。 You can also use Cython to easily call C library functions from Python, although ctypes is also adequate.
您也可以使用Cython从Python轻松调用C库函数,尽管ctypes也足够。
However, if you actually want to execute a specific C program, you can do so with Subprocess . 但是,如果您实际上要执行特定的C程序,则可以使用Subprocess来执行。 NB, this is (much) slower than calling a C library or Cython function directly.
注意,这比直接调用C库或Cython函数要慢得多。
There's also numpy which can be reasonably fast when dealing with "array operations" (sometimes called vector operations, but I find that term confusing with SIMD terminology). 还有一些numpy,在处理“数组运算”(有时称为矢量运算,但我发现该术语与SIMD术语混淆)时可以相当快。 You'll probably need numpy if you decide to go the cython route, so if the algorithm isn't too complicated, you might want to see if it is good enough with numpy by itself first.
如果您决定采用cython路线,则可能需要numpy,因此,如果算法不太复杂,您可能想先使用numpy看看它是否足够好。
Note that there are two different routes you can take here. 请注意,您可以在此处采取两种不同的路线。 You can use
subprocess
which basically issues system calls to some other program that you have written. 您可以使用
subprocess
,该subprocess
基本上将系统调用发布给您编写的其他程序。 This is slow because you need to start a new process and send the data into the process and then read the data back from the process. 这很慢,因为您需要启动新流程并将数据发送到流程中,然后从流程中读回数据。 In other words, the data gets replicated multiple times for each call.
换句话说,每次调用都会多次复制数据。 The second route is calling a C function from python.
第二种方法是从python调用C函数。 Since Cpython (the reference and most common python implementation) is written in C, you can create C extensions.
由于Cpython(参考和最常见的python实现)是用C编写的,因此您可以创建C扩展。 They're basically compiled libraries that adhere to a certain API.
它们基本上是遵循特定API的编译库。 Then Cpython can load those libraries and use the functions inside, passing pointers to the data.
然后Cpython可以加载这些库并使用其中的函数,将指针传递给数据。 In this way, the data isn't actually replicated -- You're working with the same block of memory in python that you're using in C. The downside here is that the C API is a little complex.
这样,数据实际上并不会被复制-您正在使用与C中使用的python相同的内存块。这里的缺点是C API有点复杂。 That's where 3rd party extensions and existing libraries come in (numpy, cython, ctypes, etc).
那是第3方扩展和现有库(numpy,cython,ctype等)出现的地方。 They all have different ways of pushing computations int C functions without you having to worry about the C API.
它们都有不同的方式将计算推入C函数,而您无需担心C API。 Numpy removes loops so you can add, subtract, multiply arrays quickly (among MANY other things).
Numpy删除循环,因此您可以快速添加,减去,乘以数组(还有很多其他事情)。
Cython
translates python code to C which you can then compile and import -- typically to gain speed here you need to provide additional hints which allow cython to optimize the generated code, ctypes
is a little fragile since you have to re-specify your C function prototype, but otherwise it's pretty easy as long as you can compile your library into a shared object ... The list could go on. Cython
将python代码转换为C,然后您可以对其进行编译和导入-通常在此处要提高速度,您需要提供其他提示,以使cython可以优化生成的代码, ctypes
有点脆弱,因为您必须重新指定C函数原型,但是只要您可以将库编译成共享对象,就很容易了……列表可以继续。
Also note that if you're not using numpy, you might want to check out pypy. 另请注意,如果您不使用numpy,则可能要签出pypy。 It claims to run your python code faster than Cpython.
它声称比Cpython更快地运行python代码。
Options that come to mind: 想到的选项:
Don't rush to coding in C - it's much more expensive in terms of programmer time and is prone to subtle, hard to find bugs. 不要急于用C进行编码-就程序员的时间而言,它的成本要高得多,而且容易产生细微,难以发现的错误。 Run your code in Pure Python first, to get the program produces correct results at whatever speed, and then if you find that the program is too slow, profile the program to decide which part(s) need to be redone in one of the ways listed above.
首先在Pure Python中运行您的代码,以使该程序以任何速度生成正确的结果,然后,如果发现该程序太慢,请对程序进行概要分析,以确定其中一种方法需要重做以上所列。 Usually you'll only need 0-2% of your program to be done in something other than pure python - may as well reap the programmer-time savings for as much of the code as is practical.
通常,您只需要用0-2%的程序来完成除纯python之外的其他事情-可能会为程序员节省尽可能多的实际代码。
You want ctypes . 您需要ctypes 。 It lets you run C functions from Python directly without any crazy extension stuff.
它使您可以直接从Python运行C函数,而无需任何疯狂的扩展。 Of course, you should make sure you're not optimizing prematurely.
当然,您应该确保没有过早地进行优化。 Python's math functions are pretty fast.
Python的数学函数非常快。
The ctypes module lets you call C library functions directly from Python code. 使用ctypes模块,您可以直接从Python代码调用C库函数。 That means it lets you drop the compiled library somewhere that Python can get to it and run those compiled functions.
这意味着它使您可以将编译的库放到Python可以访问的某个地方并运行这些编译的函数。 This is not the same as calling C programs from Python which, as Conrad has pointed out, can be done with subprocess.
这与从Python调用C程序不同,正如Conrad指出的那样,可以使用子进程来完成。 Of course, subprocess doesn't need programs written in C. It can run any executable.
当然,子进程不需要用C编写的程序。它可以运行任何可执行文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.