Skip to content

Commit

Permalink
_FDWaker in general does not need the reactor
Browse files Browse the repository at this point in the history
So don't give it one.

The "foreign event loop" reactors do need a reactor so preserve theirs.
  • Loading branch information
exarkun committed Nov 7, 2022
1 parent cf97936 commit c17a80e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
7 changes: 6 additions & 1 deletion src/twisted/internet/_glibbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class GlibWaker(_UnixWaker):
Run scheduled events after waking up.
"""

def __init__(self, reactor):
super().__init__()
self.reactor = reactor

def doRead(self) -> None:
super().doRead()
self.reactor._simulate()
Expand Down Expand Up @@ -106,7 +110,8 @@ class GlibReactorBase(posixbase.PosixReactorBase, posixbase._PollLikeMixin):

# Install a waker that knows it needs to call C{_simulate} in order to run
# callbacks queued from a thread:
_wakerFactory = GlibWaker
def _wakerFactory(self) -> GlibWaker:
return GlibWaker(self)

def __init__(self, glib_module: Any, gtk_module: Any, useGtk: bool = False) -> None:
self._simtag = None
Expand Down
12 changes: 4 additions & 8 deletions src/twisted/internet/_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ class _FDWaker(log.Logger):
i = None
o = None

def __init__(self, reactor):
def __init__(self):
"""Initialize."""
self.reactor = reactor
self.i, self.o = os.pipe()
fdesc.setNonBlocking(self.i)
fdesc._setCloseOnExec(self.i)
Expand Down Expand Up @@ -244,22 +243,19 @@ class _SIGCHLDWaker(_FDWaker):
received.
"""

def __init__(self, reactor):
_FDWaker.__init__(self, reactor)

def install(self):
def install(self) -> None:
"""
Install the handler necessary to make this waker active.
"""
installHandler(self.o)

def uninstall(self):
def uninstall(self) -> None:
"""
Remove the handler which makes this waker active.
"""
installHandler(-1)

def doRead(self):
def doRead(self) -> None:
"""
Having woken up the reactor in response to receipt of
C{SIGCHLD}, reap the process which exited.
Expand Down
15 changes: 6 additions & 9 deletions src/twisted/internet/cfreactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class _WakerPlus(_UnixWaker):
do.
"""

def __init__(self, reactor):
super().__init__()
self.reactor = reactor

def doRead(self):
"""
Wake up the loop and force C{runUntilCurrent} to run immediately in the
Expand Down Expand Up @@ -136,15 +140,8 @@ def __init__(self, runLoop=None, runner=None):
self._cfrunloop = runLoop
PosixReactorBase.__init__(self)

def installWaker(self):
"""
Override C{installWaker} in order to use L{_WakerPlus}; otherwise this
should be exactly the same as the parent implementation.
"""
if not self.waker:
self.waker = _WakerPlus(self)
self._internalReaders.add(self.waker)
self.addReader(self.waker)
def _wakerFactory(self) -> _WakerPlus:
return _WakerPlus(self)

def _socketCallback(
self, cfSocket, callbackType, ignoredAddress, ignoredData, context
Expand Down
9 changes: 5 additions & 4 deletions src/twisted/internet/posixbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from twisted.internet.main import CONNECTION_DONE, CONNECTION_LOST
from twisted.python import failure, log
from twisted.python.runtime import platform, platformType
from ._signals import _SIGCHLDWaker, _Waker
from ._signals import _IWaker, _SIGCHLDWaker, _Waker

# Exceptions that doSelect might return frequently
_NO_FILENO = error.ConnectionFdescWentAway("Handler has no fileno method")
Expand Down Expand Up @@ -118,7 +118,8 @@ class PosixReactorBase(_DisconnectSelectableMixin, ReactorBase):

# Callable that creates a waker, overrideable so that subclasses can
# substitute their own implementation:
_wakerFactory = _Waker
def _wakerFactory(self) -> _IWaker:
return _Waker()

def installWaker(self):
"""
Expand All @@ -128,7 +129,7 @@ def installWaker(self):
the reactor. On Windows we use a pair of sockets.
"""
if not self.waker:
self.waker = self._wakerFactory(self)
self.waker = self._wakerFactory()
self._internalReaders.add(self.waker)
self.addReader(self.waker)

Expand All @@ -142,7 +143,7 @@ def _handleSignals(self):
super()._handleSignals()
if platformType == "posix" and processEnabled:
if not self._childWaker:
self._childWaker = _SIGCHLDWaker(self)
self._childWaker = _SIGCHLDWaker()
self._internalReaders.add(self._childWaker)
self.addReader(self._childWaker)
self._childWaker.install()
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/internet/test/test_posixbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def test_noWakerConstructionWarnings(self):
"""
No warnings are generated when constructing the waker.
"""
waker = _Waker(reactor=None)
waker = _Waker()

warnings = self.flushWarnings()
self.assertEqual(len(warnings), 0, warnings)
Expand Down

0 comments on commit c17a80e

Please sign in to comment.