comparison libervia/cli/loops.py @ 4141:ba8ddfdd334f

cli (loops): run GLib loop in same thread as asyncio: use the new `install_glib_asyncio_iteration` to run GLib in the same thread as asyncio. rel 426
author Goffi <goffi@goffi.org>
date Wed, 01 Nov 2023 14:05:53 +0100
parents 47401850dec6
children 0f8ea0768a3b
comparison
equal deleted inserted replaced
4140:13dd5660c28f 4141:ba8ddfdd334f
19 import sys 19 import sys
20 import asyncio 20 import asyncio
21 import logging as log 21 import logging as log
22 from libervia.backend.core.i18n import _ 22 from libervia.backend.core.i18n import _
23 from libervia.cli.constants import Const as C 23 from libervia.cli.constants import Const as C
24 from libervia.frontends.tools import aio
24 25
25 log.basicConfig(level=log.WARNING, 26 log.basicConfig(level=log.WARNING,
26 format='[%(name)s] %(message)s') 27 format='[%(name)s] %(message)s')
27 28
28 USER_INTER_MSG = _("User interruption: good bye") 29 USER_INTER_MSG = _("User interruption: good bye")
36 37
37 38
38 def get_libervia_cli_loop(bridge_name): 39 def get_libervia_cli_loop(bridge_name):
39 if 'dbus' in bridge_name: 40 if 'dbus' in bridge_name:
40 import signal 41 import signal
41 import threading
42 from gi.repository import GLib
43 42
44 class LiberviaCLILoop: 43 class LiberviaCLILoop:
45 44
45 def __init__(self):
46 self.loop = loop = asyncio.get_event_loop()
47
46 def run(self, libervia_cli, args, namespace): 48 def run(self, libervia_cli, args, namespace):
49 aio.install_glib_asyncio_iteration()
47 signal.signal(signal.SIGINT, self._on_sigint) 50 signal.signal(signal.SIGINT, self._on_sigint)
48 self._glib_loop = GLib.MainLoop() 51 loop = self.loop
49 threading.Thread(target=self._glib_loop.run).start()
50 loop = asyncio.get_event_loop()
51 loop.run_until_complete(libervia_cli.main(args=args, namespace=namespace)) 52 loop.run_until_complete(libervia_cli.main(args=args, namespace=namespace))
52 loop.run_forever() 53 loop.run_forever()
53 54
54 def quit(self, exit_code): 55 def quit(self, exit_code):
55 loop = asyncio.get_event_loop() 56 loop = asyncio.get_event_loop()
56 loop.stop() 57 loop.stop()
57 self._glib_loop.quit()
58 sys.exit(exit_code) 58 sys.exit(exit_code)
59 59
60 def call_later(self, delay, callback, *args): 60 def call_later(self, delay, callback, *args):
61 """call a callback repeatedly 61 """call a callback repeatedly
62 62