class Credentials: _credentials = {} _initialized = False _lock = RWLock() @staticmethod def load(): with Credentials._lock.write_access: if not Credentials._initialized: for sa_data in ArgumentParser.args.gcp_service_account: credentials = load_credentials(sa_data) for project in list_credential_projects(credentials): Credentials._credentials[project["id"]] = credentials Credentials._initialized = True @staticmethod def get(project_id: str): Credentials.load() with Credentials._lock.read_access: return Credentials._credentials.get(project_id) @staticmethod def all() -> Dict: Credentials.load() with Credentials._lock.read_access: return dict(Credentials._credentials) @staticmethod def reload(): with Credentials._lock.write_access: Credentials._initialized = False Credentials.load()
CLEANUP_FINISH = "cleanup_finish" PROCESS_BEGIN = "process_begin" PROCESS_FINISH = "process_finish" GENERATE_METRICS = "generate_metrics" class Event: """An Event""" def __init__(self, event_type: EventType, data=None) -> None: self.event_type = event_type self.data = data _events = defaultdict(dict) _events_lock = RWLock() def event_listener_registered(event_type: EventType, listener: Callable) -> bool: """Return whether listener is registered to event""" if _events is None: return False return event_type in _events.keys() and listener in _events[event_type].keys() def dispatch_event(event: Event, blocking: bool = False) -> None: """Dispatch an Event""" waiting_str = "" if blocking else "not " log.debug( f"Dispatching event {event.event_type.name} and {waiting_str}waiting for listeners to return" )
def __init_variables(): buffer_ = [] rw_lock = RWLock() threads = [] return (buffer_, rw_lock, threads)
def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.lock = RWLock()
def __setstate__(self, d): d['lock'] = RWLock() self.__dict__.update(d)