equivalent to Tcl 'after' command?

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

    equivalent to Tcl 'after' command?

    I'm writing some event-driven programs, and I would like
    to do the equivalent of the Tcl 'after' command, e.g.:

    after 1000 {puts "one second has elapsed"}

    1. What's the most canonical way of doing this?

    2. What's the best reference that talks about non-gui event loop
    programming in Python?

    Many TIA!
    Mark

    --
    Mark Harrison
    Pixar Animation Studios
  • Diez B. Roggisch

    #2
    Re: equivalent to Tcl 'after' command?

    > after 1000 {puts "one second has elapsed"}[color=blue]
    >
    > 1. What's the most canonical way of doing this?
    >
    > 2. What's the best reference that talks about non-gui event loop
    > programming in Python?[/color]

    Has been a while since I used tcl/tk, so I'm a bit rusty here. But AFAIK
    that after stuff was needed when the tk event loop took over control. Sooo
    - _if_ you use a toolkit, it most probably features such a facility.

    In python, such stuff is usually accomplished using threads - and since a
    recent version, there is the module sched. Which internally uses threads. I
    personally ripped the webware taskkit for recurrent tasks.
    --
    Regards,

    Diez B. Roggisch

    Comment

    • Jeff Epler

      #3
      Re: equivalent to Tcl 'after' command?

      Python doesn't define any event loop of its own. asyncore has one, but
      I don't think it has the concept of scheduling events at a future time,
      but only of reacting to the readability / writability of sockets.

      I'm sure that more advanced systems, like Twisted, can do the kind of
      thing you're asking for.

      Jeff

      Comment

      • Roy Smith

        #4
        Re: equivalent to Tcl 'after' command?

        In article <huThc.54272$9V 6.15414@newssvr 25.news.prodigy .com>,
        Mark Harrison <mh@pixar.com > wrote:
        [color=blue]
        > I'm writing some event-driven programs, and I would like
        > to do the equivalent of the Tcl 'after' command, e.g.:
        >
        > after 1000 {puts "one second has elapsed"}[/color]

        You want to took at the Timer objects that are part of the threading
        module


        Comment

        • Peter Hansen

          #5
          Re: equivalent to Tcl 'after' command?

          Jeff Epler wrote:
          [color=blue]
          > Python doesn't define any event loop of its own. asyncore has one, but
          > I don't think it has the concept of scheduling events at a future time,
          > but only of reacting to the readability / writability of sockets.
          >
          > I'm sure that more advanced systems, like Twisted, can do the kind of
          > thing you're asking for.[/color]

          It does, using reactor callLater(), but I'd go for the sched approach
          if it's just a one-off.

          Comment

          • Alan Gauld

            #6
            Re: equivalent to Tcl 'after' command?

            On Thu, 22 Apr 2004 17:42:37 GMT, Mark Harrison <mh@pixar.com >
            wrote:[color=blue]
            > I'm writing some event-driven programs, and I would like
            > to do the equivalent of the Tcl 'after' command, e.g.:
            >
            > after 1000 {puts "one second has elapsed"}
            >
            > 1. What's the most canonical way of doing this?[/color]

            I suspect its to start a thread that in turn starts with
            a sleep command. Not very pretty but approximately the same.
            [color=blue]
            > 2. What's the best reference that talks about non-gui event loop
            > programming in Python?[/color]

            My book(paperversi on only discusses them briefly but its hardly a
            rference, more an intro to the convcept for beginners...

            Its not quite the same as a pure event loop environment but you
            could check out the cmd module for a text based command loop/menu
            system. If you haven't already....

            HTH,

            Alan G.
            Author of the Learn to Program website

            Comment

            • Jeff Epler

              #7
              Re: equivalent to Tcl 'after' command?

              > On Thu, 22 Apr 2004 17:42:37 GMT, Mark Harrison <mh@pixar.com >[color=blue]
              > wrote:[color=green]
              > > I'm writing some event-driven programs, and I would like
              > > to do the equivalent of the Tcl 'after' command, e.g.:
              > >
              > > after 1000 {puts "one second has elapsed"}
              > >
              > > 1. What's the most canonical way of doing this?[/color]
              >[/color]
              On Thu, Apr 22, 2004 at 11:52:57PM +0100, Alan Gauld wrote:[color=blue]
              > I suspect its to start a thread that in turn starts with
              > a sleep command. Not very pretty but approximately the same.[/color]

              But "doing something in about X ms in a thread" and "doing something in
              about X ms from the event loop" are different: the latter basically
              frees you from worrying about locking, race conditions, and the rest,
              because you know it can't run concurrently with arbitrary other code.

              Jeff

              Comment

              • Giles Brown

                #8
                Re: equivalent to Tcl 'after' command?

                Mark Harrison <mh@pixar.com > wrote in message news:<huThc.542 72$9V6.15414@ne wssvr25.news.pr odigy.com>...[color=blue]
                > I'm writing some event-driven programs, and I would like
                > to do the equivalent of the Tcl 'after' command, e.g.:
                >
                > after 1000 {puts "one second has elapsed"}
                >
                > 1. What's the most canonical way of doing this?[/color]

                Not exactly canonical (I certainly haven't used it more than once if that), but
                there is the standard library "sched" module.

                Giles

                Comment

                Working...