-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from pyiron/subprocess
Implement @conda decorator for executing python functions in specific environments
- Loading branch information
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ channels: | |
dependencies: | ||
- python | ||
- conda =24.7.1 | ||
- executorlib =0.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import subprocess | ||
from concurrent.futures import Future | ||
from socket import gethostname | ||
from typing import Optional | ||
|
||
from executorlib.shared.communication import SocketInterface | ||
from executorlib.shared.executor import get_command_path | ||
from executorlib.shared.interface import SubprocessInterface | ||
|
||
from conda_subprocess.process import Popen | ||
|
||
|
||
def conda( | ||
prefix_name: Optional[str] = None, | ||
prefix_path: Optional[str] = None, | ||
hostname_localhost: bool = False, | ||
): | ||
def conda_function(funct): | ||
def function_wrapped(*args, **kwargs): | ||
task_future = Future() | ||
task_dict = { | ||
"fn": funct, | ||
"args": args, | ||
"kwargs": kwargs, | ||
"resource_dict": {"cores": 1}, | ||
} | ||
interface = SocketInterface(interface=SubprocessInterface(cores=1)) | ||
command_lst = [ | ||
"python", | ||
get_command_path(executable="interactive_serial.py"), | ||
] | ||
if not hostname_localhost: | ||
command_lst += ["--host", gethostname()] | ||
command_lst += ["--zmqport", str(interface.bind_to_random_port())] | ||
interface._interface._process = Popen( | ||
args=interface._interface.generate_command(command_lst=command_lst), | ||
cwd=interface._interface._cwd, | ||
stdin=subprocess.DEVNULL, | ||
prefix_name=prefix_name, | ||
prefix_path=prefix_path, | ||
) | ||
task_future.set_result( | ||
interface.send_and_receive_dict(input_dict=task_dict) | ||
) | ||
interface.shutdown(wait=True) | ||
return task_future.result() | ||
|
||
return function_wrapped | ||
|
||
return conda_function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import sys | ||
import unittest | ||
|
||
try: | ||
from conda_subprocess.decorator import conda | ||
from executorlib.shared.executor import cloudpickle_register | ||
except ImportError: | ||
|
||
def conda(prefix_name=None, prefix_path=None): | ||
def wrap_function(funct): | ||
def function_out(*args, **kwargs): | ||
return None | ||
|
||
return function_out | ||
|
||
return wrap_function | ||
|
||
|
||
@conda(prefix_name="py312") | ||
def add_function(parameter_1, parameter_2): | ||
import os | ||
|
||
return (parameter_1 + parameter_2, os.environ["CONDA_PREFIX"]) | ||
|
||
|
||
@unittest.skipIf( | ||
sys.version_info.minor != 12, | ||
"Test environment has to be Python 3.12 for consistency.", | ||
) | ||
class TestCondaFunction(unittest.TestCase): | ||
def test_conda_function(self): | ||
cloudpickle_register(ind=1) | ||
number, prefix = add_function(parameter_1=1, parameter_2=2) | ||
self.assertEqual(prefix[-5:], "py312") | ||
self.assertEqual(number, 3) |