473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

do "some action" once a minute

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

May 10 '06 #1
7 1877
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
May 10 '06 #2
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

May 10 '06 #3
Petr Jakes wrote:
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
May 10 '06 #4
Petr Jakes wrote:
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
May 10 '06 #5
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"

May 10 '06 #6
On 2006-05-09, Petr Jakes <pe**@tpc.cz> wrote:
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.
May 10 '06 #7
Petr Jakes wrote:
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
May 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sylvie Stone | last post by:
Hi group - I have an html form for that uses username and password to login to a specific area of the website. The "area" the user wants to go to is based on a pull down menu. Becasue the...
0
by: ginette | last post by:
I really need help with a strange problem. i have a page having ONLY this cleaned and washed HTML code, no client scripting at all and "some" folks are unable to post any file till "some" are...
3
by: Zhidian Du | last post by:
I am using java to write to url, http://fatigo.bioinfo.cnio.es/ , but I found the form is : <form method="post" action="/" enctype="multipart/form-data" name="fatigo"> I have no idea why...
2
by: Don | last post by:
Can someone tell me what I'm doing wrong here. I can't believe I'm having this kind of problem. Think maybe I've been working on this for too long, and everything is starting to look the same. ...
1
by: tnhoe | last post by:
Hi, <Form method='post' action="next.htm?btn="+"this.myform.myobj.value"> What is the correct syntax for above ? Regards Hoe
1
by: RC | last post by:
If I want to explicitly save the record before executing a Close action. Which of the following should I use? Or does it depend on whether it is based on a Form or something else? If Me.Dirty...
3
by: MeNotHome | last post by:
I am trying to automate web browser navigation and form fill out in vb.net Why doesn't this work? AxWebBrowser1.Document.Forms(0).All("action-download").click() I also tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.