What is the purpose of the sub-interpreter API in CPython?

会有一股神秘感。 提交于 2019-11-30 00:22:32

I imagine the purpose is to create separate python execution environments. For instance, mod_wsgi (Apache Python module) hosts a single python interpreter and then hosts multiple applications within sub-interpreters (in the default configuration).

Some key points from the documentation:

  • 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.
  • Because sub-interpreters (and the main interpreter) are part of the same process, the insulation between them isn’t perfect — for example, using low-level file operations like os.close() they can (accidentally or maliciously) affect each other’s open files.
  • Because of the way extensions are shared between (sub-)interpreters, some extensions may not work properly; this is especially likely when the extension makes use of (static) global variables, or when the extension manipulates its module’s dictionary after its initialization.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!