1919 _mswindows = False
2020else :
2121 _mswindows = True
22+ import _winapi
2223
2324_USE_KQUEUE_POLLER = hasattr (select , "kqueue" )
2425_USE_PIDFD = hasattr (os , "pidfd_open" )
@@ -192,9 +193,17 @@ class _BusywaitPoller(_Poller):
192193
193194
194195class ProcessHandle :
196+ def __init__ (self , pid : int = None ):
197+ #FIXME:
198+ #self._pid = pid
199+ self ._pid = None
200+
195201 def alloc_waiter (self ) -> None :
196202 pass
197203
204+ def close (self ) -> none :
205+ pass
206+
198207 @property
199208 def pid (self ) -> int :
200209 # if not cached, go out to the system to get pid
@@ -205,35 +214,52 @@ def pid(self) -> int:
205214
206215class WindowsProcessHandle (ProcessHandle ):
207216 def __init__ (self , handle : _process_support .Handle , pid : int = None ):
217+ super ().__init__ (pid )
208218 self ._handle = handle
209- self ._pid = pid
210219
211220 def close (self ) -> None :
212221 self ._handle = None
213222
214223 @property
215224 def is_closed (self ):
216- return self ._fd is None
225+ return self ._handle is None
217226
218227 @property
219228 def is_definitive (self ):
220229 return True
221230
222231 def _get_pid (self ) -> int :
223- assert False
224- pass
232+ assert not self . is_closed
233+ return _winapi . GetProcessId ( self . _handle )
225234
226- def process_exists (self ) -> bool :
227- return False
235+ def _poll_process (self , timeout : float = None ) -> None :
236+ if timeout is None :
237+ timeout_millis = _winapi .INFINITE
238+ else :
239+ assert timeout >= 0.0
240+ timeout_millis = int (timeout * 1000 )
228241
229- class PosixProcessHandle (ProcessHandle ):
230- # def block_until_completed(timeout: float = None) -> None:
231- def __init__ (self , pid = None ):
232- self ._pid = pid
242+ result = _winapi .WaitForSingleObject (self ._handle , timeout_millis )
243+ if result == _winapi .WAIT_TIMEOUT :
244+ raise TimeoutExpired (timeout )
245+ elif _winapi .WAIT_OBJECT_0 :
246+ return
247+ else :
248+ # winapi.WaitForSingleObject() throws on WAIT_FAILED, so
249+ # we shouldn't get here
250+ assert False , "unexpected return from WaitForSingleObject()"
233251
234- def close (self ) -> none :
235- pass
252+ def process_exists (self ) -> bool :
253+ try :
254+ self ._poll_process (0 )
255+ return False
256+ except TimeoutExpired :
257+ return True
236258
259+ def block_while_alive (self , timeout : float = None ):
260+ self ._poll_process (timeout )
261+
262+ class PosixProcessHandle (ProcessHandle ):
237263
238264#FIXME: this method is redundant now
239265 def _do_waitid (self , id_type , identifier , wait_flags ):
0 commit comments