Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8570 > unrolled thread

how to call a function for evry 10 secs

Started byhisan <santosh.ssit@gmail.com>
First post2011-06-29 10:39 -0700
Last post2011-06-30 11:42 -0600
Articles 12 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  how to call a function for evry 10 secs hisan <santosh.ssit@gmail.com> - 2011-06-29 10:39 -0700
    Re: how to call a function for evry 10 secs Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-30 03:42 +1000
    Re: how to call a function for evry 10 secs Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-06-29 22:06 -0700
      Re: how to call a function for evry 10 secs Laurent Claessens <moky.math@gmail.com> - 2011-06-30 09:05 +0200
      Re: how to call a function for evry 10 secs Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-06-30 10:37 +0200
        Re: how to call a function for evry 10 secs Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-06-30 09:42 -0700
          Re: how to call a function for evry 10 secs Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-06-30 21:04 +0200
            Re: how to call a function for evry 10 secs Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-06-30 23:29 -0700
            Re: how to call a function for evry 10 secs Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-07-01 08:50 +0200
        Re: how to call a function for evry 10 secs MRAB <python@mrabarnett.plus.com> - 2011-06-30 18:18 +0100
        Re: how to call a function for evry 10 secs Chris Angelico <rosuav@gmail.com> - 2011-07-01 03:36 +1000
        Re: how to call a function for evry 10 secs Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-30 11:42 -0600

#8570 — how to call a function for evry 10 secs

Fromhisan <santosh.ssit@gmail.com>
Date2011-06-29 10:39 -0700
Subjecthow to call a function for evry 10 secs
Message-ID<f63802d3-da39-4efb-a09a-89458a9c1f92@h38g2000pro.googlegroups.com>
Hi All,
I need to call a function for evry 10 secs
how can i achieve this in python

[toc] | [next] | [standalone]


#8572

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-06-30 03:42 +1000
Message-ID<4e0b63eb$0$29996$c3e8da3$5496439d@news.astraweb.com>
In reply to#8570
hisan wrote:

> Hi All,
> I need to call a function for evry 10 secs
> how can i achieve this in python


import time
while True:
    time.sleep(10)
    function()



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#8591

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2011-06-29 22:06 -0700
Message-ID<mailman.518.1309410399.1164.python-list@python.org>
In reply to#8570
On Wed, 29 Jun 2011 10:39:26 -0700 (PDT), hisan <santosh.ssit@gmail.com>
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 "<stdin>", line 2, in <module>
  File "<stdin>", 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 "<stdin>", line 3, in <module>
  File "<stdin>", 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 "<stdin>", line 4, in <module>
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 "<stdin>", line 3, in <module>
  File "<stdin>", line 3, in dumpTime
KeyboardInterrupt

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#8593

FromLaurent Claessens <moky.math@gmail.com>
Date2011-06-30 09:05 +0200
Message-ID<iuh77p$fb7$1@news.univ-fcomte.fr>
In reply to#8591
> 	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!

Launch each call in a separate thread. If the calls are independent, 
this could be a solution.

Laurent

[toc] | [prev] | [next] | [standalone]


#8598

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2011-06-30 10:37 +0200
Message-ID<mvotd8-v5k.ln1@satorlaser.homedns.org>
In reply to#8591
Dennis Lee Bieber wrote:
> 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!

"time.sleep()" takes a floating point number, so an underflow like for 
fixed-size integers in C shouldn't happen. What puzzles me here is your use 
of "apparently", because here a negative value actually raises an "IOError: 
[Errno 22] Invalid argument" when I call "sleep(-1)".

The system I'm on is a Debian GNU/Linux system running on some x86 hardware, 
for the record, and I'm using Python 2.6.6.

Uli

-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

[toc] | [prev] | [next] | [standalone]


#8607

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2011-06-30 09:42 -0700
Message-ID<mailman.526.1309452179.1164.python-list@python.org>
In reply to#8598
On Thu, 30 Jun 2011 10:37:34 +0200, Ulrich Eckhardt
<ulrich.eckhardt@dominolaser.com> declaimed the following in
gmane.comp.python.general:

> "time.sleep()" takes a floating point number, so an underflow like for 
> fixed-size integers in C shouldn't happen. What puzzles me here is your use 
> of "apparently", because here a negative value actually raises an "IOError: 
> [Errno 22] Invalid argument" when I call "sleep(-1)".
> 
> The system I'm on is a Debian GNU/Linux system running on some x86 hardware, 
> for the record, and I'm using Python 2.6.6.
>
	WinXP, Python 2.5.<something>

	And that was a direct cut&paste from a command window; showing it
had slept for some 90 seconds before I killed it.

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#8614

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2011-06-30 21:04 +0200
Message-ID<vntud8-898.ln1@satorlaser.homedns.org>
In reply to#8607
Dennis Lee Bieber wrote:
> And that was a direct cut&paste from a command window; showing it
> had slept for some 90 seconds before I killed it.

Interesting. Just tried a 2.7.2 on a 32-bit MS Windows with following 
results:

