equivalent to Tcl 'after' command?

M

Mark Harrison

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
 
J

Jeff Epler

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
 
D

Diez B. Roggisch

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

Peter Hansen

Jeff said:
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.
 
A

Alan Gauld

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
 
J

Jeff Epler

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?
[/QUOTE]
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
 
G

Giles Brown

Mark Harrison said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top