Skip to content
Search

Python:Isolation

lastmod 2022-11-25created 2022-03-1718 edits

My travelogue of fumbling around with ways to isolate Python...

Categories

  • PEP 432 - Restructuring the CPython startup sequence
  • PEP 405 - Python Virtual Environments
  • process-isolation - Process Isolation in Python - Elegant process isolation in pure python

The order of my fumbling

  1. Isolated from c with cpython.
  2. Switched to using the exec built-in function in python.
  3. Installed venv and ran it with subprocess.

How to communicate with the isolated Python

  1. Communicate with grpc.
  2. Communicate with grpc+json. -> Large data transfers turned out to be slow.
  3. Communicate with grpc+json+sharedmemory. -> Bytes data is encoded for numpy serialization, but json doesn't support bytes data, which turned out to be a problem.
  4. Communicate with grpc+msgpack+sharedmemory.

Fumbling toward complete isolation and communication

  1. At first, ran it with asyncio.subprocess of the original python/recc and communicated directly inside python: the problem is that remote environments are not supported.
  2. Original python/recc <-- how to communicate? --> isolated venv subprocess: the problem is that the isolated venv has no communication module.
  3. Original python/recc <-- grpc --> isolated venv subprocess: the isolated venv needs grpc.
  4. Original python/recc <-- grpc --> in the original python subprocess, add only venv's site-packages directory to sys.path: the problem is that the original python's site-packages is shared.
  5. Original python/recc <-- grpc --> in the isolated venv subprocess, add only the original python's site-packages directory to sys.path: the problem is that it doesn't work properly.
  6. Original python/recc <-- grpc --> in the original python subprocess, context-changed the variables of the sys package following how venv works: the problem is that async/await statements run asynchronously, so a context change affects the global sys package and it does not work properly.
  7. Original python/recc <-- grpc --> installed all recc-related dependency packages in the isolated venv subprocess.
    • Downloaded the dependency files in advance with pip download --dest /download/path -r requirements.txt
    • and installed them offline with pip install --no-index -f /download/path -r requirements.txt.
    • Assuming offline use, recc copies all files already installed in the original site-packages into {venv-site-packages}/__recc_local__,
    • and adds the installed path to the {venv-site-packages}/recc.pth file so that the recc module is recognized automatically without extra work. (However, since it wasn't installed with pip, it doesn't appear in the pip list command output.)
    • Solved with this..

What is loader?

See Python:importlib#BuiltinImporter.

Does import order matter?

Does venv matter?

Let's check the virtualenv implementation

Check virtualenv#activate_this.py

See also

Favorite site

author Zer0 sitemap