Question on multiple Python users in one application

Gregory Ewing greg.ewing at canterbury.ac.nz
Fri Oct 7 04:20:39 EDT 2016


Loren Wilton wrote:
> One concern I have is if two users both "import widget". Are they now sharing
> the widget namespace? I suspect they are, and that is probably undesirable.

Yes, they will be, unless you use sub-interpreters.

I've just been looking at the current docs for sub-interpreters,
and assuming they're not lying too wildly, sub-interpreters
might be what you're after.

https://docs.python.org/3/c-api/init.html

An excerpt:

  PyThreadState* 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 builtins, __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 descriptors).

-- 
Greg



More information about the Python-list mailing list