Skip to content

Commit 7ef6ae9

Browse files
committed
glib_events: optionally retain GLib override SIGINT handler
... as this also appromixates asyncio KeyboardInterrupt SIGINT behavior. Otherwise, one tends to get stuck in mainloop without convenient escape hatch.
1 parent 71e1d58 commit 7ef6ae9

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

asyncio_glib/glib_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
class GLibEventLoop(asyncio.SelectorEventLoop):
1616
"""An asyncio event loop that runs the GLib main loop"""
1717

18-
def __init__(self, main_context=None):
18+
def __init__(self, main_context=None, handle_sigint=False):
1919
if main_context is None:
2020
main_context = GLib.MainContext.default()
21-
selector = glib_selector.GLibSelector(main_context)
21+
selector = glib_selector.GLibSelector(main_context, handle_sigint)
2222
self.selector = selector
2323
super().__init__(selector)
2424

asyncio_glib/glib_selector.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ def clear(self):
6767

6868
class GLibSelector(selectors._BaseSelectorImpl):
6969

70-
def __init__(self, context):
70+
def __init__(self, context, handle_sigint=False):
7171
super().__init__()
7272
self._context = context
73-
self._main_loop = GLib.MainLoop.new(self._context, False)
73+
if handle_sigint:
74+
self._main_loop = GLib.MainLoop()
75+
self._run = GLib.MainLoop.run
76+
else:
77+
self._main_loop = GLib.MainLoop.new(self._context, False)
78+
self._run = g_main_loop_run
7479
self._source = _SelectorSource(self._main_loop)
7580
self._source.attach(self._context)
7681

@@ -100,7 +105,7 @@ def select(self, timeout=None):
100105

101106
self._source.clear()
102107
if may_block:
103-
g_main_loop_run(self._main_loop)
108+
self._run(self._main_loop)
104109
else:
105110
self._context.iteration(False)
106111

0 commit comments

Comments
 (0)