How to create a timer/scheduler in Python?

J

John Dann

I need what I'd call (in .Net) a timer, ie I need to run a function eg
every 2 seconds - it doesn't need to be millisec accurate but it would
be nice if it wasn't eg every 4 seconds or something.

Rather surprisingly, Core Python (Chun) doesn't seem to index 'timer'
or 'scheduler', which leaves me wondering whether this is an aspect of
Python that isn't perhaps widely used?

Looking around on the net I can see references to a thread timer, but
I'm not really looking to start any new threads (I just want part of
the GUI to update every 2 secs) and don't want to get into that sort
of complication while still just learning Python.

Is there really no simple timer/scheduler function available in
Python?
 
U

Uwe Schmitt

I need what I'd call (in .Net) a timer, ie I need to run a function eg
every 2 seconds - it doesn't need to be millisec accurate but it would
be nice if it wasn't eg every 4 seconds or something.

Rather surprisingly, Core Python (Chun) doesn't seem to index 'timer'
or 'scheduler', which leaves me wondering whether this is an aspect of
Python that isn't perhaps widely used?

Looking around on the net I can see references to a thread timer, but
I'm not really looking to start any new threads (I just want part of
the GUI to update every 2 secs) and don't want to get into that sort
of complication while still just learning Python.

Is there really no simple timer/scheduler function available in
Python?

The usual way is to use threads. Depending on you GUI lib there may
exist
replacements for threads, eg wxPython. As most (all ?) GUIs have some
event handling
mechanisms they should be able to schedule tasks and process them in
the
background.

By the way: Using threads in Python is quite simple, even if you are
a novice.

Greetings, Uwe
 
M

Marc 'BlackJack' Rintsch

Looking around on the net I can see references to a thread timer, but
I'm not really looking to start any new threads (I just want part of
the GUI to update every 2 secs) and don't want to get into that sort
of complication while still just learning Python.

Look into the GUI toolkit because that's typically something solved with
functions from the specific toolkit.
Is there really no simple timer/scheduler function available in
Python?

You can do that quite easily with threads but almost all GUI toolkits
don't like it if you access the GUI from multiple threads.

Ciao,
Marc 'BlackJack' Rintsch
 
D

Daniel Fetchinson

I need what I'd call (in .Net) a timer, ie I need to run a function eg
every 2 seconds - it doesn't need to be millisec accurate but it would
be nice if it wasn't eg every 4 seconds or something.

Rather surprisingly, Core Python (Chun) doesn't seem to index 'timer'
or 'scheduler', which leaves me wondering whether this is an aspect of
Python that isn't perhaps widely used?

Looking around on the net I can see references to a thread timer, but
I'm not really looking to start any new threads (I just want part of
the GUI to update every 2 secs) and don't want to get into that sort
of complication while still just learning Python.

Is there really no simple timer/scheduler function available in
Python?


You might want to look at scheduler.py from turbogears which is
exactly the tool you describe and luckily is 99.99% independent of
turbogears. I'd think you need to modify 1-2 lines:

http://svn.turbogears.org/tags/1.0.4.4/turbogears/scheduler.py

Cheers,
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top