Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions asyncio_glib/glib_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@
)


# The override for GLib.MainLoop installs a signal wakeup fd,
# which interferes with asyncio signal handlers. Try to get the
# direct version.

MainLoop = GLib.MainLoop
RawMainLoop = MainLoop.__bases__[0]
RawMainLoop = RawMainLoop if hasattr(RawMainLoop, 'run') else MainLoop


class BasicMainLoop(RawMainLoop):

# Backwards compatible constructor API
def __new__(cls, context=None):
return GLib.MainLoop.new(context, False)
def _get_main_loop_runner(loop, handle_sigint):
# The override for GLib.MainLoop installs a signal wakeup fd,
# which interferes with asyncio signal handlers. Try to get the
# direct version.
if handle_sigint:
return loop.run
try:
return super(GLib.MainLoop, loop).run
except AttributeError:
return loop.run


class _SelectorSource(GLib.Source):
Expand All @@ -43,7 +39,10 @@ def dispatch(self, callback, args):
for (fd, tag) in self._fd_to_tag.items():
condition = self.query_unix_fd(tag)
events = self._fd_to_events.setdefault(fd, 0)
if condition & GLib.IOCondition.IN:
if (
condition & GLib.IOCondition.IN
or condition & GLib.IOCondition.HUP
):
events |= selectors.EVENT_READ
if condition & GLib.IOCondition.OUT:
events |= selectors.EVENT_WRITE
Expand Down Expand Up @@ -77,8 +76,10 @@ class GLibSelector(selectors._BaseSelectorImpl):
def __init__(self, context, handle_sigint=False):
super().__init__()
self._context = context
self._main_loop = MainLoop(self._context) \
if handle_sigint else BasicMainLoop(self._context)
self._main_loop = GLib.MainLoop.new(self._context, False)
self._main_loop_runner = _get_main_loop_runner(
self._main_loop, handle_sigint
)
self._source = _SelectorSource(self._main_loop)
self._source.attach(self._context)

Expand Down Expand Up @@ -108,7 +109,7 @@ def select(self, timeout=None):

self._source.clear()
if may_block:
self._main_loop.run()
self._main_loop_runner()
else:
self._context.iteration(False)

Expand Down