def current(): ident = get_ident() with ALL_LOCK: output = ALL.get(ident) if output is None: thread = BaseThread(ident) thread.cprofiler = CProfiler() thread.cprofiler.__enter__() with ALL_LOCK: ALL[ident] = thread Log.warning("this thread is not known. Register this thread at earliest known entry point.") return thread return output
def __enter__(self): with ALL_LOCK: ALL[self.thread.id] = self.thread self.thread.cprofiler = CProfiler() self.thread.cprofiler.__enter__() return self
def _run(self): self.id = get_ident() with ALL_LOCK: ALL[self.id] = self try: if self.target is not None: a, k, self.args, self.kwargs = self.args, self.kwargs, None, None self.cprofiler = CProfiler() with self.cprofiler: # PROFILE IN HERE SO THAT __exit__() IS RUN BEFORE THREAD MARKED AS stopped response = self.target(*a, **k) with self.synch_lock: self.end_of_thread = Data(response=response) else: with self.synch_lock: self.end_of_thread = Null except Exception as e: e = Except.wrap(e) with self.synch_lock: self.end_of_thread = Data(exception=e) if self not in self.parent.children: # THREAD FAILURES ARE A PROBLEM ONLY IF NO ONE WILL BE JOINING WITH IT try: Log.fatal("Problem in thread {{name|quote}}", name=self.name, cause=e) except Exception: sys.stderr.write( str("ERROR in thread: " + self.name + " " + text_type(e) + "\n")) finally: try: children = copy(self.children) for c in children: try: if DEBUG: sys.stdout.write( str("Stopping thread " + c.name + "\n")) c.stop() except Exception as e: Log.warning("Problem stopping thread {{thread}}", thread=c.name, cause=e) for c in children: try: if DEBUG: sys.stdout.write( str("Joining on thread " + c.name + "\n")) c.join() except Exception as e: Log.warning("Problem joining thread {{thread}}", thread=c.name, cause=e) finally: if DEBUG: sys.stdout.write( str("Joined on thread " + c.name + "\n")) self.stopped.go() DEBUG and Log.note("thread {{name|quote}} stopping", name=self.name) del self.target, self.args, self.kwargs with ALL_LOCK: del ALL[self.id] except Exception as e: DEBUG and Log.warning("problem with thread {{name|quote}}", cause=e, name=self.name) finally: DEBUG and Log.note("thread {{name|quote}} is done", name=self.name) self.stopped.go()