pythongtkpynotify

Callbacks and gtk main loop


I'm trying to make a simple Unix desktop application which uses the pynotify notification system to show users some alerts and allow them to launch relevant applications from a button on placed on the alert.

Here is the relevant simplified code :

import subprocess, pynotify, gobject, gtk

class Notifier():
    def __init__(self):
        pynotify.init('Notifications')
        n = pynotify.Notification("Some stuff")
        n.add_action("action", "Action", self.action_callback)
        n.show()
        gtk.main()

    def action_callback(self, n, action):
        subprocess.Popen(['ls', '/'])

if __name__ == '__main__':
    Notifier()

This works fine (it shows a notification popup with an "Action" button which triggers a ls / when activated) until I actually try to put the notification part into a loop (I need to regularly poll a server to get notifications to then show).

I've tried this :

import subprocess, pynotify, gobject, gtk

class Notifier():
    def __init__(self):
        pynotify.init('Notifications')
        gobject.timeout_add(0, self.main)
        gtk.main()

    def action_callback(self, n, action):
        subprocess.Popen(['ls', '/'])

    def main(self):
        n = pynotify.Notification("Some stuff")
        n.add_action("action", "Action", self.action_callback)
        n.show()
        gobject.timeout_add(10000, self.main)

if __name__ == '__main__':
    Notifier()

but for some reason the "action_callback" function is not called anymore when clicking on the "Action" button.

It seems this is an issue with the way I use the Gtk main loop. Doing something like this makes the function to actually be triggered :

import subprocess, pynotify, gobject, gtk

class Notifier():
    def __init__(self):
        pynotify.init('Notifications')
        self.main()

    def action_callback(self, n, action):
        subprocess.Popen(['ls', '/'])

    def main(self):
        n = pynotify.Notification("Some stuff")
        n.add_action("action", "Action", self.action_callback)
        n.show()
        gobject.timeout_add(10000, self.main)
        gtk.main()

if __name__ == '__main__':
    Notifier()

but of course this is no proper solution and I quickly get a "maximum recursion depth exceeded" Python RuntimeError. However it shows that changing the place of the gtk.main() call has an incidence.

I've tried to look at Gtk and Pygtk documentation about the main loop but ultimatelly didn't find a solution.

So my question is : what's the proper thing to do and what is the logic behind it ?

TL;DR : If I don't put gtk.main() in the same function which display the notification, the action_callback function is not triggered when it should. Since this function needs to be put in the gtk mainloop, I'm then stuck with having the gtk mainloop calling itself or the action_callback function not being triggered.

Thanks in advance for any help ;)


Solution

  • The problem here is that pynotify has an bug with callbacks on unreferenced objects. In your first snippet, n gets unreferenced (assuming reference counting in cPython) when the main() function exits. Unfortunately, this means that the notification object is destroyed, and the action is not called (although your notification daemon will still show the notification).

    The work-around to this is to keep a reference to that notification. The simplest thing to do is to take your first snippet and change n = pynotify.Notification to self.last_notification = n = pynotify.Notication.

    If you have multiple notifications, you’ll need to throw them in a list or a set, but then you’ll need to make sure they get deleted, both in the case where the action is triggered, and when the timeout expires.