Using Timer or Scheduler in a Class

  • Thread starter Prof. William Battersea
  • Start date
P

Prof. William Battersea

I'd like a class method to fire every n seconds.

I tried this:

class Timed:
def.__init__(self):
self.t = Timer(3, self.dothing)

def.start(self):
self.t.start()

def.dothing(self):
print "Doing Thing"

s = new Timed()
s.start()

And:

class Scheduled:
def.__init__(self):
self.s = sched.scheduler(time.time, time.sleep)
self.s.enter(3, 1, self.sync, ())

def.start(self):
self.t.start()

def.dothing(self):
print "Syncing"

s = new Scheduled()
s.start()

Both run once and end. I'm obviously missing something here.

Thanks,

Justin
 
S

Steven D'Aprano

Hi Justin,

Does Professor Battersea know you're using his gmail account? *wink*


I'd like a class method to fire every n seconds.

I tried this:

class Timed:
def.__init__(self):
self.t = Timer(3, self.dothing)
def.start(self):
self.t.start()

def.dothing(self):
print "Doing Thing"

s = new Timed()
s.start()

This can't be your actual code, because "s = new Timed()" gives a
SyntaxError. So does "def.start(self)" etc.

Also, what's Timer?

And:

class Scheduled:
def.__init__(self):
self.s = sched.scheduler(time.time, time.sleep)
self.s.enter(3, 1, self.sync, ())

def.start(self):
self.t.start()

def.dothing(self):
print "Syncing"

s = new Scheduled()
s.start()

When I fix the syntax errors and try to run the above code, I get this:

AttributeError: Scheduled instance has no attribute 'sync'

That's only the first of a number of errors. You waste our time when you
post code that doesn't run. Very few people will bother spending the time
and effort to fix your code if you don't respect their time, and those
that do will rub your nose in the fact that you're wasting their time.

Both run once and end. I'm obviously missing something here.

Let's start with some code that actually does run:
.... def __init__(self):
.... self.s = sched.scheduler(time.time, time.sleep)
.... self.s.enter(3, 1, self.dothing, ())
.... def start(self):
.... self.s.run()
.... self.s.enter(3, 1, self.dothing, ())
.... self.start()
.... def dothing(self):
.... print "Syncing"
....Syncing
Syncing
Syncing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in start
File "<stdin>", line 8, in start
File "<stdin>", line 8, in start
File "<stdin>", line 6, in start
File "/usr/lib/python2.5/sched.py", line 108, in run
delayfunc(time - now)
KeyboardInterrupt

This will run until you interrupt it (as I did) or you run out of space
on the stack due to recursion. I imagine this is probably not the best
way to do what you want.

Hint for further explorations: the scheduler keeps a queue of events. If
the queue becomes empty, it stops. You only need to restart the scheduler
with run() if it stopped, otherwise it keeps going.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top