我不清楚为什么子解释器 API 存在以及为什么它在模块(如 mod_wsgi apache 模块)中使用。它主要用于为在同一进程中运行的不同应用程序创建一个安全沙箱,还是一种允许多线程并发的方法?也许两者兼而有之?还有其他目的吗?
问问题
4481 次
2 回答
13
我想目的是创建单独的 python 执行环境。例如,mod_wsgi(Apache Python 模块)托管一个 Python 解释器,然后在子解释器中托管多个应用程序(在默认配置中)。
文档中的一些关键点:
- 这是一个(几乎)完全独立的 Python 代码执行环境。特别是,新的解释器具有所有导入模块的独立版本,包括基本模块
__builtin__
和.__main__
sys
- 加载模块表 (sys.modules) 和模块搜索路径 (sys.path) 也是分开的。
- 因为子解释器(和主解释器)是同一进程的一部分,所以它们之间的隔离并不完美——例如,使用像 os.close() 这样的低级文件操作,它们可以(意外或恶意)影响每个其他人打开的文件。
- 由于(子)解释器之间共享扩展的方式,某些扩展可能无法正常工作;当扩展使用(静态)全局变量时,或者当扩展在其初始化后操作其模块的字典时,这种情况尤其可能发生。
于 2009-04-16T07:45:18.543 回答
0
As I understood it last, the idea was to be able to execute multiple applications as well as multiple copies of the same application within the same process.
This is a feature found in other scripting languages (e.g. TCL), and is of particular use to gui builders, web servers, etc.
It breaks in python because many extensions are not multiple-interpreter safe, so one interpreter's actions could affect the variables in another interpreter.
于 2013-09-30T22:08:59.297 回答