473,326 Members | 2,173 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,326 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 2040
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...
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: 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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.