Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8103695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:37:24+00:00 2026-06-05T23:37:24+00:00

I noticed the following behavior in the following code (using threading.Timer class): import threading

  • 0

I noticed the following behavior in the following code (using threading.Timer class):

import threading

def ontimer():
    print threading.current_thread()

def main():
    timer = threading.Timer(2, ontimer)
    timer.start()
    print threading.current_thread()
    timer.cancel()
    if timer.isAlive():
        print "Timer is still alive"
    if timer.finished:
        print "Timer is finished"


 if __name__ == "__main__":
main()

The output of the code is:

<_MainThread(MainThread, started 5836)>
Timer is still alive
Timer is finished

As We notice from the output, that the timer object is still alive and finished in the same time.

In fact, I would like to call a similar function hundreds of times, and I wonder whether those “living” timers may affect the performance.

I would like to stop or cancel the timer object in a proper way. Am I doing it right?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:37:26+00:00Added an answer on June 5, 2026 at 11:37 pm

    A Timer is a subclass of a Thread and its implementation is really simple. It waits the provided time by subscribing to the event finished.

    So when you set the event by Timer.cancel it is guaranteed that the function does not get called. But it is not guaranteed that the Timer thread will directly continue (and exit).

    So the point is that the thread of the timer can still be alive after the execution of cancel, but the function will not get executed. So checking for finished is safe, while testing for Thread.is_alive (newer API, use this!) is a race condition in this case.

    Hint: You can verify this by placing a time.sleep after calling cancel. Then it will just print:

    <_MainThread(MainThread, started 10872)>
    Timer is finished
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed (and verified in the sunspot code) the following behavior class Foo <
I was writing a pluginable function when I noticed the following behavior (tested in
I am using the embedded version of jetty. I have noticed the following: in
I have the following sample code and noticed that if I attempt to use
Consider the following short code snippet. namespace B { public class Foo { public
I'm using the following code to display a context menu on my form when
I was writing code and I noticed some odd behavior in Groovy when I
Just noticed the __block keyword in some Objective-c code like the following: // myString
I've noticed the following pattern recently, but I don't entirely grasp the usage of
I've just noticed this following , relatively new thing on Facebook, when you upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.