Esempio n. 1
0
    def current():
        ident = get_ident()
        with ALL_LOCK:
            output = ALL.get(ident)

        if output is None:
            Log.warning("this thread is not known. Register this thread at earliest known entry point.")
            return BaseThread(get_ident())
        return output
Esempio n. 2
0
    def current():
        ident = get_ident()
        with ALL_LOCK:
            output = ALL.get(ident)

        if output is None:
            Log.warning(
                "this thread is not known. Register this thread at earliest known entry point."
            )
            return BaseThread(get_ident())
        return output
Esempio n. 3
0
 def current():
     id = get_ident()
     with ALL_LOCK:
         try:
             return ALL[id]
         except KeyError:
             return MAIN_THREAD
Esempio n. 4
0
 def __init__(self):
     BaseThread.__init__(self, get_ident())
     self.name = "Main Thread"
     self.please_stop = Signal()
     self.stopped = Signal()
     self.stop_logging = Log.stop
     self.timers = None
     self.shutdown_locker = allocate_lock()
Esempio n. 5
0
 def __init__(self):
     self.name = "Main Thread"
     self.id = get_ident()
     self.please_stop = Signal()
     self.children = []
     self.stop_logging = Log.stop
     self.timers = None
     self.cprofiler = Null
Esempio n. 6
0
    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
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
0
        Log.warning("programming error", cause=e)
    finally:
        if please_stop:
            Log.note("please_stop has been requested")
        Log.note("done waiting for exit")


def _wait_for_interrupt(please_stop):
    DEBUG and Log.note("wait for stop signal")
    try:
        # ALTERNATE BETWEEN please_stop CHECK AND SIGINT CHECK
        while not please_stop:
            sleep(1)  # LOCKS CAN NOT BE INTERRUPTED, ONLY sleep() CAN
    finally:
        please_stop.go()


MAIN_THREAD = MainThread()


def stop_main_thread(signum=0, frame=None):
    MAIN_THREAD.please_stop.go()


_signal.signal(_signal.SIGTERM, stop_main_thread)
_signal.signal(_signal.SIGINT, stop_main_thread)

ALL_LOCK = allocate_lock()
ALL = dict()
ALL[get_ident()] = MAIN_THREAD
Esempio n. 10
0
 def __init__(self):
     BaseThread.__init__(self, get_ident())
     self.name = "Main Thread"
     self.please_stop = Signal()
     self.stop_logging = Log.stop
     self.timers = None
Esempio n. 11
0
 def __init__(self):
     self.name = "Main Thread"
     self.id = get_ident()
     self.children = []
     self.timers = None
Esempio n. 12
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()
Esempio n. 13
0
 def __init__(self, thread=None):
     if thread is None:
         thread = BaseThread(get_ident())
     self.thread = thread
Esempio n. 14
0
 def __init__(self):
     BaseThread.__init__(self, get_ident())
     self.name = "Main Thread"
     self.please_stop = Signal()
     self.stop_logging = Log.stop
     self.timers = None
Esempio n. 15
0
                _wait_for_interrupt(please_stop)
                break

        # if DEBUG:
        #     Log.note("read line {{line|quote}}, count={{count}}", line=line, count=cr_count)
        if line == "":
            cr_count += 1
        else:
            cr_count = -1000000  # NOT /dev/null

        if line.strip() == "exit":
            Log.alert("'exit' Detected!  Stopping...")
            return


def _wait_for_interrupt(please_stop):
    DEBUG and Log.note("inside wait-for-shutdown loop")
    while not please_stop:
        try:
            sleep(1)
        except Exception:
            pass


MAIN_THREAD = MainThread()

ALL_LOCK = allocate_lock()
ALL = dict()
ALL[get_ident()] = MAIN_THREAD

Esempio n. 16
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)
            except Exception as e:
                e = Except.wrap(e)
                self.end_of_thread.exception = e
                with self.parent.child_locker:
                    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.error(
                            "Problem in thread {{name|quote}}", name=self.name, cause=e
                        )
                    except Exception:
                        sys.stderr.write(
                            str("ERROR in thread: " + self.name + " " + text(e) + "\n")
                        )
            finally:
                try:
                    with self.child_locker:
                        children = copy(self.children)
                    for c in children:
                        try:
                            DEBUG and Log.note("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 Log.note("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 Log.note("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:
                    if not self.ready_to_stop:
                        DEBUG and Log.note("thread {{name|quote}} is done, wait for join", name=self.name)
                        # WHERE DO WE PUT THE THREAD RESULT?
                        # IF NO THREAD JOINS WITH THIS, THEN WHAT DO WE DO WITH THE RESULT?
                        # HOW LONG DO WE WAIT FOR ANOTHER TO ACCEPT THE RESULT?
                        #
                        # WAIT 60seconds, THEN SEND RESULT TO LOGGER
                        (Till(seconds=60) | self.ready_to_stop).wait()

                    self.stopped.go()

                    if not self.ready_to_stop:
                        if self.end_of_thread.exception:
                            # THREAD FAILURES ARE A PROBLEM ONLY IF NO ONE WILL BE JOINING WITH IT
                            try:
                                Log.error(
                                    "Problem in thread {{name|quote}}", name=self.name, cause=e
                                )
                            except Exception:
                                sys.stderr.write(
                                    str("ERROR in thread: " + self.name + " " + text(e) + "\n")
                                )
                        elif self.end_of_thread.response != None:
                            Log.warning(
                                "Thread {{thread}} returned a response, but was not joined with {{parent}} after 10min",
                                thread=self.name,
                                parent=self.parent.name
                            )
                        else:
                            # IF THREAD ENDS OK, AND NOTHING RETURNED, THEN FORGET ABOUT IT
                            self.parent.remove_child(self)
Esempio n. 17
0
 def __init__(self, thread=None, name=None):
     if thread is None:
         thread = BaseThread(get_ident(), name)
     self.thread = thread
Esempio n. 18
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)