Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'sys': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'below)': 0.09; 'caching,': 0.09; 'cleanup': 0.09; 'exit': 0.09; 'exits': 0.09; 'main()': 0.09; 'try:': 0.09; 'def': 0.12; 'thread': 0.14; '#####': 0.16; 'graceful.': 0.16; 'idea:': 0.16; 'main():': 0.16; 'seconds.': 0.16; 'simplified': 0.16; 'subject:program': 0.16; 'termination.': 0.16; 'true:': 0.16; 'skip:= 10': 0.16; 'thanks,': 0.17; 'version.': 0.19; 'starts': 0.20; 'import': 0.22; 'script': 0.25; '(see': 0.26; 'idea': 0.28; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'terminate': 0.31; 'figure': 0.32; 'run': 0.32; 'quite': 0.32; 'subject:from': 0.34; "can't": 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'next': 0.36; 'hi,': 0.36; 'should': 0.36; 'seconds': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'launch': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'first': 0.61; '3-4': 0.68; 'launch.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=Lwwh7eC7dlIGU0JxaZH/8sZ2WEt9/9jPsTsf0TJ01Bc=; b=mSTgklDC9l5IBfBOmwyT+9ObdMhqrfMko6Mcl5Y3iwfh7UqgT5HbxYmr4CgX3uqgmo yrLK1O/u5s+uVwTA1BvU8IeC7I5KbX6exmIO6Vmt/gF5CQ4bPTFIe4iSsH+fsuG3etlm C3RJrDK4xy+nigpbV+7ELWMPzaYEdgXlpv27XWvrtE/KmK/SIXV+wXODdF8IU/0+omyX 7C7K83QoroaNfn+cNakvNB7sG7Y+MNoqFMWOEzES57mOTTnuuKt203JiVtE3mvh/bq0Q 9ZOUSUrsDq76umgVGKXQBm7aqAcr3tZ8rc++DV5VG7r1yb/okEQawjYcASMdXmIs5PQR UhJQ== X-Received: by 10.60.233.227 with SMTP id tz3mr47862603oec.3.1395492052071; Sat, 22 Mar 2014 05:40:52 -0700 (PDT) MIME-Version: 1.0 From: Jabba Laci Date: Sat, 22 Mar 2014 13:40:31 +0100 Subject: terminate a program gracefully from a thread To: Python mailing list Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395492055 news.xs4all.nl 2937 [2001:888:2000:d::a6]:48653 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68770 Hi, I have a script (see below) that I want to terminate after X seconds. The main loop of the program is waiting for user input. The program enters the main loop and I try to shut down the program after X seconds from a thread but I can't figure out how to do it. The program should also do some cleanup before termination, so the shut down must be graceful. The code below is a simplified version. The whole idea is the following: I have a script that has grown quite big over time. It needs to read several data files, so when I start it for the first time, it takes about 3-4 seconds to launch. The next start is much faster since, I guess, the OS has done some caching. I use this script a lot and the first slow launch bothers me. My idea: after booting, I want to start the script in the background in suicide mode. OS does the caching, so when I need it, it starts quickly. See the code below with comments. Thanks, Laszlo =============== import atexit import sys import time from threading import Thread import os def suicide(wait): time.sleep(wait) print("should exit now...") sys.exit() # exits this thread but not the main thread # os._exit(0) # no cleanup with this :( def cleanup(): # I want it to run before termination. print("cleaning up...") def main(): Thread(target=suicide, kwargs={'wait': 3}).start() # while True: try: inp = raw_input("in> ") print(inp) except (KeyboardInterrupt, EOFError): print() sys.exit() ##### if __name__ == "__main__": atexit.register(cleanup) main()