How to do a sys.exit() using threading.Timer in python?

import threading    
import sys    
from time import time   

def stop():

        print "stop"
        sys.exit()
t = threading.Timer(10, stop)

volts = 22

if volts > 20:

          t.start()    
          print "Start" 

After 10 seconds it prints Stop, which is fine, but it ignores the sys.exit(). I need to do a sys.exit when the timer expires.

Answers


When you run

sys.exit()

inside of a thread, it raises the SystemExit exception. When you call thread.exit(), it raises the same exception, so you are only exiting your thread, not your program.

However in the case of the code you provided, exiting your thread will cause your program to finish as well.


Need Your Help

How do I write this script's results to a file?

powershell directory output

Basic question as I'm learning powershell. At the moment I have a script that give me the output of a directory in a nice clean format. I want to write the results to file rather than to the consol...

How to run a sub when an item within a listbox is double clicked

vb.net

I have a listbox which gradually gets populated with orders as the program is used. However, I want to open a separate form when any item within the listbox is double clicked.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.