0

I have a function:

def threaded(fn):
    def wrapper(*args, **kwargs):
        thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
        thread.start()
        return thread
    return wrapper

And a class:

class someclass(somestuff):
    def __init__(self, parent=None):
        super(thing)
        .
        .
        .

    @threaded
    def start_thread(self):
        while True:
            self.update_watchers()

    def update_watchers(self):
        .
        .
        .

    def otherfunctions(self):
        .
        .
        .
        .

And in my main:

obj = someclass()
handle = obj.start_thread()
handle.join()

Now, if I take the while loop out of start_thread, everything works fine. I suspect I am using forever-threads long (a forever thread is a thread that runs as long as the program is running, of course).

As an FYI, this program occupies a list. Not a Python list, a GUI list. It looks into a file that is constantly being updated. update_watchers() updates the watchers I have running over the file, extracts information when (for example) the program is paused (which is one of the several reasons why I need a thread).

Can someone explain to me what is going on? It keeps breaking when I get to handle's definition. I know I must be using the While loop wrong and there is a correct way to do it. I need start_thread to run all the time because I need update_watchers to be running all the time.

I cant really post more code than that.

Thank you

Edit: threaded function taken from this post

John Lexus
  • 2,252
  • 2
  • 10
  • 21
  • You posted too much code and at the same time not enough. Extract a minimal but complete example, as it stands your question is off-topic. – Ulrich Eckhardt Nov 16 '17 at 20:49
  • 1
    Do you want to run what's inside `while True:` and than stop that thread somewhere in the code? If you want that thread to work from start to end of program you should consider interrupting this thread or making something like [here](https://stackoverflow.com/a/36499538/4984268) – Iluvatar Nov 16 '17 at 21:14
  • @Iluvatar Thanks! That's exactly what I was gonna look up next! – user3026388 Nov 16 '17 at 21:17

1 Answers1

1

The call to handle.join() waits until the thread represented by handle completes. Since it has a while True it will never complete, and thus the join will never terminate. You almost certainly don't want to join on a thread you intend to run forever. Solving your problem is likely as simple as removing handle.join().

Sam Hartman
  • 5,613
  • 2
  • 19
  • 34