From 2e534db16828823cf0e69b7d486ca6f06d1d823d Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 28 Jan 2020 15:59:26 -0800 Subject: [PATCH] glib: Create a new module to deal with all things glib - Chmod -x dbus.py. It appears that the file was accidentally set to permissions 0o755 instead of 0o644. glib module will contain: - Code to deal with glib main loop. - Use glib as a way to schedule timely events instead of creating long running threads. - Other mechanisms to help with asynchronous I/O until we start using asyncio. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/__main__.py | 6 ++--- plinth/dbus.py | 36 +++------------------------- plinth/glib.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 36 deletions(-) mode change 100755 => 100644 plinth/dbus.py create mode 100644 plinth/glib.py diff --git a/plinth/__main__.py b/plinth/__main__.py index 0db2af363..b0373fc31 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -21,7 +21,7 @@ import importlib import logging import sys -from . import (__version__, cfg, dbus, frontpage, log, menu, module_loader, +from . import (__version__, cfg, frontpage, glib, log, menu, module_loader, setup, utils, web_framework, web_server) if utils.is_axes_old(): @@ -130,7 +130,7 @@ def adapt_config(arguments): def on_web_server_stop(): """Stop all other threads since web server is trying to exit.""" setup.stop() - dbus.stop() + glib.stop() def main(): @@ -178,7 +178,7 @@ def main(): setup.run_setup_in_background() - dbus.run() + glib.run() web_server.init() web_server.run(on_web_server_stop) diff --git a/plinth/dbus.py b/plinth/dbus.py old mode 100755 new mode 100644 index eedc46f98..884608a64 --- a/plinth/dbus.py +++ b/plinth/dbus.py @@ -23,16 +23,13 @@ import threading from plinth.utils import import_from_gi -from . import network, setup +from . import setup -glib = import_from_gi('GLib', '2.0') gio = import_from_gi('Gio', '2.0') logger = logging.getLogger(__name__) -_thread = None _server = None -_main_loop = None class PackageHandler(): @@ -127,35 +124,8 @@ class DBusServer(): # service again. -def run(): - """Run a glib main loop forever in a thread.""" - global _thread - _thread = threading.Thread(target=_run) - _thread.start() - - -def stop(): - """Exit glib main loop and end the thread.""" - if _main_loop: - logger.info('Exiting main loop for D-Bus services') - _main_loop.quit() - - -def _run(): - """Connect to D-Bus and run main loop.""" - logger.info('Started new thread for D-Bus services') - +def init(): + """Connect to D-Bus service. Must be run from glib thread.""" global _server _server = DBusServer() _server.connect() - - # Initialize all other modules that glib main loop - # XXX: Refactor this code into separate 'glib' module later - network.init() - - global _main_loop - _main_loop = glib.MainLoop() - _main_loop.run() - _main_loop = None - - logger.info('D-Bus services thread exited.') diff --git a/plinth/glib.py b/plinth/glib.py new file mode 100644 index 000000000..b5fcac560 --- /dev/null +++ b/plinth/glib.py @@ -0,0 +1,59 @@ +# +# This file is part of FreedomBox. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +import logging +import threading + +from plinth import dbus, network +from plinth.utils import import_from_gi + +glib = import_from_gi('GLib', '2.0') + +_thread = None +_main_loop = None + +logger = logging.getLogger(__name__) + + +def run(): + """Run a glib main loop forever in a thread.""" + global _thread + _thread = threading.Thread(target=_run) + _thread.start() + + +def stop(): + """Exit glib main loop and end the thread.""" + if _main_loop: + logger.info('Exiting glib main loop') + _main_loop.quit() + + +def _run(): + """Connect to D-Bus and run main loop.""" + logger.info('Started new thread for glib main loop.') + + # Initialize all modules that use glib main loop + dbus.init() + network.init() + + global _main_loop + _main_loop = glib.MainLoop() + _main_loop.run() + _main_loop = None + + logger.info('Glib main loop thread exited.')