集成的方式介绍
集成的方式介绍
Programming Python, 3rd Edition 翻译
最新版本见:http://wiki.woodpecker.org.cn/moin/PP3eD
22.2. Integration Modes
22.2. 集成的方式
The last two technical chapters of this book introduce Python's tools for interfacing to the outside world and discuss both its ability to be used as an embedded language tool in other systems and its interfaces for extending Python scripts with new modules and types implemented in C-compatible languages. We'll also briefly explore other integration techniques that are less C specific, such as the Component Object Model (COM) and Jython.
本书最后的这两个技术章节介绍Python与外部语言的接口,不仅讨论Python的内嵌能力,即用作其它系统的内嵌语言工具,也讨论Python的扩展接口,即用C兼容语言实现新的模块和类型。我们也会简要地考察其它非特定于C的集成技术,比如组件对象模型(COM)和Jython.
Our focus in these chapters is on tight integrationwhere control is transferred between languages by a simple, direct, and fast in-process function call. Although it is also possible to link components of an application less directly using Inter-Process Communication (IPC) and networking tools such as sockets and pipes that we explored earlier in the book, we are interested in this part of the book in more direct and efficient techniques.
这两章关注的是紧密的集成,即通过简单,直接,快速的进程内函数调用,使控制在不同语言之间跳转。尽管也可以使用本书前面所述的进程间通信(IPC)和网络工具如socket和管道,不那么直接地,连接应用程序的各个部件,但我们现在感兴趣的是更直接有效的集成技术。
When you mix Python with components written in C (or other compiled languages) either Python or C can be "on top." Because of that, there are two distinct integration modes and two distinct APIs:
当你混合Python和C语言(或其它编译型语言)写的部件,“在上面”的即可以是Python,也可以是C。因此,有两种截然不同的集成方式和两套截然不同的API:
The extending interface
For running compiled C library code from Python programs
The embedding interface
For running Python code from compiled C programs
扩展接口
用于Python程序中运行已编译的C代码库
内嵌接口
用于C程序中运行Python代码
Extending generally has three main roles: to optimize programsrecoding parts of a program in C is a last-resort performance boost; to leverage existing librariesopening them up for use in Python code extends their reach; and to allow Python programs to do things not directly supported by the languagePython code cannot normally access devices at absolute memory addresses, for instance, but can call C functions that do. For example, the NumPy package for Python is largely an instance of extending at work: by integrating optimized numeric libraries, it turns Python into a flexible and efficient system that some compare to Matlab.
扩展方式一般有三个作用:优化程序——用C重写部份程序是最后可用的提升性能的手段;借用现有的库——在Python代码中使用库,扩展它们的能力范围;以及,使Python程序员做到语言本身不直接支持的事——比如,Python代码不能在绝对内存地址常规地访问设备,但是可以调用C函数来做到。如Python的NumPy包,很大程度上就是一个可用的扩展:它通过集成优化的数值库,将Python转变为一个灵活高效的数值系统,几乎可比Matlab.
Embedding typically takes the role of customizationby running user-configurable Python code, a system can be modified without shipping or building its full source code. For instance, some game systems provide a Python customization layer that can be used to modify the game or characters. Embedding is also sometimes used to route events to Python coded handlers. Python GUI toolkits, for example, usually employ embedding.
内嵌方式的典型作用是定制——通过运行用户可配置的Python代码,就可以修改系统,而无需附带或构建所有的源代码。例如,某些游戏提供了Python定制层,可以用来修改游戏或角色。内嵌Python有时也用来传送事件到Python编码的事件处理器。如Python GUI工具包,常常使用内嵌Python。
Figure 22-1 sketches this traditional dual-mode integration model. In extending, control passes from Python through a glue layer on its way to C code. In embedding, C code processes Python objects and runs Python code by calling Python C API functions. In some models, things are not as clear-cut. For example, in the COM and CORBA systems, calls are passed through different kinds of intermediaries. Under cytpes, Python scripts make library calls rather than employing glue code. And in systems such as Pyrex, things are more different still. We will meet such alternative systems later in this part of the book. For now, our focus is on traditional Python/C integration models.
图22-1勾画了这种传统的双模集成方式。扩展方式下,控制从Python经过一个粘合层到达C代码。内嵌方式下,C代码通过调用Python C API函数处理Python对象并运行Python代码。而在有些方式下,事情不是这样清晰。例如,在COM和CORBA系统,调用会在各种不同中间件之间传递。使用ctypes模块时,Python脚本直接调用库函数,而不是通过粘合层。在诸如Pyrex的系统中,事情则更加不同。本书会在以后讲到这样的不同系统。现在,我们关注的是传统的Python/C集成方式。
Figure 22-1. Traditional integration model
图22-1. 传统的集成方式
(Todo: 谁来把图片插上?)
This chapter covers extending, and the next explores embedding. Although we will study these topics in isolation, keep in mind that many systems combine the two techniques. For instance, embedded Python code run from C can also import and call linked-in C extensions to interface with the enclosing application. And in callback-based systems, C code initially accessed through extending interfaces may later use embedding techniques to run Python callback handlers on events.
本章讲扩展方式,下章是内嵌方式。虽然我们分开来研究,但是记住,许多系统会组合运用这两种技术。例如,C语言中内嵌的Python代码可以导入并调用C语言编译的扩展,来与包裹在外面的应用交互。又如,在基于回调的系统中,Python通过扩展接口操纵C代码,使之用内嵌技术运行Python事件回调函数。
For example, when we created buttons with Python's Tkinter GUI library earlier in the book, we called out to a C library through the extending API. When our GUI's user later clicked those buttons, the GUI C library caught the event and routed it to our Python functions with embedding. Although most of the details are hidden to Python code, control jumps often and freely between languages in such systems. Python has an open and reentrant architecture that lets you mix languages arbitrarily.
例如,使用前述的Python Tkinter GUI库,当我们创建按钮时,我们通过扩展API调用了C库。当用户点击按钮,GUI C库触发事件,并传送到我们内嵌的Python函数。虽然大多数细节对Python代码是隐藏的,但在这样的系统中,控制常常是在语言之间自由地跳转。Python具有开放和可重入的体系结构,可以让你任意地混和各种语言。
22.2.1. Presentation Notes
22.2.1. 说明
Before we get into details, I should also mention that Python/C integration is a big topic. In principle, the entire set of extern C functions in the Python system makes up its runtime interface. Because of that, these next two chapters focus on the tools commonly used to implement integration with external componentsjust enough to get you started.
在进入细节以前,我应该指出Python/C集成是一个很大的主题。理论上,Python系统的运行时接口都是由外部C函数组成的。所以,下面两章仅仅是能够让你起步,主要讲实现与外部组件集成的常用工具。
For additional examples beyond this book and its examples distribution, see the Python source code itself; its Modules and Objects directories are a wealth of code resources. Most of the Python built-ins we have used in this bookfrom simple things such as integers and strings to more advanced tools such as files, system calls, Tkinter, and the DBM files underlying shelves. Their utilization of integration APIs can be studied in Python's source code distribution as models for extensions of your own.
除了本书的例子,还可以看Python源码随带发布的一些例子;它的Modules和Objects目录是丰富的代码资源。其中包含我们已使用过的大多数Python的内建模块,从简单的如整数和字符串到更高级的如文件,系统调用,Tkinter和DBM及shelves。阅读Python发布的源码中的模块,学习它们如何使用集成API,可以作为你自己的扩展模块的基矗
In addition, Python's Extending and Embedding and Python/C API manuals are now reasonably complete, and they provide supplemental information to the presentation here. If you plan to do integration, you should browse these as a next step. For example, the manuals go into additional details about C extensions in threaded programs and multiple interpreters in embedded programs, which we will largely finesse here.
另外,有关Python扩展与内嵌及Python/C API的帮助手册现在已相当完善,它们提供了对此处示例的补充信息。如果你要进行集成,下一步你应该浏览那些信息。例如,手册上有关于多线程和多处理器的C语言扩展的额外细节,而我们避开了大部份这些内容。
These chapters also assume that you know basic C programming concepts. If you don't, you won't miss much by skipping or skimming these chapters. Typically, C developers code the extending and embedding interfaces of a system, and others do the bulk of the system's programming with Python alone. But if you know enough about C programming to recognize a need for an extension language, you probably already have the required background knowledge for this chapter.
这两章同时假定你了解基本的C语言编程的概念。如果不是,你可以跳过这两章,也不会有问题。一般是,C开发者对系统的扩展和内嵌接口进行编码,而其他人仅使用Python做系统的主体编程。如果你对C编程很了解,以至于认识到需要一种扩展语言,那么你可能已经具备了本章所要求的背景知识。
The good news in both chapters is that much of the complexity inherent in integrating Python with a static compiled language such as C can be automated with tools in the extension domain and higher-level APIs in the embedding world. For example, in this chapter we begin with a brief look at basic C API use, but then quickly move onto using the automated SWIG integration code generator in three examples and introduce alternatives such as ctypes, Pyrex, and Boost.Python at the end of the chapter. The next chapter takes a similar approach to embedding, with a basic introduction followed by a higher-level library. For now, let's get started with some extending fundamentals.
在这两章中,好消息是,对于Python和静态编译语言如C语言集成的内在复杂性,扩展方面可使用自动化工具,内嵌方面可由高层的API自动处理。例如,本章我们先简单看看基本的C API使用,但是马上转向使用SWIG自动集成代码生成,会举三个实例进行使用,结束时介绍一些其它的工具,如ctypes, Pyrex和Boost.Python。下一章讲内嵌,也是同样,先是基本介绍,然后是高级的库调用。现在,让我们以一些扩展的基本原理开始吧。