473,287 Members | 1,714 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,287 software developers and data experts.

Re: Continuous Timer

En Fri, 30 May 2008 22:50:13 -0300, Robert Dailey <rc******@gmail.com>
escribió:
Reading through the Python 2.5 docs, I'm seeing a Timer class in the
threading module, however I cannot find a timer object that will
continuously call a function of my choice every XXXX amount of
milliseconds.
For example, every 1000 milliseconds I want a function named Foo to be
called. This would continue to happen until I terminate the timer in my
main
thread. Thanks for the help.
Use an Event object; its wait() will provide the sleep time, and when it
is set() the thread knows it has to exit.

import threading
import time

def repeat(event, every, action):
while True:
event.wait(every)
if event.isSet():
break
action()

def foo():
print "I'm bored to death..."

print "creating event and thread"
ev = threading.Event()
t1 = threading.Thread(target=repeat, args=(ev, 1.0, foo))
print "starting thread"
t1.start()
print "waiting for 10 seconds in main thread"
time.sleep(10)
print "setting event"
ev.set()
print "waiting for thread to finish"
t1.join()
print "quit"

--
Gabriel Genellina

Jun 27 '08 #1
1 2039
Gabriel Genellina wrote:
En Fri, 30 May 2008 22:50:13 -0300, Robert Dailey <rc******@gmail.com>
escribió:
>Reading through the Python 2.5 docs, I'm seeing a Timer class in the
threading module, however I cannot find a timer object that will
continuously call a function of my choice every XXXX amount of
milliseconds.
For example, every 1000 milliseconds I want a function named Foo to be
called. This would continue to happen until I terminate the timer in
my main
thread. Thanks for the help.

Use an Event object; its wait() will provide the sleep time, and when it
is set() the thread knows it has to exit.

import threading
import time

def repeat(event, every, action):
while True:
event.wait(every)
if event.isSet():
break
action()
Actually, to do this right, it's necessary to account for the time used by
"action". The code above will run no sooner than the time "every" after
the COMPLETION of action.

I've done this sort of thing under QNX, the real-time operating system,
which has better timing primitives, and seen the action executed within
a few microseconds of the correct time, every time. But that was in C++.

If you're trying to do hard real time in Python on Linux or Windows,
don't expect reliable timing. Remember, Python isn't really preemptive,
because of the global interpreter lock and the lack of thread priorities.

John Nagle
Jun 27 '08 #2

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

Similar topics

19
by: Nicolas Pernetty | last post by:
Hello, I'm looking for any work/paper/ressource about continuous system simulation using Python or any similar object oriented languages (or even UML theory !). I'm aware of SimPy for...
3
by: Prakash Wadhwani | last post by:
Is there any EASY way to highlight a full row in a continuous form so that as i navigate up & down the table/continuous form using the arrow keys, the entire line (all fields) get highlighted ? ...
13
by: Jeff Pritchard | last post by:
I have an MDB that contains a single table. Checkweigher data is being continuously written to this MDB from a dedicated workstation over a network into the MDB on the server at the rate of about...
3
by: Mark | last post by:
Hi there, I have a subform, set as a continuous form. When a user selects a particular record in that subform, how can I make that particular record stand out (color or font change, size, etc) from...
0
by: Dave | last post by:
Hi, I have a form that displays jobs and the time that is left before the job runs over. there is a timer on the form that runs every minute to update the time remaining for each job i want to...
3
by: Richard Hollenbeck | last post by:
I have the following query in my form's code: Private Function Get_Data(fieldNum As Integer) Dim strSQL As String Dim db As DAO.Database Dim rs As DAO.Recordset strSQL = "SELECT & "", "" & ...
4
by: Kathy | last post by:
What is the standard technique for handling the fields in the following scenario on a continuous form? Multiple Divisions. Each Division has multiple Buildings. Each Building has a Supervisor. ...
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
2
by: Steve | last post by:
I have a continuous form showing Product Code and Product Name. Product Code is five digits and is sequential. I have a textbox in the form header. What is the code to scroll the continuous form so...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.