Using Timer or Scheduler in a Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prof. William Battersea

    Using Timer or Scheduler in a Class

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

    I tried this:

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

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

    def.dothing(sel f):
    print "Doing Thing"

    s = new Timed()
    s.start()

    And:

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

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

    def.dothing(sel f):
    print "Syncing"

    s = new Scheduled()
    s.start()

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

    Thanks,

    Justin
  • Steven D'Aprano

    #2
    Re: Using Timer or Scheduler in a Class

    Hi Justin,

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


    On Wed, 13 Aug 2008 23:16:12 -0400, Prof. William Battersea wrote:
    I'd like a class method to fire every n seconds.
    >
    I tried this:
    >
    class Timed:
    def.__init__(se lf):
    self.t = Timer(3, self.dothing)
    def.start(self) :
    self.t.start()
    >
    def.dothing(sel f):
    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__(se lf):
    self.s = sched.scheduler (time.time, time.sleep)
    self.s.enter(3, 1, self.sync, ())
    >
    def.start(self) :
    self.t.start()
    >
    def.dothing(sel f):
    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:
    >>class Scheduled:
    .... 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"
    ....
    >>s = Scheduled()
    >>s.start()
    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)
    KeyboardInterru pt

    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.



    --
    Steven

    Comment

    Working...