I'm coming from Tcl-world ...

Oren Tirosh oren-py-l at hishome.net
Sat Aug 3 17:57:01 CEST 2002

On Sat, Aug 03, 2002 at 02:57:22PM +0100, Robin Becker wrote:
> In message <20020803133549.GA66035 at hishome.net>, Oren Tirosh <oren-py-
> l at hishome.net> writes
> >On Sat, Aug 03, 2002 at 11:46:28AM +0100, Robin Becker wrote:
> >> I've always thought it was a pity that Tcl's event driven file i/o
> >> didn't catch on elsewhere. Python seems locked into an older world in
> >> its low level i/o mechanisms although at higher levels it's very clean.
> >> Polling feels inefficient, but I guess is very robust.
> >
> >Have you tried asyncore?
> >
> >       Oren
> >
> yes, it's quite a mess working out what's going on.

Yes, you're right. The asyncore documentation could be better. Actually it's 
very simple to use:

Write an object with the following methods:

   writable() - return true if you wish to get write events
   readable() - return true if you wish to get read events

   handle_error() - called on any errors
   handle_write_event() - called on write event
   handle_read_event() - called on read event 
       also called on EOF, in that case reading will return ''

set socket_map[fd] to point to this object (multiple objects, more likely)
call asyncore.loop(optional_timeout)
The loop will return either on timeout or when one of the handlers
raises the asyncore.ExitNow exception.

That's all.

Everything else is just predefined objects you can inherit that help you 
handle the little details.  The asyncore.dispatcher handles setting up the 
socket, listening for incoming connections, etc. The dispatcher_with_send 
has a buffered send function, file_dispatcher handles pipes and other 
non-socket fds, the asynchat module helps you implement two-way stateful 

If you have any ideas how to make it easier to use I'd love to hear them,
whether they're tcl-inspired or not...


More information about the Python-list mailing list