Skip to content

Commit

Permalink
DOC: update logging from process that spawns workers, update LoggingV…
Browse files Browse the repository at this point in the history
…isitor
  • Loading branch information
tangkong committed Nov 18, 2024
1 parent 3565f58 commit b8fea68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
9 changes: 6 additions & 3 deletions beams/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,14 @@ def __init__(self, print_status: bool = False):

def run(self, behaviour: Behaviour) -> None:
"""
Write node status to logging stream.
Write node status to logging stream, executes AFTER a node is ticked.
(operates on nodes yielded by Behaviour.tick())
If print_status is requested and the console logger won't display, also
print to console
print to console.
"""
out_msg = f"{behaviour.__class__.__name__}.run() [{behaviour.status}]"
name = getattr(behaviour, 'name', None) or behaviour.__class__.__name__
out_msg = f"{name} node tick completed [{behaviour.status.name}]"
if behaviour.feedback_message:
logger.debug(out_msg + f": [{behaviour.feedback_message}]")
if self.print_status and (self.stream_handler_level > logging.DEBUG):
Expand Down
24 changes: 12 additions & 12 deletions beams/sequencer/helpers/worker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
* An base class for child classes whos main function is to support a work thread.
* Holds volatile `self.do_work` which is intended to handle kill signal
* Provides `start_work` and `stop_work` functions to spawn and stop work processes
* Optional constructor arg `stop_func` to be executde on process termination before joining work process.
* Holds volatile `self.do_work` which is intended to handle kill signal.
* Provides `start_work` and `stop_work` functions to spawn and stop work processes.
* Optional arg `stop_func` run on process termination before joining work process.
"""
import logging
from ctypes import c_bool
Expand All @@ -26,41 +26,41 @@ def __init__(
self.proc_name = proc_name
self.proc_type = proc_type
self.add_args = add_args or []
# TODO: we may want to decorate work func so it prints proc id... This may be a case of wrapper_func as opposed to decorator
if (work_func is None):
self.work_proc = proc_type(target=self.work_func, name=self.proc_name)
else:
self.work_func = work_func
# Critical Note: This makes assumptions of the work_func signature in that it takes a Value argument in position 0
# Critical Note: This makes assumptions of the work_func signature
# in that it takes a Value argument in position 0
self.work_proc = proc_type(target=self.work_func,
name=self.proc_name,
args=(self.do_work, *self.add_args,))
self.stop_func = stop_func

def start_work(self):
if self.do_work.value:
logger.error("Already working, not starting work")
logger.error(f"({self.proc_name}) -->>: Already working, cannot start")
return
self.do_work.value = True
self.work_proc.start()
logger.debug("Starting work")
logger.debug(f"({self.proc_name}) -->>: Starting work")

def stop_work(self):
logger.debug(f"Calling stop work on {self.proc_name}")
logger.debug(f"({self.proc_name}) -->>: Calling stop work")
if not self.do_work.value:
logger.error(f"Not working, not stopping work on {self.proc_name}")
logger.error(f"({self.proc_name}) -->>: Not working, not stopping work")
return
self.do_work.value = False
logger.debug(f"Sending terminate signal to process {self.proc_name} : {self.work_proc.pid}")
logger.debug(f"({self.proc_name}) -->>: Sending terminate signal to process")
# Send kill signal to work process. # TODO: the exact location of this
# is important. Reflect with self.do_work.get_lock():
self.work_proc.terminate()
if (self.stop_func is not None):
self.stop_func()

logger.debug(f"Ending work, calling join on {self.proc_name}")
logger.debug(f"({self.proc_name}) -->>: Ending work, calling join")
self.work_proc.join()
logger.debug(f"Worker process joined from {self.proc_name}")
logger.debug(f"({self.proc_name}) -->>: Worker process joined")

def work_func(self):
"""
Expand Down

0 comments on commit b8fea68

Please sign in to comment.