Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wed,': 0.12; '(last': 0.16; 'bieber': 0.16; 'declaimed': 0.16; 'email addr:ix.netcom.com': 0.16; 'email name:wlfraed': 0.16; 'from:addr:ix.netcom.com': 0.16; 'from:addr:wlfraed': 0.16; 'from:name:dennis lee bieber': 0.16; 'function?': 0.16; 'received:66.245': 0.16; 'received:dsl.mindspring.com': 0.16; 'received:mindspring.com': 0.16; 'received:wlfraed': 0.16; 'seconds,': 0.16; 'subject:function': 0.16; 'unsigned,': 0.16; 'url:netcom': 0.16; 'url:wlfraed': 0.16; 'wulfraed': 0.16; 'argument': 0.16; '>>>': 0.16; 'def': 0.16; '(most': 0.21; 'seconds': 0.21; '-0700': 0.23; 'runs': 0.23; 'traceback': 0.25; 'url:home': 0.25; 'function': 0.26; 'lee': 0.28; 'all,': 0.28; 'import': 0.29; 'delay': 0.29; 'second': 0.29; 'sleep': 0.30; 'print': 0.32; 'to:addr:python-list': 0.34; 'header:X-Complaints- To:1': 0.34; 'there': 0.34; '(as': 0.34; 'last):': 0.35; 'charset :us-ascii': 0.36; 'file': 0.36; 'but': 0.37; 'received:org': 0.38; 'takes': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'skip:4 10': 0.40; 'called': 0.40; 'skip:d 20': 0.40; 'skip:1 10': 0.66; 'limit': 0.66; 'dennis': 0.77; 'subject:call': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: how to call a function for evry 10 secs Date: Wed, 29 Jun 2011 22:06:22 -0700 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: user-11fa86a.dsl.mindspring.com X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 119 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1309410399 news.xs4all.nl 21765 [2001:888:2000:d::a6]:48956 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8591 On Wed, 29 Jun 2011 10:39:26 -0700 (PDT), hisan declaimed the following in gmane.comp.python.general: > Hi All, > I need to call a function for evry 10 secs > how can i achieve this in python Do you mean you need a 10 second delay BETWEEN each call of the function? OR do you mean the function must be called every 10 seconds (as long as the function completes in less than 10 seconds). The first is the simple one that's no doubt been shown already >>> import time >>> >>> def dumpTime(t): ... print t ... time.sleep(5) ... >>> while True: ... dumpTime(time.clock()) ... time.sleep(10) ... 68.0374675818 83.0283211852 98.0280303107 113.027740058 128.027449863 143.027157991 158.026871578 Traceback (most recent call last): File "", line 2, in File "", line 3, in dumpTime KeyboardInterrupt >>> The other gets more complex >>> import time >>> >>> def dumpTime(t): ... print t ... time.sleep(5) ... >>> while True: ... last = time.clock() ... dumpTime(last) ... time.sleep((last + 10.0) - time.clock()) ... 199.787792062 209.791497063 219.791306975 229.79110609 239.790991578 249.790718419 Traceback (most recent call last): File "", line 3, in File "", line 3, in dumpTime KeyboardInterrupt >>> But if the function itself runs for longer than 10 seconds, there will be a major problem, as the sleep apparently takes the argument as unsigned, and a negative number is a very big sleep! >>> import time >>> >>> def dumpTime(t): ... print t ... time.sleep(11) ... >>> while True: ... last = time.clock() ... dumpTime(last) ... time.sleep((last + 10.0) - time.clock()) ... 403.96232976 Traceback (most recent call last): File "", line 4, in KeyboardInterrupt >>> time.clock() 498.87339572032164 >>> Better limit the sleep >>> import time >>> >>> def dumpTime(t): ... print t ... time.sleep(11) ... >>> while True: ... last = time.clock() ... dumpTime(last) ... time.sleep(max(0.0, (last + 10.0) - time.clock())) ... 608.521519761 619.517915274 630.517711063 641.5175254 652.517293743 663.517072219 674.516939156 685.516625507 696.516427311 707.516240174 718.515978656 729.51577823 740.515593446 Traceback (most recent call last): File "", line 3, in File "", line 3, in dumpTime KeyboardInterrupt -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/