I want to use a bridge between a flask webapp and python subprocesses under python3. I couldn't find a copy paste-able example of a service instantiated without config files for the same.

The reason for wanting to do this is so that I can have multiple threads/processes and use a single Dbus process as a session broker between app instances to hold game state.

All of the trivial Dbus examples I found only included python2.x demos or old versions of whatever mainloop vehicle you prefer.


This example requires:

python3-dbus.mainloop.pyqt5, python3-dbus

Server example:

import signal, sys
signal.signal(signal.SIGINT, signal.SIG_DFL)

from PyQt5.QtCore import *
import math
import dbus
import dbus.service
from dbus.mainloop.pyqt5 import DBusQtMainLoop

class Server(dbus.service.Object):
  def __init__(self):
    busName = dbus.service.BusName('org.documentroot.Fisticuffs', bus = dbus.SessionBus())
    dbus.service.Object.__init__(self, busName, '/Fisticuffs')
    #Insert subprocesses here...

  @dbus.service.method('org.documentroot.Fisticuffs', in_signature = 's', out_signature = 's')
  def move(self, a): return "Move recieved"
  @dbus.service.method('org.documentroot.Fisticuffs', in_signature = 'dd', out_signature = 'd')
  def add(self, a, b): return a+b

DBusQtMainLoop(set_as_default = True)
app = QCoreApplication(sys.argv)
server = Server()

Client example:

import dbus

bus = dbus.SessionBus()
server = bus.get_object('org.documentroot.Fisticuffs', '/Fisticuffs')
print('5 and 10 are:')
print(server.add(5, 10, dbus_interface = 'org.documentroot.Fisticuffs'))

Adapted from here to get pyqt5 to listen to Ctl+C
and here to update a tutorial on getting a dbus-python server to run in the PyQt5 mainloop on python3.

  • All I get is ModuleNotFoundError: No module named 'dbus.mainloop.pyqt5' Apr 24 '20 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.