Timer interrupt of execution?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    Timer interrupt of execution?

    Hello all,

    I know why the following doesn't work ... I can't figure out how to make it
    work like I want it to. Basically, I only want the loop computing
    fibonacci numbers to run for approx 5 seconds. The timer fires, the
    exception is raised, but it is not caught ... because the execution stack
    for the function that raises isn't in the same thread as the try: except:.

    How do you fix this? Is it better to do this without exceptions as the
    control construct?

    Regards,
    Mark

    PS I tried two positions of starting the thread ... unfortunately, lexical
    scope doesn't affect the execution stack ... it's still in its own space, I
    guess.

    #!/usr/bin/env python

    import threading


    class TimeException(E xception):
    def __init__(self, i):
    self.msg = "Out of time"

    def raiser():
    print "raising"
    raise TimeException
    print "raised"

    def fib(x):
    if x == 0 or x == 1:
    return 1
    else:
    return fib(x-1) + fib(x-2)

    #position 1
    #t = threading.Timer (5.0, raiser)
    #t.start()

    try:
    # position 2
    t = threading.Timer (5.0, raiser)
    t.start()
    for i in range(1,1000000 0):
    t = fib(i)
    print i, ": ", t
    except TimeException:
    print "thread done"
    except:
    print "other exception"

  • Ulrich Petri

    #2
    Re: Timer interrupt of execution?

    "Mark" <Hobbes2176@yah oo.com> schrieb im Newsbeitrag
    news:Bvwnb.1337 7$%e3.11891@nwr ddc03.gnilink.n et...[color=blue]
    > Hello all,
    >
    > I know why the following doesn't work ... I can't figure out how to make[/color]
    it[color=blue]
    > work like I want it to. Basically, I only want the loop computing
    > fibonacci numbers to run for approx 5 seconds. The timer fires, the
    > exception is raised, but it is not caught ... because the execution stack
    > for the function that raises isn't in the same thread as the try: except:.
    >
    > How do you fix this? Is it better to do this without exceptions as the
    > control construct?
    >
    > Regards,
    > Mark
    >
    > PS I tried two positions of starting the thread ... unfortunately, lexical
    > scope doesn't affect the execution stack ... it's still in its own space,[/color]
    I[color=blue]
    > guess.
    >[/color]

    Hi Mark,

    are you deliberately trying to break a butterfly on the wheel?

    try this:

    ------ cut here --------
    #!/usr/local/bin/python

    import time

    def fib(x):
    if x == 0 or x == 1:
    return 1
    else:
    return fib(x-1) + fib(x-2)

    i=1
    t=time.time()
    while(time.time ()-t<5):
    print "%d: %d" % (i, fib(i))
    i += 1

    -------- cut ---------

    HTH

    Ciao Ulrich


    Comment

    • Dennis Lee Bieber

      #3
      Re: Timer interrupt of execution?

      Mark fed this fish to the penguins on Tuesday 28 October 2003 08:08 am:

      [color=blue]
      >
      > How do you fix this? Is it better to do this without exceptions as
      > the control construct?
      >[/color]
      Assuming you need a general case applicable to anything with a "work
      task" that needs to return some sort of result after some specific time
      period...

      I'd drop the exception, and use a Queue. The timer expiration should
      write the termination code to the queue, and the worker task should be
      coded such that it regularly checks the queue (non-blocking) for data.

      import threading
      import Queue

      termQ = Queue.Queue(0)

      def terminator():
      global termQ
      termQ.put("Time 's Up, You're Dead")

      def fib(x):
      if x < 0: return None
      if x == 0 or x == 1:
      return 1
      else:
      return fib(x-1) + fib(x-2)

      termTimer = threading.Timer (5.0, terminator)
      termTimer.start ()
      for i in xrange(1,100000 00):
      try:
      msg = termQ.get_nowai t()
      print msg
      break
      except:
      print i, ": ", fib(i)


      --[color=blue]
      > =============== =============== =============== =============== == <
      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
      > wulfraed@dm.net | Bestiaria Support Staff <
      > =============== =============== =============== =============== == <
      > Bestiaria Home Page: http://www.beastie.dm.net/ <
      > Home Page: http://www.dm.net/~wulfraed/ <[/color]

      Comment

      Working...