[英]How to create for Dbus Mainloop for two separate Dbus Python programs
我有一個 python 進程監視 Dbus 服務並與之交互(NetworkManager)
目前這在主程序的自己的線程中運行
import dbus.mainloop.glib
import NetworkManager
PYTHON3 = sys.version_info >= (3, 0)
if PYTHON3:
from gi.repository import GObject as gobject
from gi.repository import GLib as glib
else:
import gobject
class NetworkController(threading.Thread):
def __init__(self, run_daemon_mode=False):
# Set up DBus loop
self.loop = None
dbus.mainloop.glib.threads_init()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
if PYTHON3:
self.loop = glib.MainLoop()
else:
self.loop = gobject.MainLoop()
gobject.threads_init()
#Create the thread
super(NetworkController, self).__init__(daemon=run_daemon_mode)
def run(self):
"""
Method to run the DBus main loop (on a thread)
"""
# NetworkManager init stuff...
logger.debug("Starting Network Controller loop.")
self.loop.run()
logger.debug("Network Controller loop has exited.")
def main(argv):
args=cli(argv)
#create network controller object but don't start the thread
network_con = network_controller.NetworkController(True)
network_con.start() #
try:
while True:
我現在需要添加一個線程來控制我的也使用 Dbus 的藍牙 GATT 服務(pyBluez 實現)。 如何構建我的代碼以讓每個進程在它自己的線程中運行並使用 Dbus。
謝謝!
我創建了我的新 class,它使用 Dbus 並且是一個單獨的線程,就像我創建我的第一個 class 一樣。 然后從我的主程序中調用 class 構造函數並啟動每個線程。
class AnotherGlibMainloop(threading.Thread):
def __init__(self, run_daemon_mode=False):
# Set up DBus loop
self.loop = None
dbus.mainloop.glib.threads_init()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
if PYTHON3:
self.loop = glib.MainLoop()
else:
self.loop = gobject.MainLoop()
gobject.threads_init()
#Create the thread
super(NetworkController, self).__init__(daemon=run_daemon_mode)
def run(self):
"""
Method to run the DBus main loop (on a thread)
"""
# NetworkManager init stuff...
logger.debug("Starting Another Glib Mainloop.")
self.loop.run()
logger.debug("Another Glib Mainloop has exited.")
def main(argv): args=cli(argv)
#create network controller object but don't start the thread
network_con = network_controller.NetworkController(True)
network_con.start() #
another_glib_mainloop = AnotherGlibMainloop(True)
another_glib_mainloop.start()
try:
while True:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.