0

I'm trying to create a recursive function that would launch a given function in a single thread, and in case of failure would restart it n times and then quit.

Here's my code:

def run_thread_n_times(self, func, **kwargs):

    def _manage_thread(thread_restart_count):
        while (thread_restart_count < MAX_THREAD_RESTART_COUNT):
            try:
                # runs given function
                time.sleep(3)
                raise Exception("fuck")
                func()
            except Exception:
                thread_restart_count += 1
                print(f"Thread  crashed for {thread_restart_count} time(s)")
                _manage_thread(thread_restart_count)

            print(f"Thread crashed more than {MAX_THREAD_RESTART_COUNT} times, exiting...")
            return

    # override thread name if not in kwargs
    name = self.name if "name" not in kwargs else kwargs['name']

    print(f"{name}: thread started")
    thread = Thread(target=_manage_thread, name=name, args=(0,))
    thread.start()

I have raised an exception to simulate a similar event happening in the supplied function.

Normally I expect an output of the likes of:

Thread  crashed for 1 time(s)
Thread  crashed for 2 time(s)
Thread  crashed for 3 time(s)
Thread crashed more than 3 times, exiting...

However, my output is the following:

Thread  crashed for 1 time(s)
Thread  crashed for 2 time(s)
Thread  crashed for 3 time(s)
Thread crashed more than 3 times, exiting...
Thread crashed more than 3 times, exiting...
Thread crashed more than 3 times, exiting...

I do not understand why the last log is printed thrice. What am I doing wrong here?

martineau
  • 119,623
  • 25
  • 170
  • 301
toing_toing
  • 2,334
  • 1
  • 37
  • 79
  • 3
    because you call `_manage_thread` recursively, so it will reach the print statement three times – Matiiss Oct 09 '21 at 23:52
  • does this mean that the rest of the function below the recursive call is also executed every time? – toing_toing Oct 10 '21 at 00:09
  • 1
    yes, after the called function has finished, that is, _"Thread crashed more than 3 times, exiting..."_ first (the first occurrence of this sentence in the console) comes from the last (technically it is the one before last but the last one just returns None without doing anything much) function call (third function is what prints out the first _"Thread crashed more than 3 times, exiting..."_ and the last _"Thread crashed more than 3 times, extiting..."_ is printed by the original function that was started as the thread) – Matiiss Oct 10 '21 at 00:14
  • thank you very much, it's so clear now, I just moved the last print to the previous tab, and moved it to after the return, and it works fine – toing_toing Oct 10 '21 at 00:24
  • 1
    great, the only issue now would be recursion depth but doesn't seem like you would go that deep, also here is a somewhat related question: https://stackoverflow.com/questions/68368929/how-to-get-back-to-the-for-loop-with-the-same-variable-when-exception-occurs/68369014#68369014 – Matiiss Oct 10 '21 at 00:26

0 Answers0