By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

equivalent to Tcl 'after' command?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
> 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
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
In article <hu*******************@newssvr25.news.prodigy.com> ,
Mark Harrison <mh@pixar.com> 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/li...r-objects.html
Jul 18 '05 #4

P: n/a
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.
Jul 18 '05 #5

P: n/a
On Thu, 22 Apr 2004 17:42:37 GMT, Mark Harrison <mh@pixar.com>
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
Jul 18 '05 #6

P: n/a
> On Thu, 22 Apr 2004 17:42:37 GMT, Mark Harrison <mh@pixar.com>
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

Jul 18 '05 #7

P: n/a
Mark Harrison <mh@pixar.com> wrote in message news:<hu*******************@newssvr25.news.prodigy .com>...
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
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.