Skip to content

Commit

Permalink
log now at least partially thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
slongwell committed Aug 28, 2019
1 parent d679f48 commit 0617335
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions acqpack/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import types
import time
import threading

# TODO:
# - continously updating display with new events (flag?)
Expand All @@ -15,7 +16,10 @@
# - bug where __init__ time logging doesn't work properly
# - how to handle re-running cells/etc
# - single .track() function
# - simulation (on ordered scale, not necessarily time-based)
# - log state interpreter
# - config .show() to hide fields
# - thread safety https://stackoverflow.com/questions/261683/what-is-meant-by-thread-safe-code
# - 'cls' usage problem: https://stackoverflow.com/questions/4613000/what-is-the-cls-variable-used-for-in-python-classes
class Log():
"""
Expand Down Expand Up @@ -65,6 +69,7 @@ def f2(input):
def __init__(self):
self.df = pd.DataFrame(columns=['t_in', 'ts_in', 't_out', 'ts_out', 'dt',
'class', 'fn', 'in', 'out'])
self._lock = threading.Lock()
self.write = self.track_fn(self.write)
self.write('init')

Expand All @@ -73,12 +78,13 @@ def format_time(self, time_s):
return time.strftime("%Y%m%d_%H:%M:%S", time.localtime(time_s))


def show(self, ascending=True, clear_display=False):
def show(self, ascending=True, clear_display=True):
ret = (self.df[['ts_in', 'ts_out', 'dt',
'class', 'fn', 'in', 'out']].sort_index(ascending=ascending)
.style
.set_properties(subset=['in','out'], **{'text-align': 'left'})
.set_table_styles([dict(selector='th', props=[('text-align', 'left')] ) ]))
.set_properties(**{'text-align': 'left'})
.set_table_styles([dict(selector='th', props=[('text-align', 'left')] ) ])
.format({'dt': "{:.3f}"}))
if clear_display:
disp.clear_output(wait=True)
disp.display(ret)
Expand All @@ -101,10 +107,11 @@ def wrapper(*args):
inputs = args
#obj = str(args[0])[str(args[0]).find('at ')+3:-1]
fn = f.__name__
i = len(self.df)

t_in = time.time()
self.df.loc[i, ['t_in','ts_in','class','fn','in']] = [t_in, self.format_time(t_in), cls, fn, inputs]
with self._lock:
i = len(self.df)
t_in = time.time()
self.df.loc[i, ['t_in','ts_in','class','fn','in']] = [t_in, self.format_time(t_in), cls, fn, inputs]
outputs = f(*args)
t_out = time.time()
self.df.loc[i, ['t_out','ts_out','dt','out']] = [t_out, self.format_time(t_out), t_out-t_in, outputs]
Expand Down

0 comments on commit 0617335

Please sign in to comment.