Skip to content Skip to sidebar Skip to footer

How To Kill This Threading.timer?

I have launched a thread using a Timer object. Now, I want to stop this thread but I can't. I have used cancel(), but it doesn't work. I don't know why. import threading import tim

Solution 1:

The problem here can be solved by reading the documentation more carefully. A timer thread's cancel method "... will only work if the timer is still in its waiting stage."

By the time you call t.cancel the timer has fired, and its associated function is executing so it is no longer "in its waiting stage", even though the function actually spends most of its time sleeping - this does NOT mean it's in its waiting stage, which terminates when the timer fires..

More generally there is no way to kill a thread without its active cooperation, as severalSO answers already summarize well.

Rather than using cancel you should set some sort of flag that function looks at to determine whether it should terminate its loop (and therefore the whole thread) or not.

Solution 2:

Try:

del threadTimer

I don't this will work or not, but the cancel method should be used on the waiting stage. See the documentation for more help. You are trying to use cancel the timer after it's waiting stage. If the del statement doesn't work, try this:

threadTimer = None

Hopefully some of this helps.

Post a Comment for "How To Kill This Threading.timer?"