本文介绍了Python DBUS SESSION_BUS-X11依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经运行了示例python代码,在Ubuntu桌面上也可以使用:

I've got running sample python code which is fine in Ubuntu desktop:

import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
from dbus.mainloop.glib import threads_init
import subprocess
from subprocess import call

gobject.threads_init()
threads_init()
dbus.mainloop.glib.DBusGMainLoop( set_as_default = True )

p = subprocess.Popen('dbus-launch --sh-syntax', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
call( "export DBUS_SESSION_BUS_ADDRESS" , shell=True )
call( "export DBUS_SESSION_BUS_PID" , shell=True )

bus = dbus.SessionBus()

# get DBUS objects, do other stuff with SESSION_BUS
# in same time we can start more independent processes with this file
# finaly kill the SESSION_BUS process

在桌面上成功之后,我将代码移到了只有Shell的服务器版本。 dbus-launch启动了该过程,但是python dbus.SessionBus()返回 / bin / dbus-launch异常终止,并出现以下错误:自动启动错误:X11初始化失败的错误。

After success on desktop I moved the code to the server edition which is only with shell. The dbus-launch starts the process, but python dbus.SessionBus() returns error with "/bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed".

希望当以 dbus-launch开始的进程成功启动并运行时,SESSION_BUS和X11之间不应严格依赖。错误来自python。

Hope there shouldn't be strict dependency between SESSION_BUS and X11 when the process started with "dbus-launch" go up and running with success. The error comes in python.

最好的解决方案是干净的python或linux环境设置,最糟糕的情况是,但对于某些假的或虚拟的X11可能是可接受的(尝试时我并不幸运。

Best solution will be clean python or linux environment settings, worst but maybe acceptable with some fake or virtual X11 (I was not lucky when I try it)

推荐答案

问题是您正在运行导出在单独的外壳中调用。您需要捕获 dbus-launch 的输出,解析值,然后使用 os.environ 将其写入环境:

The problem is that you're running the export calls in separate shells. You need to capture the output of dbus-launch, parse the values, and use os.environ to write them to the environment:

p = subprocess.Popen('dbus-launch', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for var in p.stdout:
  sp = var.split('=', 1)
  print sp
  os.environ[sp[0]] = sp[1][:-1]

这篇关于Python DBUS SESSION_BUS-X11依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 12:35