How can I make my D-Bus service exit automatically when idle?

27 Views Asked by At

I have a D-Bus service written in Python with GDBus (using bindings provided by PyGObject).

Since it's written in Python and has a few dependencies, that service has a relatively big memory footprint (about 30MiB). It's only needed when the client app is actually being used by the user, so most of the time it's just sitting around idle. It's installed as a D-Bus-activated systemd service, so it's started on-demand.

I'm looking for a way to have it automatically exit if it hasn't been used for some time. I was hoping that systemd could do this for me automatically but it doesn't seem to support that. It does implement this feature for its own services though. I also found this PoC for a race-free exit-on-idle GDBus service but it's written in C.

1

There are 1 best solutions below

3
user2804197 On

I ended up writing my own Python module which implements race-free exit-on-idle, inspired by the systemd code and the PoC by cgwalters.

It's available as a PyPI package, so you can install it via pip:

pip install gdbus-util

It's documented on https://python-gdbus-util.readthedocs.io.

Example usages can be found here.