Skip to content

Commit 5d54e9e

Browse files
authored
Merge pull request #56 from fal-ai/batuhan/fea-609-support-agent-environment-definitions-in
feat: Support agent environment definitions in the server
2 parents 7eaa040 + 41d6e8b commit 5d54e9e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/isolate/server/server.py

+22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from grpc import ServicerContext, StatusCode
1313

1414
from isolate.backends import EnvironmentCreationError, IsolateSettings
15+
from isolate.backends.common import active_python
1516
from isolate.backends.local import LocalPythonEnvironment
17+
from isolate.backends.virtualenv import VirtualPythonEnvironment
1618
from isolate.connections.grpc import AgentError, LocalPythonGRPC
1719
from isolate.logs import Log, LogLevel, LogSource
1820
from isolate.server import definitions
@@ -23,6 +25,14 @@
2325

2426
# Number of threads that the gRPC server will use.
2527
MAX_THREADS = int(os.getenv("MAX_THREADS", 5))
28+
_AGENT_REQUIREMENTS_TXT = os.getenv("AGENT_REQUIREMENTS_TXT")
29+
30+
if _AGENT_REQUIREMENTS_TXT is not None:
31+
with open(_AGENT_REQUIREMENTS_TXT) as stream:
32+
AGENT_REQUIREMENTS = stream.read().splitlines()
33+
else:
34+
AGENT_REQUIREMENTS = []
35+
2636

2737
# Number of seconds to observe the queue before checking the termination
2838
# event.
@@ -69,6 +79,18 @@ def Run(
6979
for environment in environments:
7080
environment.apply_settings(run_settings)
7181

82+
primary_environment = environments[0]
83+
84+
if AGENT_REQUIREMENTS:
85+
python_version = getattr(
86+
primary_environment, "python_version", active_python()
87+
)
88+
agent_environ = VirtualPythonEnvironment(
89+
requirements=AGENT_REQUIREMENTS,
90+
python_version=python_version,
91+
)
92+
environments.insert(1, agent_environ)
93+
7294
extra_inheritance_paths = []
7395
if INHERIT_FROM_LOCAL:
7496
local_environment = LocalPythonEnvironment()

tests/test_server.py

+30
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,33 @@ def test_server_multiple_envs(
259259
raw_result = run_request(stub, request)
260260

261261
assert from_grpc(raw_result) == "0.6.0 2.8.2"
262+
263+
264+
@pytest.mark.parametrize("python_version", ["3.8"])
265+
def test_agent_requirements_custom_version(
266+
stub: definitions.IsolateStub,
267+
monkeypatch: Any,
268+
python_version: str,
269+
) -> None:
270+
requirements = ["pyjokes==0.6.0"]
271+
agent_requirements = ["dill==0.3.5.1", f"{REPO_DIR}[grpc]"]
272+
monkeypatch.setattr("isolate.server.server.AGENT_REQUIREMENTS", agent_requirements)
273+
274+
env_definition = define_environment(
275+
"virtualenv",
276+
requirements=requirements,
277+
python_version=python_version,
278+
)
279+
request = definitions.BoundFunction(
280+
function=to_serialized_object(
281+
partial(
282+
eval,
283+
"__import__('sysconfig').get_python_version(), __import__('pyjokes').__version__",
284+
),
285+
method="dill",
286+
),
287+
environments=[env_definition],
288+
)
289+
290+
raw_result = run_request(stub, request)
291+
assert from_grpc(raw_result) == ("3.8", "0.6.0")

0 commit comments

Comments
 (0)