如何重新启动dbus SessionBus

时间:2015-01-07 18:18:29

标签: python dbus

我在一个项目中使用dbus-python,该项目(有时)由cron或udev启动而没有任何environ。一旦import dbus被调用(直接或由任何其他导入的类),这使得dbus.SessionBus()连接到(几乎)空的总线。无论我在致电environ之前是否设置了正确dbus.SessionBus()

$ env -i DISPLAY=$DISPLAY python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ
environ({'DISPLAY': ':0'})
>>> import dbus
>>> bus = dbus.SessionBus()
>>> bus.list_names()
dbus.Array([dbus.String('org.freedesktop.DBus'), dbus.String(':1.2')], signature=dbus.Signature('s'))
>>> 

仅当我在 environ之前设置正确的import dbus 时,它才有效。

$ env | grep ^DBUS
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-FGccmgO6TN
$ env -i DISPLAY=$DISPLAY python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['DBUS_SESSION_BUS_ADDRESS'] = 'unix:abstract=/tmp/dbus-FGccmgO6TN'
>>> os.environ                                                                   
environ({'DISPLAY': ':0', 'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-FGccmgO6TN'})
>>> import dbus
>>> bus = dbus.SessionBus()
>>> bus.list_names()
dbus.Array([dbus.String('org.freedesktop.DBus'), dbus.String('com.canonical.Unity.Launcher'), dbus.String('org.freedesktop.Notifications'), dbus.String(':1.7'), dbus.String(':1.107'), dbus.String(':1.8'), dbus.String('com.canonical.indicator.datetime'), dbus.String(':1.108'), dbus.String(':1.9'), dbus.String(':1.109'), dbus.String('com.canonical.Unity.Master.Scope.home.T6004534105088'), dbus.String('org.gtk.Private.AfcVolumeMonitor'), dbus.String('com.canonical.indicator.sound'), dbus.String('org.gtk.vfs.Daemon'), dbus.String('org.pulseaudio.Server'), dbus.String('com.canonical.indicator.application'), dbus.String('com.canonical.Unity.Webapps.Service'), dbus.String('com.ubuntu.Upstart'), dbus.String('org.gnome.SessionManager'), dbus.String('org.gnome.evolution.dataserver.Sources2'), dbus.String('com.canonical.indicator.session'), dbus.String('com.canonical.hud'), dbus.String(':1.60'), dbus.String(':1.61'), dbus.String('com.canonical.indicator.messages'), dbus.String(':1.62'), dbus.String('org.gnome.GConf'), dbus.String(':1.40'), dbus.String(':1.63'), dbus.String('org.a11y.Bus'), dbus.String('org.gnome.network_manager_applet'), dbus.String(':1.64'), dbus.String(':1.20'), dbus.String(':1.65'), dbus.String('org.gnome.SettingsDaemon.Power'), dbus.String(':1.273'), dbus.String('org.gnome.EvolutionAlarmNotify'), dbus.String(':1.66'), dbus.String('org.gnome.keyring'), dbus.String('org.gnome.Shell'), dbus.String(':1.45'), dbus.String('com.canonical.Unity.WindowStack'), dbus.String('org.gnome.Gedit'), dbus.String(':1.110'), dbus.String(':1.68'), dbus.String(':1.46'), dbus.String('com.canonical.Unity.Panel.Service.Desktop'), dbus.String(':1.24'), dbus.String(':1.133'), dbus.String(':1.69'), dbus.String(':1.47'), dbus.String(':1.25'), dbus.String(':1.134'), dbus.String('org.gnome.DejaDup.Monitor'), dbus.String(':1.112'), dbus.String(':1.48'), dbus.String(':1.26'), dbus.String(':1.135'), dbus.String(':1.113'), dbus.String('org.gtk.Private.MTPVolumeMonitor'), dbus.String(':1.49'), dbus.String(':1.27'), dbus.String(':1.136'), dbus.String(':1.114'), dbus.String('org.freedesktop.Telepathy.Client.IndicatorApprover'), dbus.String(':1.28'), dbus.String(':1.137'), dbus.String(':1.115'), dbus.String('org.freedesktop.Telepathy.ChannelDispatcher'), dbus.String('ca.desrt.dconf'), dbus.String(':1.29'), dbus.String(':1.116'), dbus.String('org.gnome.SettingsDaemon'), dbus.String('com.canonical.indicator.printers'), dbus.String('org.gnome.evolution.dataserver.Calendar4'), dbus.String('org.freedesktop.ScreenSaver'), dbus.String('org.gnome.SettingsDaemon.XRANDR'), dbus.String('com.canonical.Unity.Scope.LocalFiles'), dbus.String('org.ayatana.bamf'), dbus.String('org.freedesktop.FileManager1'), dbus.String('com.canonical.indicator.bluetooth'), dbus.String('im.pidgin.purple.PurpleService'), dbus.String('org.PulseAudio1'), dbus.String('org.gnome.Nautilus'), dbus.String('org.freedesktop.Telepathy.AccountManager'), dbus.String('com.canonical.indicator.keyboard'), dbus.String('org.freedesktop.Telepathy.Client.IndicatorObserver'), dbus.String('com.canonical.Unity'), dbus.String('org.gnome.evolution.dataserver.AddressBook5'), dbus.String(':1.70'), dbus.String(':1.71'), dbus.String('org.gnome.ScreenSaver'), dbus.String('org.gnome.zeitgeist.Engine'), dbus.String(':1.72'), dbus.String(':1.50'), dbus.String(':1.51'), dbus.String('org.gtk.vfs.Metadata'), dbus.String(':1.96'), dbus.String('org.gtk.Private.UDisks2VolumeMonitor'), dbus.String(':1.30'), dbus.String(':1.97'), dbus.String(':1.53'), dbus.String(':1.31'), dbus.String(':1.98'), dbus.String('org.gnome.SettingsDaemon.Keyboard'), dbus.String(':1.32'), dbus.String(':1.10'), dbus.String(':1.55'), dbus.String(':1.33'), dbus.String(':1.11'), dbus.String('org.freedesktop.Telepathy.MissionControl5'), dbus.String(':1.56'), dbus.String(':1.34'), dbus.String('org.kde.StatusNotifierWatcher'), dbus.String(':1.12'), dbus.String('com.canonical.AppMenu.Registrar'), dbus.String('org.gnome.Terminal.Display_0'), dbus.String('com.canonical.Unity.Scope.Home'), dbus.String(':1.79'), dbus.String('de.mh21.indicator.multiload'), dbus.String(':1.57'), dbus.String(':1.0'), dbus.String(':1.13'), dbus.String(':1.100'), dbus.String(':1.1'), dbus.String(':1.14'), dbus.String('org.gnome.zeitgeist.datahub'), dbus.String(':1.101'), dbus.String(':1.59'), dbus.String(':1.37'), dbus.String('org.freedesktop.secrets'), dbus.String(':1.2'), dbus.String(':1.15'), dbus.String(':1.102'), dbus.String('org.gnome.Nautilus.SearchProvider'), dbus.String(':1.38'), dbus.String(':1.3'), dbus.String(':1.16'), dbus.String('com.canonical.Unity.Scope.Applications'), dbus.String('org.gnome.zeitgeist.SimpleIndexer'), dbus.String(':1.103'), dbus.String(':1.4'), dbus.String(':1.17'), dbus.String('com.canonical.Unity.SmartScopes.RemoteScopesModel'), dbus.String(':1.104'), dbus.String('org.gtk.Private.GPhoto2VolumeMonitor'), dbus.String(':1.5'), dbus.String(':1.105'), dbus.String(':1.6'), dbus.String('org.freedesktop.IBus'), dbus.String(':1.19'), dbus.String('com.canonical.indicator.power')], signature=dbus.Signature('s'))
>>> 

我可以做些什么来重新初始化dbus.SessionBus()?我试着用imp.reload(dbus)没有运气......

1 个答案:

答案 0 :(得分:0)

好的,解决方法是直接连接到SessionBus套接字。

bus = dbus.bus.BusConnection(os.environ['DBUS_SESSION_BUS_ADDRESS'])

(之前我用过这个,但由于其他问题,我认为这不起作用)