Python COM for Linux?

Warren Postma embed at NOSPAM.geocities.com
Wed Jun 28 09:49:49 EDT 2000


> That sounds interesting. Could you expand on it a little more? Is the
> publish/subscribe done in the python module or in the C extension? Or
> both?

It's all in the C extension.  I didn't need to wrap it in a python module
(at least not yet), because it was a fairly quick hack designed for my
specific embedded application.

The basic idea is that you have a dictionary (created and maintained in the
C extension), and the methods are:

events.dict()       - returns a list of event names, and the parameters they
take, and the documentation strings for each event
ie:
    {  'event1':  ( '(iis)', 'event1(Foo,Bar,Baz) - something called event1
happened and it has data Foo,Bar,Baz' ),
        'event2':  ( '(O)', 'event1(FooObj) - something called event1
happened and it has data some arbitrary python object FooObj' ),
    }
events.subscribe( 'eventname', myself.callme ) - pass it any callable object
(an instance method for example)
events.unsubscribe( 'eventname', myself.callme ) - undo the subscription

On the C side of it, there is a publish API, which looks like this:

publish( char *name, char *argFormat /*ie: "(iis)", "(O)", etc */, char
*docstring);
unpublish( char *name);
event( char *name, ... ); // var args, like printf, format specified in
publish,


You register and unregister interest from whatever python modules you add
on. In my case, I start whatever python objects are needed by modifying
site.py to have it start up threads, etcetera.  In this case, only C
functions generate and publish and only Python subscribes, but if I was
trying to be more generic, I would certainly implement that either one could
publish or subscribe.

The other trick I have implemented in C is that I generate a thread in C,
and an associated thread state for Python, and an anonymous pipe which I use
as a  processing queue.   The events are handled in that thread context,
sequentially, but asynchronously to whatever thread registered the event
callback.   All events are handled by calling them from this queue.  If you
are interested more, email me and I can send you more code-intensive
samples.

Warren





More information about the Python-list mailing list