Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a name argument similar to threads #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
*swp
.eggs
build/

*.py[co]
[.]venv/
8 changes: 5 additions & 3 deletions src/extrainterpreters/base_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@

class BaseInterpreter:

def __init__(self):
def __init__(self, name=None):
# .intno and .id are both set to the interpreter id,
# but .intno is set to None when the interpreter is closed.
self.intno = self.id = None
self.name = name
self.lock = threading.RLock()

def start(self):
if self.intno is not None:
raise RuntimeError("Interpreter already started")
with self.lock:
self.intno = self.id = interpreters.create()
self.name = self.name or f"Subinterpreter #{self.intno}"
running_interpreters[self.intno] = self
self.thread = None
self._create_channel()
Expand All @@ -44,11 +46,11 @@ def close(self, *args):
try:
while time.monotonic() - self._started_at < _TTL:
# subinterpreters need sometime to stabilize.
# shutting then imediatelly may lead to a segfault.
# shutting then immediately may lead to a segfault.
time.sleep(0.002)
if interpreters.is_running(self.intno):
# TBD: close on "at exit"
# # but really, just enduser code running with "run_string΅ on other thread should
# # but really, just end user code running with "run_string" on other thread should
# leave the sub-interpreter on this state.
return
interpreters.destroy(self.intno)
Expand Down
7 changes: 2 additions & 5 deletions src/extrainterpreters/piped_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class FuncData(StructBase):
def _dispatcher(pipe, buffer):
"""the core running function in a PipedInterpreter

This is responsible for watching comunications with the parent
interpreter, dispathing execution, and place the return values
This is responsible for watching communications with the parent
interpreter, dispatching execution, and place the return values
"""

while True:
Expand Down Expand Up @@ -113,6 +113,3 @@ def _close_channel(self):
self.pipe.read(timeout=None)
self.pipe.close()
super()._close_channel()



4 changes: 2 additions & 2 deletions src/extrainterpreters/simple_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ class SimpleInterpreter(_BufferedInterpreter):
This implementation uses a memory area (by default of 10MB), to send pickled objects back and fort at fixed offsets.
"""

def __init__(self, target=None, args=(), kwargs=None):
def __init__(self, name=None, target=None, args=(), kwargs=None):
kwargs = kwargs or {}
super().__init__()
super().__init__(name=name)
self.target = target
self._args = args
self._kwargs = kwargs
Expand Down