Skip to main content

PEP 554 Subinterpreters usable from Python code

Project description

extrainterpreters

Utilities to make use of new "interpreters" module in Python 3.12

Status: beta

Usage: Works with cPython >= 3.12

Just import "extrainterpreters" and use the Interpreter class as a wrapper to the subinterpreter represented by a single integer handle created by the new interpreters module:

import extrainterpreters as ET

import time

def my_function():
    time.sleep(1)
    print("running in other interpreter")
    return 42

interp = ET.Interpreter(target=my_function)
interp.start()
print("running in main interpreter")
time.sleep(1.5)
print(f"computation result: {interp.result()}")

history

PEP 554 Describes a Python interface to make use of Sub-interpreters- a long living feature of cPython, but only available through C code embedding Python. While approved, the PEP is not in its final form, and as of Python 3.13 the interpreters module suggested there is made available as _interpreters. (And before that, in Python 3.13, it is available as _xxsubinterpreters.

With the implementation of PEP 684 before Python 3.12, using subinterpreters became a lot more interesting, because each subinterpreter now has an independent GIL. This means that different interpreters (running in different threads) can actually execute Python code in parallel in multiple CPU cores.

"extrainterpreters" offer a nice API to make use of this feature before PEP 554 becomes final, and hopefully, will keep offering value as a nice API wrapper when it is final. Enjoy!

forms of use

with an extrainterpreter.Interpreter instance, one can call: inter.run(func_name, *args, **kwargs}) to run code there in the same thread: the return value is just returned as long as it is pickleable. The inter.run_in_thread with the same signature, will start a fresh thead and call the target function there - the Interpreter instance then should be pooled in its "done()" method, and once done, the return value will be available by calling "result".

Also working is a threading like interface:

from extrainterpreters import Interpreter

