Skip to content

Commit

Permalink
FIX|NIT: addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
joshc-slac committed Sep 3, 2024
1 parent b4d5133 commit e7e3d39
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
21 changes: 8 additions & 13 deletions beams/behavior_tree/ActionNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(
work_lock=Lock(),
**kwargs,
): # TODO: can add failure condition argument...
super(ActionNode, self).__init__(name)
super().__init__(name)
# print(type(self.status))
self.__volatile_status__ = VolatileStatus(self.status)
# TODO may want to instantiate these locally and then decorate the passed work function with them
Expand Down Expand Up @@ -84,18 +84,13 @@ def update(self) -> py_trees.common.Status:

return new_status

# TODO: serious introspection about that we want to do here.
# def terminate(self, new_status: py_trees.common.Status) -> None:
# """Nothing to clean up in this example."""
# print(f"TERMINATE CALLED ON {self.name}, pid: {os.getpid()}")
# if self.work_proc.is_alive():
# print(f"The process is still alive on {os.getpid()}")
# self.work_proc.terminate()
# self.logger.debug(
# py_trees.console.red
# + "%s.terminate()[%s->%s]"
# % (self.__class__.__name__, self.status, new_status)
# )
def terminate(self, new_status: py_trees.common.Status) -> None:
"""Nothing to clean up."""
self.logger.debug(
py_trees.console.red
+ "%s.terminate()[%s->%s]"
% (self.__class__.__name__, self.status, new_status)
)


if __name__ == "__main__":
Expand Down
12 changes: 4 additions & 8 deletions beams/sequencer/helpers/Worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
"""
import logging
from multiprocessing import Process, Value
from typing import Callable, Any, Optional, Union, List

from epics.multiproc import CAProcess
from typing import Callable, Any, Optional, List


class Worker():
def __init__(self,
proc_name: str,
stop_func: Optional[Callable[[None], None]] = None,
work_func: Optional[Callable[[Any], None]] = None,
proc_type: Union[Process, CAProcess] = Process,
proc_type: type[Process] = Process,
add_args: List[Any] = []
):
self.do_work = Value('i', False)
Expand All @@ -31,18 +29,16 @@ def __init__(self,
self.stop_func = stop_func

def start_work(self):
if (self.do_work.value):
if self.do_work.value:
logging.error("Already working, not starting work")
return
self.do_work.value = True
print(self.work_func)
# breakpoint()
self.work_proc.start()
logging.debug(f"Starting work on: {self.work_proc.pid}")

def stop_work(self):
logging.info(f"Calling stop work on: {self.work_proc.pid}")
if (not self.do_work.value):
if not self.do_work.value:
logging.error("Not working, not stopping work")
return
self.do_work.value = False
Expand Down
3 changes: 3 additions & 0 deletions beams/tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def __init__(self,
def work_func(self):
while self.do_work.value or self.val.value < 100:
if self.val.value < 100: # Note: value captured via closure
# Note: even in python if a full bore loop captures a lock other Processes may not have time
# to acquire this shared resource; therefore the sleep is very strongly suggested if not needed
# when using a lock
time.sleep(0.01)
with self.val.get_lock():
self.val.value += 10 # Note: value captured via closure
Expand Down

0 comments on commit e7e3d39

Please sign in to comment.