python/callback

Tony Arcieri tony at clickcaster.com
Sat Feb 16 01:19:04 CET 2008


On Fri, Feb 15, 2008 at 5:21 AM, <malek at lcde.net> wrote:

>
> Hi,
>
> I'm implementing a python wrapper for libev, and I'd like people
> interested in having such an extension to help on a small design
> decision.
>
> Watchers need a callback and, as I see it, we could define it in two
> different ways:
>
> 1/ we could require the callback to have the following signature:
> def callback(watcher, revents)
> In this case watcher would be the python watcher object, and revents a
> python int.
> In fact, it would be almost like libev does it.
>
> 2/ or we could go a more "pythonic" way, and only require that the
> callback signature include revents as the first arg:
> def callback(revents, *args, **kwargs)
> args and kwargs could be provided at __init__ or by calling the
> set_callback method.
>

In my Ruby binding, I did something more like the Observer pattern:  Each
Watcher is an object, and receives the events directly as method calls.

Watchers have "callback" methods relating specifically to the event type.
For example, the IO Watcher class has:

on_readable
on_writable

This way the event dispatch is already factored apart for you, and knowing
which watcher fired is easy, that's just self.  Chances are the very first
thing you're going to do with revents is implement the above pattern anyway:
what you do when something is readable is going to be a lot different than
what you do when something is writable.

Now you can just subclass a Watcher, populate your own instance variables,
and define on_readable and on_writable to your liking... and you don't have
to deal with checking any icky revents variable.

-- 
Tony Arcieri
ClickCaster, Inc.
tony at clickcaster.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.schmorp.de/pipermail/libev/attachments/20080215/9e66a518/attachment.htm 


More information about the libev mailing list