473,732 Members | 2,210 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 1897
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
11437
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
1327
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
4362
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
1449
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
2006
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
6796
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
5944
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
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
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,...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
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
4550
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...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.