...

    interp = Interpreter(target=myfunc, args=(...,))
    interp.start()
    ...
    interp.join()

    # bonus: we have return values!

    print(interp.result()

It will even work for methods defined on the REPL, so, just give it a try.

There is a new class in the works which will be able to run code in the sub-interpreter without depending of the PEP554 "run_string" (once the setup is done). All processing in the sub- interpreter will automatically take place in another thread, and the roadmap is towards having a Future object and an InterpreterPoolExecutor compatible with the existing executors in concurrent.Futures

A lot of things here are subject to change. For now, all data exchange takes place in a custom memory buffer with 10MB (it is a bytearray on the parent interpreter, and a memoryview for that on the target)

Data is passed to and back the interpreter using pickle - so, no black magic trying to "kill an object here" and "make it be born there" (not even if I intended to, as ctypes is not currently importable on sub-interpreters).

The good news is that pickle will take care of importing whatever is needed on the other side, so that a function can run.

API and Usage:

In beta stage the suggestion is to use this as one would threading.Thread - see the example above - and do not rely on the provided Queue class (seriously it is broken right now), until some notice about it is given.

Roadmap

first the basics:

we should get Lock, Rlock, Queue and some of the concurrency primitives
as existing for threads, async and subprocessing working - at that point
this should be production quality

second, the bells and whistles

I plan to get this compatible with concurrent.futures.Executor, and have an easy way to schedule a subinterpreter function execution as an async task.

Also, I should come up with a Queue object to pass data back and forth. As for the data passing mechanism: we will use pickle, no doubt.

Architecture

The initial implementation used a pre-allocated mmaped file to send/get data from other interpreters, until I found out the mmap call in the sub-interpreter would build a separate mmap object in a different memory region, and a lot of data copying was taking place.

Currently, two C methods retrieve the address of an object implementing the buffer interface, and create a "stand alone" memoryview instance from the address alone. This address is passed to the subinterpreter as a string during the setup stage,

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

extrainterpreters-0.2.0.tar.gz (38.0 kB view details)

Uploaded Source

Built Distributions

extrainterpreters-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl (44.4 kB view details)

Uploaded CPython 3.13 musllinux: musl 1.2+ x86-64

extrainterpreters-0.2.0-cp313-cp313-musllinux_1_2_i686.whl (44.3 kB view details)

Uploaded CPython 3.13 musllinux: musl 1.2+ i686

extrainterpreters-0.2.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (44.2 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

extrainterpreters-0.2.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (43.8 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

extrainterpreters-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (37.6 kB view details)

Uploaded CPython 3.13 macOS 11.0+ ARM64

extrainterpreters-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl (37.2 kB view details)

Uploaded CPython 3.13 macOS 10.13+ x86-64

extrainterpreters-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl (44.4 kB view details)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

extrainterpreters-0.2.0-cp312-cp312-musllinux_1_2_i686.whl (44.2 kB view details)

Uploaded CPython 3.12 musllinux: musl 1.2+ i686

extrainterpreters-0.2.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (44.2 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

extrainterpreters-0.2.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (43.8 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

extrainterpreters-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (37.6 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

extrainterpreters-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl (37.2 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

File details

Details for the file extrainterpreters-0.2.0.tar.gz.

File metadata

  • Download URL: extrainterpreters-0.2.0.tar.gz
  • Upload date:
  • Size: 38.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for extrainterpreters-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e30e08abfd67ce414696a3dc715af3c521da8958c1e4a9519d8d846af8d52c89
MD5 57efe49338d150bdf611aba75d45b47e
BLAKE2b-256 d09c282c759ac3fc3c362e09b3ed3713f10a33b6d846db23f8fd0ced9efe6b33

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0.tar.gz:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c7695c00353d24119cb0804104ba1b026f902808ee6b0ae1ee7f7331d4fe6295
MD5 b7bed3f7182fe6b7d41f5e1dc592b55e
BLAKE2b-256 b729e8d2be383f75e3537d7fa58ba23b479473d84ba07bf4b74890ecb7904cb6

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp313-cp313-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp313-cp313-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 f28d587d094627b3aaf4adc8c7a64ee70bb7403ddb2416de0899e8a8f6b11662
MD5 973158444908db78cf01faec68cf5ac3
BLAKE2b-256 531ae04f7e976a71b27fac48d3cbebb2cc637b8ff6c130de8ceec0c05fcee1eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp313-cp313-musllinux_1_2_i686.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cd5a339785bfe829dac2a2c0710dbbb9dd37eb1a7eda5533c6a3f0e8d25cd3f1
MD5 277b2e431c7c291162047b155bd07e4d
BLAKE2b-256 e129bc2b814a5bbfc9c35797ee0e55d6dd6e05f11d5887438d7cfb97caa63fc9

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 7c2d7aae841611cfc69283af683b439f03809f1a3029e452a6e5bb0f080fd66d
MD5 4c971b96c7a48a1d9c36acf4e5f25dd1
BLAKE2b-256 adfdbd76f220f953a65ff7dc911e6373be2dd78f6c8e6edcb607e9334f6f4f4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8cc61ff6cadf583a351b3fdc58fd1c4c0f94468a0a3775143e0e5f3bd79f5921
MD5 14f0f1f175615c975f2f38221f633c0b
BLAKE2b-256 d8fc56c01eb4008de084426c7b2226ebca4bb980015cdde6b231bf6053fd624a

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d9f952fc6c3fc521bdef4ec536b0f50303d4326fa2c89e004b5644d4471ddb6f
MD5 cdea2dbfdf13b2f3fef3862545f2ad1d
BLAKE2b-256 4e5375e31aaccffed2c89f6a1b9b33cfc8ce01bfcef11d5ebb1905166d4b23c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5e14850b9f99650d5f54d9c2dece4bba146ca7c78fd26d1a8cfe07fb7d2cd6b1
MD5 99cf0f7431fe8ffa2c82f95fb63c663f
BLAKE2b-256 97bf7b87d43d1376cfdc637a0aca452830e8480c51d0b40dcf97a9519becf6ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp312-cp312-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp312-cp312-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 126dada13562637492a60aa4886b9b1bb87eda584f57993c088874f668408fd3
MD5 9ac4d55441230fd96be020dfaaa6aac0
BLAKE2b-256 39ecf05a5fc33f7a9b29b37878f453c70cf9d6faeb14636e61b5fc31943a55d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp312-cp312-musllinux_1_2_i686.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5c130af4946eb7503db8869b78a96b9909580feb09693be9898a1f0a2474bdb9
MD5 4ffd0fbb0fe3af54dd91d5de9f05523d
BLAKE2b-256 6cc9336b7409b42d11721f1bad850d1aa113eba6125b5bd9e091223ea31f6149

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 84b8a2494c14b71fb189f93fb8dd4f7e946b46be79191fe049986821ff03b64b
MD5 b1538418a08f52bc59c1a1a0c5040027
BLAKE2b-256 f728a826cb87799c5695a93da08b438a4aa094e32d08c491d224233b562c00b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d6c172748df88c30a7b694cf3cb865a32ec1d4fcc00addc0bcefa33fe24de143
MD5 6f34561d637e5c56f97a8d85afe6d0a0
BLAKE2b-256 afb5e0fc74acfde55d848c0aac502d5c53fe457731541b9ef38a7d7b332cc66a

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

File details

Details for the file extrainterpreters-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for extrainterpreters-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 4fe9daf836ffa7c005990023f7a68efdd0a705262a55b4330276757eafb3fa8f
MD5 572fd4f40c2f54e7669e37ba3ec4044f
BLAKE2b-256 5fcfa5bf1426c288db495bdccb9f9652518c1e5bbccbc2d5303e532feed21e3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for extrainterpreters-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: pypi.yml on jsbueno/extrainterpreters

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page