Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'desired.': 0.07; 'exception.': 0.07; 'exceeds': 0.09; 'exception,': 0.09; 'stderr': 0.09; 'throw': 0.09; 'def': 0.14; '"""run': 0.16; '2",': 0.16; 'alarm': 0.16; 'command,': 0.16; 'hints,': 0.16; 'seconds.': 0.16; 'time.time()': 0.16; 'shell': 0.18; 'please?': 0.22; 'terminate': 0.22; 'try:': 0.22; 'pass': 0.22; 'code,': 0.23; 'raise': 0.24; 'executing': 0.27; 'time:': 0.27; 'message-id:@mail.gmail.com': 0.28; 'command': 0.28; 'skip:( 20': 0.28; 'reset': 0.29; 'to:name :python-list': 0.31; "i'd": 0.31; 'seconds': 0.31; 'certain': 0.31; 'code': 0.31; 'run': 0.32; 'skip:d 20': 0.32; 'class': 0.33; 'received:google.com': 0.34; 'to:addr:python-list': 0.35; 'execution': 0.35; 'generic': 0.35; 'except': 0.36; 'skip:g 20': 0.37; 'skip:p 20': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'some': 0.40; 'within': 0.64 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=J1kfy5nGjZiryNjm4ag9ngbuAPdcVMfti00PtuUupng=; b=ANhqZ1dBHcRv3zON9UFjTCGCrBRJGH0XLyG7chLj0dOJeZBQFH0viMG/fo3Zdz+cXm nxru6e4ikAvk7rFQb2zQuBTEk1MELgSV3T9xBsqY4e4MOj+ARTZy1v/yfwliK34D0Ob1 upQkGFLHPcQNRIA50+cnpTTwLJYQPyVILIew6akcpgeQhnq5Y8q/JOC/YNpBkTUSumAK B3m5neviwk48obDb8Y/tkld9ilbVkahNfXPI7Jg4H3amws30/XOu6Wrv0E14b5uukrdG YXiH1E/elyft4b7DISBe0+7bAGxvTqTOrdsHe9DRIk6wKllkM3Qr58MMt/AjMdQS9ECQ S3Sw== MIME-Version: 1.0 X-Received: by 10.180.88.72 with SMTP id be8mr12540276wib.45.1432347354727; Fri, 22 May 2015 19:15:54 -0700 (PDT) Date: Fri, 22 May 2015 20:15:54 -0600 Subject: decorators and alarms From: Jason Friedman To: python-list Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432347717 news.xs4all.nl 2944 [2001:888:2000:d::a6]:35339 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91087 I have the following code to run a shell command and kill it if it does not return within a certain amount of time: def run_with_time_limit(command, limit): """Run the given command via a shell. Kill the command, and raise an exception, if the execution time exceeds seconds. Else return (exit_code, stdout, stderr, duration).""" class Alarm(Exception): pass def alarm_handler(signum, frame): raise Alarm start_stamp = time.time() signal.signal(signal.SIGALRM, alarm_handler) signal.alarm(limit) try: process = subprocess.Popen( command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) stdout, stderr = process.communicate() duration = time.time() - start_stamp signal.alarm(0) # reset the alarm except Alarm: process.kill() raise Alarm("The command did not return within %(limit)s seconds." % locals()) return (process.returncode, stdout, stderr, duration) run_with_time_limit("sleep 2", 1) This works as desired. But, I'd like to expand this to take some generic code, not just a shell command, and terminate it if it does not return quickly. @time_limiter def generic_function(arg1, arg2, time_limit=10): do_some_stuff() do_some_other_stuff() return val1, val2 If generic_function does not finish within 10 seconds it would stop executing and throw an exception. May I have some hints, please?