Timer

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

    Timer

    Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
    this bit of code" sort of thing?
  • Noen

    #2
    Re: Timer

    Derek Fountain wrote:[color=blue]
    > Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
    > this bit of code" sort of thing?[/color]
    The official home of the Python Programming Language


    but you should perhaps also look into twisteds event-based framework who
    does it even better. www.twistedmatrix.com

    Comment

    • Georgy Pruss

      #3
      Re: Timer


      "Noen" <not.available@ na.no> wrote in message news:zp4nb.2357 7$BD3.4439081@j uliett.dax.net. ..[color=blue]
      > Derek Fountain wrote:[color=green]
      > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
      > > this bit of code" sort of thing?[/color]
      > http://www.python.org/Doc/lib/module-sched.html
      >[/color]

      The above link doesn't work. This does:




      --
      Georgy Pruss
      E^mail: 'ZDAwMTEyMHQwMz MwQGhvdG1haWwuY 29t\n'.decode(' base64')


      Comment

      • Dave Harrison

        #4
        Re: Timer

        > > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run[color=blue][color=green][color=darkred]
        > > > this bit of code" sort of thing?[/color][/color][/color]

        why not just use the time.sleep() function ?

        Comment

        • Alan Kennedy

          #5
          Re: Timer

          [Derek Fountain][color=blue]
          > Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
          > this bit of code" sort of thing?[/color]

          Like this?

          #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
          import threading

          def hello():
          print "hello, world"

          t = threading.Timer (30.0, hello)
          t.start() # after 30 seconds, "hello, world" will be printed
          #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

          Timers start a new thread of execution.

          More info from here



          HTH,

          --
          alan kennedy
          -----------------------------------------------------
          check http headers here: http://xhaus.com/headers
          email alan: http://xhaus.com/mailto/alan

          Comment

          • Derek Fountain

            #6
            Re: Timer

            Dave Harrison wrote:
            [color=blue][color=green][color=darkred]
            >> > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds,
            >> > > run this bit of code" sort of thing?[/color][/color]
            >
            > why not just use the time.sleep() function ?[/color]

            Because that stops the thread. I want things to continue, and then be
            interrupted in order to execute a bit of code. Tcl has the 'after' command:

            after 5000 { set disabled 0 }

            The script carries on, and after 5 seconds whatever is being done gets
            interrupted in order to run a bit of script - in this case set a variable
            turning off a disablement of some sort. This makes it trivial to implement
            "disable this feature for 5 seconds" kinds of logic.

            I can't find anything like that in Python.

            Comment

            • Derek Fountain

              #7
              Re: Timer

              Alan Kennedy wrote:
              [color=blue]
              > [Derek Fountain][color=green]
              >> Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
              >> this bit of code" sort of thing?[/color]
              >
              > Like this?
              > http://www.python.org/doc/current/li...r-objects.html[/color]

              That looks like what I'm after, yes. I haven't looked into threads since
              they've represented trouble in every other language I've used. But I'll
              have a look into their implementation in Python and see if they're any more
              usable here...

              Thanks.

              Comment

              • Anthony Briggs

                #8
                Re: Timer

                At 9:04 PM +0800 27/10/03, Derek Fountain wrote:[color=blue]
                >Dave Harrison wrote:
                >[color=green][color=darkred]
                >>> > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds,
                >>> > > run this bit of code" sort of thing?[/color]
                >>
                >> why not just use the time.sleep() function ?[/color]
                >
                >Because that stops the thread. I want things to continue, and then be
                >interrupted in order to execute a bit of code. Tcl has the 'after' command:
                >
                >after 5000 { set disabled 0 }
                >
                >The script carries on, and after 5 seconds whatever is being done gets
                >interrupted in order to run a bit of script - in this case set a variable
                >turning off a disablement of some sort.[/color]

                Wow, that must make your scripts so much more interesting to debug ;)

                Anthony
                --
                ----------------------------------------------------
                HyPEraCtiVE? HeY, WhO aRE YoU cALliNg HypERaCtIve?!
                aBRiGgS@wEStNeT .cOm.aU
                ----------------------------------------------------

                Comment

                • Bob Gailer

                  #9
                  Re: Timer

                  At 09:21 PM 10/26/2003, Derek Fountain wrote:
                  [color=blue]
                  >Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
                  >this bit of code" sort of thing?[/color]

                  Check out the sched and time modules.

                  Bob Gailer
                  bgailer@alum.rp i.edu
                  303 442 2625


                  ---
                  Outgoing mail is certified Virus Free.
                  Checked by AVG anti-virus system (http://www.grisoft.com).
                  Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003

                  Comment

                  • Roy Smith

                    #10
                    Re: Timer

                    In article <3f9d1775$0$235 84$5a62ac22@fre enews.iinet.net .au>,
                    Derek Fountain <nomail@hursley .ibm.com> wrote:[color=blue]
                    >Dave Harrison wrote:
                    >[color=green][color=darkred]
                    >>> > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds,
                    >>> > > run this bit of code" sort of thing?[/color]
                    >>
                    >> why not just use the time.sleep() function ?[/color]
                    >
                    >Because that stops the thread.[/color]

                    Check out the threading module. In particular, the threading.Timer class.

                    Comment

                    • Cameron Laird

                      #11
                      Concurrency models (was: Timer)

                      In article <mailman.132.10 67262731.702.py thon-list@python.org >,
                      Anthony Briggs <abriggs@westne t.com.au> wrote:

                      Comment

                      • Alan Kennedy

                        #12
                        Re: Concurrency models (was: Timer)

                        [Cameron Laird][color=blue]
                        > This thread is really about asynchronous or concurrent
                        > processing,[/color]

                        I really don't have time to post to the level of detail I would like,
                        but I had to put forward a couple of thoughts.
                        [color=blue]
                        > I claim, and I further claim we really don't
                        > have *any* satisfying model for coding those.[/color]

                        I think you might get some disagreement from the Twisted, Medusa and
                        ZServer people. And possibly see a slanging match erupt on whose
                        design is best....
                        [color=blue]
                        > Threading
                        > is a quagmire (everyone agree?),[/color]

                        Definitely. Sometimes.

                        I think threads are really a useful abstraction for programmers who
                        are learning to deal with developing servers for the first time. And
                        threads fulfill their function very well, to a point.

                        But they have fundamental scalability problems: The C10K problem
                        illustrates this well: Surely it should be possible for a modern
                        computer, with giga-everything (!), to serve 10,000 clients
                        simultaneously?



                        To go beyond the performance limits imposed by the resource-hogging
                        threads (OMG, > 1MB of core on some platforms!), it's necessary to
                        move to a different concurrency mechanism, such as coroutines, which
                        represents "tasklets" much more efficiently.

                        But event-based concurrency models suffer from a fundamental
                        limitation: it is more difficult to spread work across multiple
                        processors. CPython, to some degree, lets event-based developers off
                        the hook, because the presence of the GIL allows them to not address
                        the problem, because it wouldn't achieve anything anyway.....

                        And the patchy support for SSL in asynch frameworks is another
                        fundamental problem.

                        Another point in favour of threads: On a multi-processor box, the
                        thread abstraction generally "automagica lly" gives you free migration
                        to other processors. You (generally) don't have to change a line of
                        your code: the underlying [OS|VM] takes care of it.
                        [color=blue]
                        > and our guild has demonstrated only marginally more
                        > reliability in working with,
                        > say, co-routines, chords, continuations, and so on. For
                        > my money, the event-oriented model behind the [after]
                        > above is at least as robust as any other.[/color]

                        FWIW, I think that Java has a very nice model for asynchronous IO,
                        with all "selectors" being thread-safe, so that "selectable s" that are
                        ready for IO can be processed in another thread, i.e. potentially on
                        another processor, thus giving the best of both worlds.


                        [color=blue]
                        > Among other frailties, I hold a grudge against the aca-
                        > demic world that it hasn't shown more leadership in this
                        > matter.[/color]

                        Googling for "'high performance' asynchronous server" shows that both
                        Academia and Industry are certainly not short of powerpoint-ware on
                        the subject......
                        [color=blue]
                        > I'll summarize: validation of event-oriented designs
                        > implemented with [after] and similar mechanisms, is at
                        > worst no more difficult than validation of the corre-
                        > sponding thread-based designs. Concurrency is hard to
                        > get right, at least with our current understanding.[/color]

                        My €0,02: Generators (and, by extension, coroutines) will be python's
                        greatest advance in simplifying writing event-based server
                        architectures: resumable functions are a honking great idea.

                        regards,

                        --
                        alan kennedy
                        -----------------------------------------------------
                        check http headers here: http://xhaus.com/headers
                        email alan: http://xhaus.com/mailto/alan

                        Comment

                        • Alan Kennedy

                          #13
                          Re: Concurrency models (was: Timer)

                          [Cameron Laird][color=blue]
                          > This thread is really about asynchronous or concurrent
                          > processing,[/color]

                          I really don't have time to post to the level of detail I would like,
                          but I had to put forward a couple of thoughts.
                          [color=blue]
                          > I claim, and I further claim we really don't
                          > have *any* satisfying model for coding those.[/color]

                          I think you might get some disagreement from the Twisted, Medusa and
                          ZServer people. And possibly see a slanging match erupt on whose
                          design is best....
                          [color=blue]
                          > Threading
                          > is a quagmire (everyone agree?),[/color]

                          Definitely. Sometimes.

                          I think threads are really a useful abstraction for programmers who
                          are learning to deal with developing servers for the first time. And
                          threads fulfill their function very well, to a point.

                          But they have fundamental scalability problems: The C10K problem
                          illustrates this well: Surely it should be possible for a modern
                          computer, with giga-everything (!), to serve 10,000 clients
                          simultaneously?



                          To go beyond the performance limits imposed by the resource-hogging
                          threads (OMG, > 1MB of core on some platforms!), it's necessary to
                          move to a different concurrency mechanism, such as coroutines, which
                          represents "tasklets" much more efficiently.

                          But event-based concurrency models suffer from a fundamental
                          limitation: it is more difficult to spread work across multiple
                          processors. CPython, to some degree, lets event-based developers off
                          the hook, because the presence of the GIL allows them to not address
                          the problem, because it wouldn't achieve anything anyway.....

                          And the patchy support for SSL in asynch frameworks is another
                          fundamental problem.

                          Another point in favour of threads: On a multi-processor box, the
                          thread abstraction generally "automagica lly" gives you free migration
                          to other processors. You (generally) don't have to change a line of
                          your code: the underlying [OS|VM] takes care of it.
                          [color=blue]
                          > and our guild has demonstrated only marginally more
                          > reliability in working with,
                          > say, co-routines, chords, continuations, and so on. For
                          > my money, the event-oriented model behind the [after]
                          > above is at least as robust as any other.[/color]

                          FWIW, I think that Java has a very nice model for asynchronous IO,
                          with all "selectors" being thread-safe, so that "selectable s" that are
                          ready for IO can be processed in another thread, i.e. potentially on
                          another processor, thus giving the best of both worlds.


                          [color=blue]
                          > Among other frailties, I hold a grudge against the aca-
                          > demic world that it hasn't shown more leadership in this
                          > matter.[/color]

                          Googling for "'high performance' asynchronous server" shows that both
                          Academia and Industry are certainly not short of powerpoint-ware on
                          the subject......
                          [color=blue]
                          > I'll summarize: validation of event-oriented designs
                          > implemented with [after] and similar mechanisms, is at
                          > worst no more difficult than validation of the corre-
                          > sponding thread-based designs. Concurrency is hard to
                          > get right, at least with our current understanding.[/color]

                          My €0,02: Generators (and, by extension, coroutines) will be python's
                          greatest advance in simplifying writing event-based server
                          architectures: resumable functions are a honking great idea.

                          regards,

                          --
                          alan kennedy
                          -----------------------------------------------------
                          check http headers here: http://xhaus.com/headers
                          email alan: http://xhaus.com/mailto/alan

                          Comment

                          • Anand Pillai

                            #14
                            Re: Timer

                            Python standard library offers a 'Timer' class as part
                            of its threading library.

                            The documentation should be reachable at


                            It is easy to create your own timer objects by using
                            python threads.

                            Here is a sample code for the same. It is a working
                            code, and creates a custom timer class with a maximum
                            timeout, which calls the function again and again until
                            it times out. It is different from the standard timer
                            which calls its target function just once.

                            ----------------<snip>-----<snip>--------------------
                            from threading import Thread
                            import time

                            class ModifiedTimer(T hread):

                            def __init__(self, interval, maxtime, target):
                            self.__interval = interval
                            self.__maxtime = maxtime
                            self.__tottime = 0.0
                            self.__target = target
                            self.__flag = 0
                            Thread.__init__ (self, None, 'mytimer', None)

                            def run(self):

                            while self.__flag==0:
                            time.sleep(self .__interval)
                            self.__target()
                            self.__tottime += self.__interval

                            if self.__tottime >= self.__maxtime:
                            print 'Timing out...'
                            self.end()

                            def end(self):
                            self.__flag=1

                            class MyTimer:

                            def __init__(self):
                            self.__interval = 5.0

                            def __timerfunc(sel f):
                            print 'Hello, this is the timer function!'

                            def make_timer(self , interval):
                            self.__interval = interval
                            self.__timer = ModifiedTimer(s elf.__interval, 60.0, self.__timerfun c)
                            self.__timer.st art()

                            if __name__=="__ma in__":
                            t=MyTimer()
                            t.make_timer(10 .0)

                            ------------------<snip>-----------<snip>---------------


                            HTH

                            -Anand Pillai


                            Bob Gailer <bgailer@alum.r pi.edu> wrote in message news:<mailman.1 36.1067267376.7 02.python-list@python.org >...[color=blue]
                            > At 09:21 PM 10/26/2003, Derek Fountain wrote:
                            >[color=green]
                            > >Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
                            > >this bit of code" sort of thing?[/color]
                            >
                            > Check out the sched and time modules.
                            >
                            > Bob Gailer
                            > bgailer@alum.rp i.edu
                            > 303 442 2625
                            >
                            > --[/color]

                            Comment

                            • Cameron Laird

                              #15
                              Re: Concurrency models (was: Timer)

                              In article <3F9D77BE.B66D1 F08@hotmail.com >,
                              Alan Kennedy <alanmk@hotmail .com> wrote:[color=blue]
                              >[Cameron Laird][color=green]
                              >> This thread is really about asynchronous or concurrent
                              >> processing,[/color]
                              >
                              >I really don't have time to post to the level of detail I would like,
                              >but I had to put forward a couple of thoughts.[/color]
                              This is mostly a "me, too" follow-up. I decided
                              to post it, though, if only to ensure[color=blue]
                              >[color=green]
                              >> I claim, and I further claim we really don't
                              >> have *any* satisfying model for coding those.[/color]
                              >
                              >I think you might get some disagreement from the Twisted, Medusa and
                              >ZServer people. And possibly see a slanging match erupt on whose
                              >design is best....[/color]
                              that Twisted et al. don't feel abused. I am VERY
                              fond of Twisted, Medusa, and Zope, and have even
                              written an (unpublished, still) article on their
                              concurrency management. I, also, won't go into
                              detail now; I just want to make clear that I think
                              they're great, and was excluding them only for
                              categorical reasons.

                              Comment

                              Working...