Skip to content

Commit

Permalink
Merge pull request #154 from pynbody/simplify-sim-from-calc
Browse files Browse the repository at this point in the history
Simplify get_simulation_property to always use SharedLock
  • Loading branch information
apontzen authored Oct 10, 2021
2 parents 28ff396 + 9e37f36 commit 072c80a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
22 changes: 5 additions & 17 deletions tangos/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ class PropertyCalculation(six.with_metaclass(PropertyCalculationMetaClass,object
def all_classes(cls):
return cls._all_classes

def __init__(self, simulation, use_database_lock=False):
def __init__(self, simulation):
"""Initialise a PropertyCalculation calculation object
:param simulation: The simulation from which the properties will be derived
:type simulation: tangos.core.simulation.Simulation
:param use_database_lock: If True,
"""
self.__simulation = simulation
self.__use_database_lock = use_database_lock
self.__simulation_property_cache = {}
self.timing_monitor = timing_monitor.TimingMonitor()

Expand Down Expand Up @@ -89,10 +86,7 @@ def _get_simulation_property_uncached(self, name, default):
This is safe to call even if the database might be locked.
"""
if self.__use_database_lock:
with parallel_tasks.lock.SharedLock("insert_list"):
return self.__simulation.get(name, default)
else:
with parallel_tasks.lock.SharedLock("insert_list"):
return self.__simulation.get(name, default)

@classmethod
Expand Down Expand Up @@ -484,22 +478,16 @@ def providing_classes(property_name_list, handler_class, silent_fail=False):
return classes

def instantiate_classes(simulation, property_name_list, silent_fail=False):
"""Instantiate appropriate property calculation classes for a given simulation and list of property names.
Assumes that these will be used in a parallel_tasks session (most likely by the property_writer),
and accordingly activates the parallel_tasks database locking mechanism."""
"""Instantiate appropriate property calculation classes for a given simulation and list of property names."""
instances = []
handler_class = type(simulation.get_output_handler())
for property_identifier in property_name_list:
instances.append(providing_class(property_identifier, handler_class, silent_fail)(simulation, True))
instances.append(providing_class(property_identifier, handler_class, silent_fail)(simulation))

return instances

def instantiate_class(simulation, property_name, silent_fail=False):
"""Instantiate an appropriate property calculation class for a given simulation and property name
Assumes that these will be used in a parallel_tasks session (most likely by the property_writer),
and accordingly activates the parallel_tasks database locking mechanism."""
"""Instantiate an appropriate property calculation class for a given simulation and property name."""
instance = instantiate_classes(simulation, [property_name], silent_fail)
if len(instance)==0:
return None
Expand Down
10 changes: 8 additions & 2 deletions tangos/scripts/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ def add_tracker(halo, size=None):
core.get_default_session().commit()


def grep_run(st):
def grep_run(opts):
run = core.get_default_session().query(Creator).filter(
Creator.command_line.like("%" + st + "%")).all()
Creator.command_line.like("%" + opts.query + "%")).all()
for r in run:
print(r.id, end=' ')

Expand Down Expand Up @@ -447,6 +447,12 @@ def get_argument_parser_and_subparsers():
subparse_recentruns.add_argument("num", type=int,
help="The number of runs to display, starting with the most recent")

subparse_greprun = subparse.add_parser("grep-runs",
help="List IDs of runs matching command line")
subparse_greprun.set_defaults(func=grep_run)
subparse_greprun.add_argument("query", type=str,
help="The sub-string to search for in the command line")

# The following subcommands currently do not work and is disabled:
"""
subparse_remruns = subparse.add_parser("rm", help="Remove a simulation from the database")
Expand Down

0 comments on commit 072c80a

Please sign in to comment.