Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize thread local value on each thread #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion btalib/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
]


metadata.callstack = [] # keep indicators which are currently being executed
metadata.register('callstack', list) # keep indicators which are currently being executed


_NAMES_IND = {}
Expand Down
4 changes: 2 additions & 2 deletions btalib/meta/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,8 @@ def _applymulti(self, func, *args, raw=False, **kwargs):
# instances, to avoid having them declared as attributes. Or else __setattr__
# would set them as Line objects (or logic would be needed in __setattr__ to
# avoid assigning an object not the real value
metadata.minperiods = {}
metadata.minperiod = {}
metadata.register('minperiods', dict)
metadata.register('minperiod', dict)


class Lines:
Expand Down
19 changes: 18 additions & 1 deletion btalib/meta/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,21 @@

__all__ = ['metadata']

metadata = threading.local()

class Metadata(object):

def __init__(self):
self._metadata = threading.local()

@classmethod
def register(cls, name, default=None):

def get_or_default(self):
if not hasattr(self._metadata, name):
setattr(self._metadata, name, default())
return getattr(self._metadata, name)

setattr(cls, name, property(get_or_default))


metadata = Metadata()