[Python-Dev] Towards native fileevents in Python (Was Re: Python multiplexing is too hard)

Alexandre Ferrieux alexandre.ferrieux@cnet.francetelecom.fr
Tue, 23 May 2000 09:36:35 +0200

Ka-Ping Yee wrote:
> > Should Python have an event model?  I'm not con-
> > vinced.
> Indeed.  This would be a huge core change, way too
> large to be feasible. 

Warning here. What would indeed need a huge core change,
is a pervasive use of events like in E. 'Having an event model'
is often interpreted in a less extreme way, simply meaning
'having the proper set of primitives at hand'.
Our discussion (and your comments below too, agreed !) was focussed
on the latter, so we're only talking about a pure library issue.
Asking any change in the Python lang itself for such a peripheral need
never even remotely crossed my mind !

> But i do think it would be
> excellent to simply provide more facilities for
> helping people use whatever model they want, and
> given the toolkit we let people build great things.


> What you described sounded like it could be implemented
> fairly easily with some functions like
>     register(handle, mode, callback)
>         or file.register(mode, callback)
>         Put 'callback' in a dictionary of files
>         to be watched for mode 'mode'.
>     mainloop(timeout)
>         Repeat (forever or until 'timeout') a
>         'select' on all the files that have been
>         registered, and do calls to the callbacks
>         that have been registered.
> Presumably there would be some exception that a
> callback could raise to quietly exit the 'select'
> loop.

Great !!! That's exactly the kind of Pythonic translation I was
expecting. Thanks !

>     1. How does Tcl handle exiting the loop?
>        Is there a way for a callback to break
>        out of the vwait?

Yes, as explained before, in Tcl the loop-breaker is a write sentinel
on a variable. When a callback wants to break out, it simply sets the
var. But as also mentioned before, I'd prefer an exception-based
mechanism as you summarized.

>     2. How do you unregister these callbacks in Tcl?

We just register an empty string as the callback name (script).
But this is just a random API choice. Anything more Pythonic is welcome
(an explicit unregister function is okay for me).