1. sleep(5 - 2**32) sleeps for a few seconds
2. sleep(-1) sleeps much longer

So this seems to confirm that it's a 32-bit underflow while preparing the 
argument for win32's Sleep() function.

That said, an "IOError" is a bit better but still leaves room for 
improvement. I'll take this to the developers mailinglist and see if they 
consider the behaviour a bug. At the very least the docs are bad, I would 
say.


Cheers!

Uli


-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

[toc] | [prev] | [next] | [standalone]


#8624

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2011-06-30 23:29 -0700
Message-ID<mailman.535.1309501797.1164.python-list@python.org>
In reply to#8614
On Thu, 30 Jun 2011 21:04:56 +0200, Ulrich Eckhardt
<ulrich.eckhardt@dominolaser.com> declaimed the following in
gmane.comp.python.general:

> 
> So this seems to confirm that it's a 32-bit underflow while preparing the 
> argument for win32's Sleep() function.
> 
> That said, an "IOError" is a bit better but still leaves room for 
> improvement. I'll take this to the developers mailinglist and see if they 
> consider the behaviour a bug. At the very least the docs are bad, I would 
> say.
> 
	"RuntimeError" or "OSError" would seem to be better than "IOError";
though having the Python runtime incorporate a max(delay, 0) behind the
scenes would prevent both OS type behaviors.

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#8626

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2011-07-01 08:50 +0200
Message-ID<n370e8-h9t.ln1@satorlaser.homedns.org>
In reply to#8614
Ulrich Eckhardt wrote:
> I'll take this to the developers mailinglist and see if they
> consider the behaviour a bug.

Filed as bug #12459.

Uli

-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

[toc] | [prev] | [next] | [standalone]


#8609

FromMRAB <python@mrabarnett.plus.com>
Date2011-06-30 18:18 +0100
Message-ID<mailman.527.1309454291.1164.python-list@python.org>
In reply to#8598
On 30/06/2011 17:42, Dennis Lee Bieber wrote:
> On Thu, 30 Jun 2011 10:37:34 +0200, Ulrich Eckhardt
> <ulrich.eckhardt@dominolaser.com>  declaimed the following in
> gmane.comp.python.general:
>
>> "time.sleep()" takes a floating point number, so an underflow like for
>> fixed-size integers in C shouldn't happen. What puzzles me here is your use
>> of "apparently", because here a negative value actually raises an "IOError:
>> [Errno 22] Invalid argument" when I call "sleep(-1)".
>>
>> The system I'm on is a Debian GNU/Linux system running on some x86 hardware,
>> for the record, and I'm using Python 2.6.6.
>>
> 	WinXP, Python 2.5.<something>
>
> 	And that was a direct cut&paste from a command window; showing it
> had slept for some 90 seconds before I killed it.
>
Looks like it hasn't changed even in WinXP, Python 3.2.

Is IOError what you'd expect, anyway?

What should it do in Python 3.2? Exception or max(seconds, 0)?

[toc] | [prev] | [next] | [standalone]


#8610

FromChris Angelico <rosuav@gmail.com>
Date2011-07-01 03:36 +1000
Message-ID<mailman.528.1309455375.1164.python-list@python.org>
In reply to#8598
On Fri, Jul 1, 2011 at 3:18 AM, MRAB <python@mrabarnett.plus.com> wrote:
> Looks like it hasn't changed even in WinXP, Python 3.2.
>
> Is IOError what you'd expect, anyway?
>
> What should it do in Python 3.2? Exception or max(seconds, 0)?

The obvious thing for it to do is to go back in time and resume
executing that many seconds ago, but failing that, I would have it do
the latter. It's already documented as potentially sleeping a bit
longer than the specified time (for instance, it's rounded up to the
minimum resolution, and task switches can increase the time), so it's
not much of a stretch to have negative sleep times result in a
yield/context switch and no further sleeping.

ChrisA

[toc] | [prev] | [next] | [standalone]


#8611

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-06-30 11:42 -0600
Message-ID<mailman.529.1309455805.1164.python-list@python.org>
In reply to#8598
On Thu, Jun 30, 2011 at 11:18 AM, MRAB <python@mrabarnett.plus.com> wrote:
>>        And that was a direct cut&paste from a command window; showing it
>> had slept for some 90 seconds before I killed it.
>>
> Looks like it hasn't changed even in WinXP, Python 3.2.

It gets cast to an unsigned long, so I expect sleep(-1) on Windows
would sleep for 2 ** 32 - 1000 ms, or about 49.7 days.

More interestingly, sleep(-0.001) should pass 2**32 - 1 to the
underlying Windows API, which would cause it to sleep forever (or
until interrupted).

> Is IOError what you'd expect, anyway?
>
> What should it do in Python 3.2? Exception or max(seconds, 0)?

sleep(0) has some special semantics on Windows.  Raising ValueError
(or IOError to match Linux) makes the most sense to me.

Cheers,
Ian

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web