2008-01-07 17:55 星期一
嵌入Python解析器执行一些简单的Python 脚本脚本很容易,但是当python解析器用到扩展模块时和多个线程都需要Python解析器执行脚本时,遇到了一些麻烦问题,下面是在查看帮助文档过程中,一些简单的翻译记录:
Py_NewInterpreter():
Create a new sub-interpreter. This is an (almost) totally separate environment for the execution of Python code. In particular, the new interpreter has separate, independent versions of all imported modules, including the fundamental modules __builtin__, __main__ and sys. The table of loaded modules (sys.modules) and the module search path (sys.path) are also separate. The new environment has no sys.argv variable. It has new standard I/O stream file objects sys.stdin, sys.stdout and sys.stderr (however these refer to the same underlying FILE structures in the C library).
创建一个子的解析器。这是一个独立的运行Python代码的环境,新解析器是独立的,不受已经导入的Module约束,包括基本的模块 __builtin__,__main__和sys。记录加载Modules的表单和module搜索路径sys.path都是独立的。新环境没有sys.argv 变量。他有一个新的标准I/O流文件对象 sys.stdin,sys.stdout 和 sys.stderr 并且 这些引用和底层的Clibrary中的文件结构相同。
The return value points to the first thread state created in the new sub-interpreter. This thread state is made in the current thread state. Note that no actual thread is created; see the discussion of thread states below. If creation of the new interpreter is unsuccessful, NULL is returned; no exception is set since the exception state is stored in the current thread state and there may not be a current thread state. (Like all other Python/C API functions, the global interpreter lock must be held before calling this function and is still held when it returns; however, unlike most other Python/C API functions, there needn't be a current thread state on entry.)
返回值指向在新的子解析器中第一个被创建的线程的状态。这个线程状态被人做为当前的线程状态。注意没有实际的线程被创建;看下面线程状态的讨论。如果创建新的解析器不成功,函数将返回Null;由于异常状态被存储在当前的线程状态中并且可能并不存在当前的线程状态,所以没有异常被设置。就像所有的Python/C API函数一样,在调用这个函数之前,全局的解析器锁必须被Hold直到该函数返回,但是不像别的Python/C API,他不需要一个当前线程状态作为入口。
Extension modules are shared between