Skip to content

Commit a45f814

Browse files
committed
force singelton for localbox
1 parent 2744766 commit a45f814

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/codeboxapi/local.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,27 @@ class LocalBox(CodeBox):
3232
"""
3333
LocalBox is a CodeBox implementation that runs code locally using IPython.
3434
This is useful for testing and development.
35+
36+
Only one instance can exist at a time. For parallel execution, use:
37+
- DockerBox for local parallel execution
38+
- Get an API key at codeboxapi.com for hosted parallel execution
3539
"""
3640

41+
_instance: t.Optional["LocalBox"] = None
42+
3743
def __new__(cls, *args, **kwargs) -> "LocalBox":
38-
# This is a hack to ignore the CodeBox.__new__ factory method.
39-
return object.__new__(cls)
44+
if cls._instance:
45+
raise RuntimeError(
46+
"Only one LocalBox instance can exist at a time.\n"
47+
"For parallel execution use:\n"
48+
"- Use DockerBox for local parallel execution\n"
49+
"- Get an API key at https://codeboxapi.com for secure remote execution"
50+
)
51+
52+
# This is a hack to ignore the CodeBox.__new__ factory method
53+
instance = object.__new__(cls)
54+
cls._instance = instance
55+
return instance
4056

4157
def __init__(
4258
self,

0 commit comments

Comments
 (0)