1

when I call a function with threading.Timer like below:

threading.Timer(LOOP_TIME,self.broadCast).start()

does broadCast get run in a separate thread? Or is it just in the same thread? I'm using threading.Timer so I can have a function called every so much time interval. I do not want the broadCast function to be called outside of the main thread.

1 Answer 1

2

Yes. You can look into Python threading.py source code:

def Timer(*args, **kwargs):
    """Factory function to create a Timer object.

    Timers call a function after a specified number of seconds:

        t = Timer(30.0, f, args=[], kwargs={})
        t.start()
        t.cancel()     # stop the timer's action if it's still waiting

    """
    return _Timer(*args, **kwargs)

class _Timer(Thread):
    """Call a function after a specified number of seconds:

            t = Timer(30.0, f, args=[], kwargs={})
            t.start()
            t.cancel()     # stop the timer's action if it's still waiting

    """

    def __init__(self, interval, function, args=[], kwargs={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()

Source code available in Python source code repository.

If you want timers and your main thread is not doing co-operative multitasking, I suggest you refactor your code so that you can use it from other threads.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.