do "some action" once a minute

P

Petr Jakes

I would like to do "some action" once a minute. My code (below) works,
I just wonder if there is some more pythonic approach or some "trick"
how to do it differently.

minutes=time.localtime()[4]
while 1:
min, sec = time.localtime()[4:6]
if sec==0 and minutes!=min: # first occur of sec==0 only!! polling
10x a second
minutes=min
print "Eureca"
time.sleep(0.1)

Regards

Petr Jakes
 
S

Sybren Stuvel

Petr Jakes enlightened us with:
I would like to do "some action" once a minute. My code (below)
works, I just wonder if there is some more pythonic approach or some
"trick" how to do it differently.

I'd use the Threading module, and the Timer object from that module to
be more precise. There you can simply say "call this function in 60
seconds" or something similar. Then your program just sleeps until
it's time to call the function.

Sybren
 
D

Dennis Lee Bieber

I would like to do "some action" once a minute. My code (below) works,
I just wonder if there is some more pythonic approach or some "trick"
how to do it differently.
Your code is at conflict with your description...

Your description says merely "once a minute"; the code says "once ON
THE MINUTE".

time.sleep(60.0)

will mean that, at most, it will occur once per minute (but may be
delayed a few milliseconds depending on system load and priorities).
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

Petr Jakes

Thanks for your comment. It is mainly English issue (I am not native
English speaker).

OK, to be more specific, I would like to run the code, when the value
of seconds in the timestamp become say "00".
The whole code will run in the infinitive loop and other actions will
be executed as well, so it can not "sleep" for 60 seconds :).

Regards

Petr
 
D

Diez B. Roggisch

Petr said:
Thanks for your comment. It is mainly English issue (I am not native
English speaker).

OK, to be more specific, I would like to run the code, when the value
of seconds in the timestamp become say "00".
The whole code will run in the infinitive loop and other actions will
be executed as well, so it can not "sleep" for 60 seconds :).

The you have to go for a thread, as otherwise you'd have to create all sorts
of checks in your program to check for the current date. That therad then
could wait like this:


import time
def seconds():
return time.localtime()[5]


def wait_for_seconds(secs=0):
while seconds() != secs:
time.sleep(.5)

def foo():
# initial wait
wait_for_seconds()
do_something()
while True:
time.sleep(50)
wait_for_seconds
do_something()


Diez
 
I

Irmen de Jong

Petr said:
OK, to be more specific, I would like to run the code, when the value
of seconds in the timestamp become say "00".
The whole code will run in the infinitive loop and other actions will
be executed as well, so it can not "sleep" for 60 seconds :).

Have a look at my 'Kronos' task scheduler (based on the sched module).
Available via http://www.razorvine.net/downloads.html

It may provide the functionality you want.

--Irmen
 
J

jordan.taylor2

This is why your best bet is probably threads.

Class Eureka(Threading.Thread):
def __init__(self):
Threading.Thread.__init__(self)
self.start()
def run(self,sleep_time):
while 1:
time.sleep(sleep_time)
print "eureka"
 
B

Ben C

I would like to do "some action" once a minute.

You can try the sched module (import sched).

You give it a time at which to call a callback. Then in the callback you
can reset the "alarm" for a minute later, using enterabs.

If the task might take longer than a minute, it just means you'll be
setting an alarm for a time in the past, but it should still get called.
But there is another problem, which is that the queue will presumably
get very big over time, so you'll need to clear it out now and again
with empty()... which means keeping track of the event objects yourself,
which is annoying.
 
L

Larry Bates

Petr said:
I would like to do "some action" once a minute. My code (below) works,
I just wonder if there is some more pythonic approach or some "trick"
how to do it differently.

minutes=time.localtime()[4]
while 1:
min, sec = time.localtime()[4:6]
if sec==0 and minutes!=min: # first occur of sec==0 only!! polling
10x a second
minutes=min
print "Eureca"
time.sleep(0.1)

Regards

Petr Jakes
What platform? If it is Windows, they write your application
as a Windows Service and have it sleep for however long you
want. It won't impact your system looping and sleeping.
It will also be asynchronous, as it will sleep for time you
specify, run your code to completion and then sleep again
which isn't the same as running every one minute.

If it is Linux, others have answered separately.

-Larry Bates
 
D

Dennis Lee Bieber

OK, to be more specific, I would like to run the code, when the value
of seconds in the timestamp become say "00".
The whole code will run in the infinitive loop and other actions will
be executed as well, so it can not "sleep" for 60 seconds :).
Simplest solution would be to put the timed portion into a separate
thread, with a long enough sleep call to minimize task switching
overhead. However, even in a thread, you can not enforce that a sleep
will end on any given portion of the time -- if the other code calls a
poorly written C-extension that doesn't give up the global interpreter
lock until it is complete (say it runs 40 seconds and your sleep on set
for 10 seconds -- that sleep may not return for up to 50 seconds
[9.99999 seconds of sleep, and then the 40 second extension started]).
No matter what you put in for the sleep value, that only specifies a
/minimum/ period.

Within this thread, something like the following will set the
sleep...
.... t0 = time.time()
.... t1 = 60.0 + (t0 - (t0 % 60.0))
.... time.sleep(t1 - t0)
.... now = time.time()
.... print t1, t0, t1 - t0, now
.... print time.ctime(t1), time.ctime(t0), time.ctime(now)
.... 1147195680.0 1147195627.13 52.875 1147195680.0
Tue May 09 10:28:00 2006 Tue May 09 10:27:07 2006 Tue May 09 10:28:00
20061147195740.0 1147195688.52 51.4839999676 1147195740.0
Tue May 09 10:29:00 2006 Tue May 09 10:28:08 2006 Tue May 09 10:29:00
2006
.... to wake up on the next minute. Notice how my keyboard entries
resulted in sleeps of ~52 seconds. This scheme adjusts for the
processing performed during each activation, and the inherent
uncertainty in wake up time.


The only other means would be something of a discrete event
dispatcher, where ALL of your code is written to do maybe one or two
statements at a time and return, and the main program just increments
time and calls each operation that is supposed to run on that time
step... Lot's of overhead, difficult to code number crunchers to return
in mid-computation... etc.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

James Stroud

Petr said:
I would like to do "some action" once a minute. My code (below) works,
I just wonder if there is some more pythonic approach or some "trick"
how to do it differently.

minutes=time.localtime()[4]
while 1:
min, sec = time.localtime()[4:6]
if sec==0 and minutes!=min: # first occur of sec==0 only!! polling
10x a second
minutes=min
print "Eureca"
time.sleep(0.1)

Regards

Petr Jakes

This may be what you want:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/464959

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top