简体   繁体   English

从python程序执行c程序

[英]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: 想到的选项:

  1. Cython: A dialect of Python that can freely mix Python and C datatypes. Cython:Python的一种方言,可以自由地混合使用Python和C数据类型。 Gives a modest speedup with a few type annotations for classes, and can be quite a bit faster if you give your main loop all C types to operate on. 提供适度的加速,并为类提供一些类型注释,并且如果将所有C类型提供给主循环,则速度会快很多。 Pretty much CPython only at this time, but there's been discussion of making it work with Pypy. 目前只有CPython差不多,但是已经有讨论使其与Pypy一起使用。
  2. SWIG: A sort of interface/glue language for matching up a C library with a potentially large list of higher level languages, including CPython. SWIG:一种接口/胶水语言,用于将C库与可能包括较大数量的更高级语言(包括CPython)的列表进行匹配。
  3. ctypes: Allows you to call individual C functions and access individual C datatypes. ctypes:允许您调用单个C函数并访问单个C数据类型。 Not blazing in CPython unless you spend a lot of time in the C code. 除非您花费大量时间在C代码中,否则不要在CPython中大放异彩。 Can be a bit brittle. 可能有点脆弱。 Also works on Pypy. 也适用于Pypy。
  4. C extension module: Pretty standard stuff for CPython - this is how much of the CPython the standard library is put together. C扩展模块:CPython的相当标准的东西-这是标准库放在一起的CPython数量。 Some but not all of these work with Pypy if you use cpyext. 如果使用cpyext,则其中一些(但不是全部)可以与Pypy一起使用。 In short, with this you're writing a module with a bunch of boiler plate, in C, but it can be called from CPython. 简而言之,您正在用C编写带有一堆样板的模块,但是可以从CPython调用它。
  5. CFFI: A new foreign function interface coming from the Pypy project. CFFI:来自Pypy项目的新外部功能接口。 They have it working with CPython now, and intend to get it going with Pypy before long. 他们现在已将其与CPython一起使用,并打算不久后将其与Pypy一起使用。 It is like ctypes, but is less brittle - the downside is it requires a C compiler at runtime, and it's pretty young code. 它就像ctypes一样,但不那么脆弱-缺点是它在运行时需要C编译器,而且代码还很年轻。
  6. Pypy: This isn't really a way calling C itself, but it's got almost as much speed as C for a lot of pure python code. Pypy:这并不是调用C本身的方法,但是对于许多纯python代码,它的速度几乎与C一样快。 If you don't have a lot of C extension modules you require, then Pypy might be a good option for you. 如果您不需要很多C扩展模块,那么Pypy可能是您的理想选择。
  7. subprocess: A very portable way of interacting with another process in not-necessarily-the-same-language. 子过程:与另一过程进行交互的一种非常可移植的方式,不需要使用相同的语言。 Can be used with just about any language in the child process. 子进程中几乎可以使用任何一种语言。 Not blazing fast, unless you spend little time passing data back and forth. 除非您花费很少的时间来回传递数据,否则请不要急忙。 Pretty much all data exchanged has to be serialized to ASCII or something, but it is so portable and simple and loosely coupled that it's worth thinking about. 几乎所有交换的数据都必须序列化为ASCII之类的东西,但是它是如此的可移植,简单,松散耦合,值得考虑。
  8. You might be able to just optimize your Python: http://wiki.python.org/moin/PythonSpeed/PerformanceTips 您也许可以优化您的Python: http : //wiki.python.org/moin/PythonSpeed/PerformanceTips

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM