Ejemplo n.º 1
0
    def _run(self):
        self.id = get_ident()
        with RegisterThread(self):
            try:
                if self.target is not None:
                    a, k, self.args, self.kwargs = self.args, self.kwargs, None, None
                    self.end_of_thread.response = self.target(*a, **k)
                    self.parent.remove_child(self)  # IF THREAD ENDS OK, THEN FORGET ABOUT IT
            except Exception as e:
                e = Except.wrap(e)
                with self.synch_lock:
                    self.end_of_thread.exception = e
                with self.parent.child_lock:
                    emit_problem = self not in self.parent.children
                if emit_problem:
                    # 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:
                    with self.child_lock:
                        children = copy(self.children)
                    for c in children:
                        try:
                            DEBUG and 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:
                            DEBUG and 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:
                            DEBUG and sys.stdout.write(str("Joined on thread " + c.name + "\n"))

                    del self.target, self.args, self.kwargs
                    DEBUG and Log.note("thread {{name|quote}} stopping", name=self.name)
                except Exception as e:
                    DEBUG and Log.warning("problem with thread {{name|quote}}", cause=e, name=self.name)
                finally:
                    self.stopped.go()
                    DEBUG and Log.note("thread {{name|quote}} is done", name=self.name)
Ejemplo n.º 2
0
    def _run(self):
        self.id = get_ident()
        with RegisterThread(self):
            try:
                if self.target is not None:
                    a, k, self.args, self.kwargs = self.args, self.kwargs, None, None
                    self.end_of_thread.response = self.target(*a, **k)
                    self.parent.remove_child(self)  # IF THREAD ENDS OK, THEN FORGET ABOUT IT
            except Exception as e:
                e = Except.wrap(e)
                with self.synch_lock:
                    self.end_of_thread.exception = e
                with self.parent.child_lock:
                    emit_problem = self not in self.parent.children
                if emit_problem:
                    # 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:
                    with self.child_lock:
                        children = copy(self.children)
                    for c in children:
                        try:
                            DEBUG and 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:
                            DEBUG and 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:
                            DEBUG and sys.stdout.write(str("Joined on thread " + c.name + "\n"))

                    del self.target, self.args, self.kwargs
                    DEBUG and Log.note("thread {{name|quote}} stopping", name=self.name)
                except Exception as e:
                    DEBUG and Log.warning("problem with thread {{name|quote}}", cause=e, name=self.name)
                finally:
                    self.stopped.go()
                    DEBUG and Log.note("thread {{name|quote}} is done", name=self.name)
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    def _run(self):
        with CProfiler():

            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
                    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(b"ERROR in thread: " +
                                         str(self.name) + b" " + str(e) +
                                         b"\n")
            finally:
                try:
                    children = copy(self.children)
                    for c in children:
                        try:
                            if DEBUG:
                                sys.stdout.write(b"Stopping thread " +
                                                 str(c.name) + b"\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(b"Joining on thread " +
                                                 str(c.name) + b"\n")
                            c.join()
                        except Exception as e:
                            Log.warning("Problem joining thread {{thread}}",
                                        thread=c.name,
                                        cause=e)
                        finally:
                            if DEBUG:
                                sys.stdout.write(b"Joined on thread " +
                                                 str(c.name) + b"\n")

                    self.stopped.go()
                    if DEBUG:
                        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:
                    if DEBUG:
                        Log.warning("problem with thread {{name|quote}}",
                                    cause=e,
                                    name=self.name)
                finally:
                    self.stopped.go()
                    if DEBUG:
                        Log.note("thread {{name|quote}} is done",
                                 name=self.name)