equivalent to Tcl 'after' command?

Discussion in 'Python' started by Mark Harrison, Apr 22, 2004.

  1. 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
    Mark Harrison, Apr 22, 2004
    #1
    1. Advertising

  2. Mark Harrison

    Jeff Epler Guest

    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
    Jeff Epler, Apr 22, 2004
    #2
    1. Advertising

  3. > 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?


    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
    Diez B. Roggisch, Apr 22, 2004
    #3
  4. Mark Harrison

    Roy Smith Guest

    In article <huThc.54272$>,
    Mark Harrison <> wrote:

    > 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"}


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

    http://www.python.org/doc/current/lib/timer-objects.html
    Roy Smith, Apr 22, 2004
    #4
  5. Mark Harrison

    Peter Hansen Guest

    Jeff Epler wrote:

    > 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.


    It does, using reactor callLater(), but I'd go for the sched approach
    if it's just a one-off.
    Peter Hansen, Apr 22, 2004
    #5
  6. Mark Harrison

    Alan Gauld Guest

    On Thu, 22 Apr 2004 17:42:37 GMT, Mark Harrison <>
    wrote:
    > 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?


    I suspect its to start a thread that in turn starts with
    a sleep command. Not very pretty but approximately the same.

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


    My book(paperversion 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
    http://www.freenetpages.co.uk/hp/alan.gauld
    Alan Gauld, Apr 22, 2004
    #6
  7. Mark Harrison

    Jeff Epler Guest

    > On Thu, 22 Apr 2004 17:42:37 GMT, Mark Harrison <>
    > wrote:
    > > 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?

    >

    On Thu, Apr 22, 2004 at 11:52:57PM +0100, Alan Gauld wrote:
    > I suspect its to start a thread that in turn starts with
    > a sleep command. Not very pretty but approximately the same.


    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
    Jeff Epler, Apr 23, 2004
    #7
  8. Mark Harrison

    Giles Brown Guest

    Mark Harrison <> wrote in message news:<huThc.54272$>...
    > 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?


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

    Giles
    Giles Brown, Apr 23, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinay Gupta
    Replies:
    2
    Views:
    1,096
    Michael Schlenker
    Jul 27, 2004
  2. Peter Gordon
    Replies:
    2
    Views:
    317
    Peter Hansen
    Nov 14, 2004
  3. gamename

    Equivalent of TCL's "subst" ?

    gamename, Nov 13, 2007, in forum: Python
    Replies:
    4
    Views:
    592
    gamename
    Nov 17, 2007
  4. Replies:
    3
    Views:
    492
    alex23
    Mar 19, 2009
  5. Mumia W.

    Inline::Tcl vs. Inline::Tcl

    Mumia W., Aug 23, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    127
    Mumia W.
    Aug 23, 2006
Loading...

Share This Page