473,698 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.lo caltime()[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 1895
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_second s(secs=0):
while seconds() != secs:
time.sleep(.5)

def foo():
# initial wait
wait_for_second s()
do_something()
while True:
time.sleep(50)
wait_for_second s
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(Threadin g.Thread):
def __init__(self):
Threading.Threa d.__init__(self )
self.start()
def run(self,sleep_ time):
while 1:
time.sleep(slee p_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.lo caltime()[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
11435
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 user/password info is in differnet databases, how can I call different validation scripts based on this input ? THANK YOU! See below please. Somethinglike this:
0
1323
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 able. All using IE6 (SP1 or not) Leaving the <input type="file"> unassigned is ok for everyone but when you select a file, even a SMALL SMALL SMALL 10 bytes almost empty txt file, the form fails to be submitted.
3
4358
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 the action is not a URL, can you explain it why. Thanks.
2
1443
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. I'm trying to use JS on a client-side page to prefill "action" of a <form> tag. Here's what I'm using: document.forms.action.value = "http://..." Can someone please give me a hand with this.
1
1995
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
6784
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 Then RunCommand acCmdSaveRecord End If DoCmd.Close or
3
5939
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 AxWebBrowser1.Document.Forms(0).All.item("action-download").click() "action-download" is the name of a submit button
0
8608
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.