Mainloops Role, DBus Event Loops

This is a standard example of a DBus service.

import dbus
import gobject
from dbus import service
# from gi._gobject import MainLoop
from dbus.mainloop.glib import DBusGMainLoop

class DBusServer(service.Object):
    def __init__(self, name, object_path):
        # super(service.Object, self).__init__(name, object_path)
        dbus.service.Object.__init__(self, name, object_path)

    @dbus.service.method("com.test", in_signature='s', out_signature="s")
    def test(self, args):
        return args + " Sent by dbus client"

    @dbus.service.method("com.test", in_signature='s', out_signature="s")
    def foo(self, args):
        return "foo"

bus_loop = DBusGMainLoop(set_as_default=True)
session_bus = dbus.SessionBus()
session_name = service.BusName("com.test", session_bus)
dbus_server = DBusServer(session_name, "/test")

loop = gobject.MainLoop()

try:
    loop.run()
except KeyboardInterrupt:
    loop.quit()

I have questions regarding the two mainloops used here
1. What is the role of each mainloop or event loop in this code (if I use the correct terminology. Both of them are event loops that I assume)
2. If my application is not a GUI, why should I use gobject mainloop or qt mainloop, since it is necessary to detect user-generated events from the X11 library (in the case of Linux)
3. Why can't I use an infinite loop that does nothing instead of the main gobject loop (as follows)

while True:
    pass
+2
1

, . .

1:

, , mainloop, loop = gobject.MainLoop(). , . D-Bus, .

2:

, D-Bus, , mainloop. dbus-python mainloop, mainloop - ( dbus-python, GLib).

3:

- , , D-Bus ( ).

:

, mainloop , . D-Bus , , D-Bus. mainloop. , , , . , mainloops, , "" , , , , , , (, GLib).

D-Bus dbus-python , mainloop, mainloop. mainloop - , mainloop , mainloop (, , D-Bus). , .

, , mainloop, , . , , , /, mainloop, , mainlop . , .

bus_loop, :

bus_loop = DBusGMainLoop(set_as_default=True)

... , dbus.mainloop.NativeMainLoop, mainloop dbus-python.

dbus-python GLL mainloop, . dbus-python GLL-mainloop - GLL mainloop, :

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

GLib, GLL-GLOBAL-ROOL , dbus-python . mainloop - , GLib. mainloop gobject, :

loop = gobject.MainLoop()
loop.run()

mainloop , . "" D-Bus dbus-python GObject gobject ( GLib).

, , , :

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

- , , mainloop, :

loop = gobject.MainLoop()
loop.run()

, - . , GLib , , .

, GLib GMainLoop GMainContext . , .

+6

Source: https://habr.com/ru/post/1621889/


All Articles