Skip to content

Commit

Permalink
Trivial attach implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasFehring committed Apr 17, 2024
1 parent 429036f commit 8c7dd91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
13 changes: 11 additions & 2 deletions py_experimenter/database_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

from py_experimenter import utils
from py_experimenter.config import DatabaseCfg, Keyfield
from py_experimenter.exceptions import (CreatingTableError, DatabaseConnectionError, EmptyFillDatabaseCallError, NoExperimentsLeftException,
NoPausedExperimentsException, TableHasWrongStructureError)
from py_experimenter.exceptions import (
CreatingTableError,
DatabaseConnectionError,
EmptyFillDatabaseCallError,
NoExperimentsLeftException,
NoPausedExperimentsException,
TableHasWrongStructureError,
)
from py_experimenter.experiment_status import ExperimentStatus


Expand Down Expand Up @@ -267,6 +273,9 @@ def random_order_string():
def _get_pull_experiment_query(self, order_by: str):
return f"SELECT `id` FROM {self.database_configuration.table_name} WHERE status = 'created' ORDER BY {order_by} LIMIT 1"

def pull_experiment(self, experiment_id: int) -> Dict[str, Any]:
pass # TODO Get keyfields for the allready existing experiment

def _write_to_database(self, combinations: List[Dict[str, str]]) -> None:
columns = list(combinations[0].keys())
values = [list(combination.values()) for combination in combinations]
Expand Down
28 changes: 23 additions & 5 deletions py_experimenter/experimenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
from py_experimenter.config import PyExperimenterCfg
from py_experimenter.database_connector_lite import DatabaseConnectorLITE
from py_experimenter.database_connector_mysql import DatabaseConnectorMYSQL
from py_experimenter.exceptions import (
InvalidConfigError,
NoExperimentsLeftException,
)
from py_experimenter.exceptions import InvalidConfigError, NoExperimentsLeftException
from py_experimenter.experiment_status import ExperimentStatus
from py_experimenter.result_processor import ResultProcessor

Expand Down Expand Up @@ -234,7 +231,7 @@ def add_experiment_and_execute(
self, keyfield_values: Dict, experiment_function: Callable[[Dict, Dict, ResultProcessor], Optional[ExperimentStatus]]
) -> None:
"""
Add one new experiment to the database table with status CREATED_FOR_EXECUTION and execute it.
Add one new experiment to the database table with status CREATED_FOR_EXECUTION and execute it.
The given `keyfield_values` are added to the database table. The status of the experiment is set to `Running`.
Then _execute_experiment is called with the given `experiment_function` and the `keyfield_values`, to immidiately start
Expand Down Expand Up @@ -334,6 +331,27 @@ def unpause_experiment(self, experiment_id: int, experiment_function: Callable)

self._delete_codecarbon_config()

def attach(self, experiment_function: Callable, experiment_id) -> None:
"""
Executes the given `own_function` on the allready running experiment.
:param experiment_function: The function that should be executed..
:type own_function: Callable
:param experiment_id: The id of the experiment to be executed.
:type experiment_id: int
:raises ValueError: If the database provider is sqlite.
"""
if self.config.database_configuration.provider == "sqlite":
raise ValueError("Attaching is not supported for sqlite databases")
if self.use_codecarbon:
self.logger.warning(
"CodeCarbon is not supported for attached functions. Therefore the emissions are only tracked for the main experiment."
)

result_processor = ResultProcessor(self.config.database_configuration, self.db_connector, experiment_id=experiment_id, logger=self.logger)

return experiment_function(result_processor)

def _worker(self, experiment_function: Callable[[Dict, Dict, ResultProcessor], None], random_order: bool) -> None:
"""
Worker that repeatedly pulls open experiments from the database table and executes them.
Expand Down

0 comments on commit 8c7dd91

Please sign in to comment.