@@ -199,7 +199,7 @@ async def _make_subprocess_transport(self, protocol, args, shell,
199199 extra = None , ** kwargs ):
200200 with warnings .catch_warnings ():
201201 warnings .simplefilter ('ignore' , DeprecationWarning )
202- watcher = events .get_child_watcher ()
202+ watcher = events .get_event_loop_policy (). _watcher
203203
204204 with watcher :
205205 if not watcher .is_active ():
@@ -1009,59 +1009,6 @@ def remove_child_handler(self, pid):
10091009 return True
10101010
10111011
1012- class BaseChildWatcher (AbstractChildWatcher ):
1013-
1014- def __init__ (self ):
1015- self ._loop = None
1016- self ._callbacks = {}
1017-
1018- def close (self ):
1019- self .attach_loop (None )
1020-
1021- def is_active (self ):
1022- return self ._loop is not None and self ._loop .is_running ()
1023-
1024- def _do_waitpid (self , expected_pid ):
1025- raise NotImplementedError ()
1026-
1027- def _do_waitpid_all (self ):
1028- raise NotImplementedError ()
1029-
1030- def attach_loop (self , loop ):
1031- assert loop is None or isinstance (loop , events .AbstractEventLoop )
1032-
1033- if self ._loop is not None and loop is None and self ._callbacks :
1034- warnings .warn (
1035- 'A loop is being detached '
1036- 'from a child watcher with pending handlers' ,
1037- RuntimeWarning )
1038-
1039- if self ._loop is not None :
1040- self ._loop .remove_signal_handler (signal .SIGCHLD )
1041-
1042- self ._loop = loop
1043- if loop is not None :
1044- loop .add_signal_handler (signal .SIGCHLD , self ._sig_chld )
1045-
1046- # Prevent a race condition in case a child terminated
1047- # during the switch.
1048- self ._do_waitpid_all ()
1049-
1050- def _sig_chld (self ):
1051- try :
1052- self ._do_waitpid_all ()
1053- except (SystemExit , KeyboardInterrupt ):
1054- raise
1055- except BaseException as exc :
1056- # self._loop should always be available here
1057- # as '_sig_chld' is added as a signal handler
1058- # in 'attach_loop'
1059- self ._loop .call_exception_handler ({
1060- 'message' : 'Unknown exception in SIGCHLD handler' ,
1061- 'exception' : exc ,
1062- })
1063-
1064-
10651012class ThreadedChildWatcher (AbstractChildWatcher ):
10661013 """Threaded child watcher implementation.
10671014
@@ -1161,15 +1108,10 @@ class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
11611108
11621109 def __init__ (self ):
11631110 super ().__init__ ()
1164- self ._watcher = None
1165-
1166- def _init_watcher (self ):
1167- with events ._lock :
1168- if self ._watcher is None : # pragma: no branch
1169- if can_use_pidfd ():
1170- self ._watcher = PidfdChildWatcher ()
1171- else :
1172- self ._watcher = ThreadedChildWatcher ()
1111+ if can_use_pidfd ():
1112+ self ._watcher = PidfdChildWatcher ()
1113+ else :
1114+ self ._watcher = ThreadedChildWatcher ()
11731115
11741116 def set_event_loop (self , loop ):
11751117 """Set the event loop.
@@ -1185,33 +1127,6 @@ def set_event_loop(self, loop):
11851127 threading .current_thread () is threading .main_thread ()):
11861128 self ._watcher .attach_loop (loop )
11871129
1188- def get_child_watcher (self ):
1189- """Get the watcher for child processes.
1190-
1191- If not yet set, a ThreadedChildWatcher object is automatically created.
1192- """
1193- if self ._watcher is None :
1194- self ._init_watcher ()
1195-
1196- warnings ._deprecated ("get_child_watcher" ,
1197- "{name!r} is deprecated as of Python 3.12 and will be "
1198- "removed in Python {remove}." , remove = (3 , 14 ))
1199- return self ._watcher
1200-
1201- def set_child_watcher (self , watcher ):
1202- """Set the watcher for child processes."""
1203-
1204- assert watcher is None or isinstance (watcher , AbstractChildWatcher )
1205-
1206- if self ._watcher is not None :
1207- self ._watcher .close ()
1208-
1209- self ._watcher = watcher
1210- warnings ._deprecated ("set_child_watcher" ,
1211- "{name!r} is deprecated as of Python 3.12 and will be "
1212- "removed in Python {remove}." , remove = (3 , 14 ))
1213-
1214-
12151130SelectorEventLoop = _UnixSelectorEventLoop
12161131DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy
12171132EventLoop = SelectorEventLoop
0 commit comments