Python DBUS SESSION_BUS - X11 依赖项

发布于 2024-08-19 10:48:21 字数 1023 浏览 9 评论 0原文

我已经在 Ubuntu 桌面上运行了示例 python 代码,效果很好:

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 初始化失败”。

希望当进程以“dbus-launch”启动并成功运行时,SESSION_BUS 和 X11 之间不应该存在严格的依赖关系。错误出现在 python 中。

最好的解决方案是干净的 python 或 linux 环境设置,最糟糕但也许可以接受一些假的或虚拟的 X11(当我尝试时我并不幸运)

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

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".

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.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2024-08-26 10:48:21

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

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]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文