0

I am attempting to use pynotify for a small project, but am having a strange problem on my Fedora 13 machine. It appears that when pynotify is run programmatically it crashes when show() is called - however if I type that line myself it runs fine! I have tested it also on my Ubuntu box, where it runs absolutely fine.

My testing code is:

import pynotify

pynotify.init('someName')
n = pynotify.Notification("Summary", "Body")
n.show()

And the results of running this:

$ python -i test.py 
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    n.show()
glib.GError: Unable to connect to server
>>> n.show()
True
>>> 

So, does anyone have any ideas what may cause this sort of behaviour? Unfortunately the Fedora environment is one that I have little control over, so any solutions requiring root access/etc would not really work. I can try installing pynotify locally, however, if needed. Thanks for any help.

Stephen
  • 6,027
  • 4
  • 37
  • 55
  • The error message indicates that you have no DBus server running or that you don't have permission to use it. What is the value of $DBUS_SESSION_BUS_ADDRESS? – filmor Apr 11 '11 at 15:16
  • @filmor "unix:abstract=/tmp/dbus-GclYzMQHLA,guid=d09f45c01f86826fe519fb84000a0587", from either running `echo $DBUS_SESSION_BUS_ADDRESS` in a terminal, or `os.system('$DBUS_SESSION_BUS_ADDRESS')` or `os.getenv("DBUS_SESSION_BUS_ADDRESS")` in the python program. – Stephen Apr 11 '11 at 15:27
  • Not really. It's an interesting thing that I've noticed on these machines - `notify-send` appears to work but does absolutely nothing. There is no error, but also no notification bubble! But remember, like I said, calling `n.show()` manually with pynotify has given me a notification bubble before... – Stephen Apr 11 '11 at 15:42
  • @filmor - Any ideas? There's a nice big bounty on it now... ;) – Stephen Apr 14 '11 at 12:37
  • Does pynotify.init() return true when run in the script? I guess it does but worth checking. It sounds like a race condition might be occurring. Can you try adding time.sleep(1) before the n.show() in the script and see if that helps? – Owen Fraser-Green Apr 14 '11 at 21:02
  • @Owen Fraser-Green - Good news and bad news! Your sleep method unfortunately did not work, but it did point me towards an answer that did work. It's a horrible hack, but if you `try ... except` the first `n.show()` and then run another it works fine... so, thanks for the idea. Not sure what to do regarding an answer here - you were the closest to the right answer so if you want to take it I'll be happy to give you the bounty... – Stephen Apr 15 '11 at 13:19
  • @Stephen - Hi, sorry, didn't see your comment before now. Since you came up with the workaround, I couldn't have laid claim to the bounty anyway. I had a look through the pynotify and libnotify source to find the root cause but there are no obvious candidates. I think it must be something happening deeper down in DBus. – Owen Fraser-Green Apr 19 '11 at 08:38

1 Answers1

0

Since Owen has not accepted my offer to take this answer, here is the solution that worked for me. Note that I have no idea why this works (other than vague guesses), and that I don't make any sort of guarantees about whether this is a good solution or not, but maybe if you are ever in as odd a position as I was this will help.

If you execute n.show() twice, it will run successfully the second time. Therefore, in order to avoid setting two notifications on a system where Pynotify does work correctly, I have used the following:

import pynotify

pynotify.init('someName')
n = pynotify.Notification("Summary", "Body")
try:
    n.show()
except:
    n.show()

Note of course that this small example has flaws, least of all the outcome if there is an actual problem with Pynotify that will be thrown on both n.show()s - this is merely a minimum working example.

Stephen
  • 6,027
  • 4
  • 37
  • 55