[mod_python] mod_python + dbus ?

Graham Dumpleton grahamd at dscpl.com.au
Mon Jul 3 18:33:26 EDT 2006

Panard wrote ..
> Hi,
> I'm tring to use a running dbus service with mod_python. The service runs
> fine, a python cgi script uses it fine, but a mod_python script doesn't
> respond...
> Here are the scripts used :
> The service itself :
> ======== dbus_service.py ========
> import dbus
> import dbus.service
> import gobject
> iface = 'org.inzenet.test_mp.IFace'
> class test( dbus.service.Object ) :
>     def __init__( self, bus_name ) :
>         dbus.service.Object.__init__( self, bus_name, '/org/inzenet/test_mp'
> )
>     @dbus.service.method(iface)
>     def test( self ) :
>         return "OK!"
> bus = dbus.service.BusName('org.inzenet.test_mp', bus=dbus.SystemBus())
> test( bus )
> mainloop = gobject.MainLoop()
> mainloop.run()
> ===========
> DBus perms :
> ======
> <busconfig>
>         <policy user="panard">
>                 <allow own="org.inzenet.test_mp"/>
>         </policy>
>         <policy context="default">
>                 <allow send_destination="org.inzenet.test_mp"/>
>                 <allow receive_sender="org.inzenet.test_mp"/>
>         </policy>
> </busconfig>
> ========
> Launch the service with
> $ python dbus_service.py
> The mod_python script :
> ===== test_dbus.pu =====
> def test( req ) :
>     import dbus
>     bus = dbus.SystemBus()
>     service = bus.get_object('org.inzenet.test_mp','/org/inzenet/test_mp')
>     return service.test( dbus_interface = 'org.inzenet.test_mp.IFace' )
> Calling http://localhost/~panard/mp/test_dbus.py/test doesn't respond...
> Any clues?

What version of Apache are you using and what MPM is compiled into it,
ie., prefork or worker? What version of mod_python are you using?

In general, embedding a messaging type framework where persistent socket
connections are maintained between processes where the Python module
is simply a wrapper around a C library, into Apache/mod_python is
not simple and if the dbus Python wrappers have not been designed and
implemented specifically to cope with the way that Python is used by
mod_python, it is probably unlikely to work. The two main issues which
make it a complicated business are:

1. If "worker" MPM is compiled into Apache, or if on Win32 platform,
then concurrent requests can be handled in distinct threads. If dbus
Python wrappers are not properly thread aware it will probably not work.

2. Multiple Python interpreters may be created by mod_python for
different parts of the URL namespace. If the dbus Python wrappers have
only been implemented to use the simple GIL threading model it will
probably not work. There would be a small chance that it will work if
all requests are forced to run in the "main_interpreter" and mod_python
3.2.9 (released real soon now) is used.

There is a lot more to it than just this though and it is a really hard
thing to explain so you would understand it unless you are quite
intimate with use of multiple Python interpreters and Python thread
programming at the C API level.

That said, are you wanting to use dbus because you specifically want to
talk to an existing application which is already implemented with support
for dbus, or were you after a general messaging framework that could
be used from Apache/mod_python and you were going to be implementing
everything from scratch?

If you are starting from scratch, I can perhaps suggest another system
which is known to work from Apache/mod_python and does cope with
all the problems of multiple interpreters and multithreading that it
presents and which makes things complicated.


More information about the Mod_python mailing list