我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用os.WNOHANG。
def stop(self): self._log.debug("stop()") if self.get_commandAlive() == True: for sig, timeout in self.STOP_SIGNALS: try: os.kill(self._pid, sig) except OSError: self._pid = None return if timeout != None: giveup_time = time.time() + timeout while os.waitpid(self._pid, os.WNOHANG) == (0,0): time.sleep(0.1) if time.time() > giveup_time: break else: # Wait until there is a response os.waitpid(self._pid, 0) self._pid = None ###################################### # Implement specific property setters/getters
def get_commandAlive(self): if self._pid != None: try: os.kill(self._pid, 0) if os.waitpid(self._pid, os.WNOHANG) == (0,0): return True else: return False except OSError: pass return False
def stop(self): self._log.debug("%s.stop()", self.naming_service_name) if self.query_commandAlive() == True: for sig, timeout in self.STOP_SIGNALS: try: os.kill(self._pid, sig) except OSError: self._pid = None return if timeout != None: giveup_time = time.time() + timeout while os.waitpid(self._pid, os.WNOHANG) == (0,0): time.sleep(0.1) if time.time() > giveup_time: break else: # Wait until there is a response os.waitpid(self._pid, 0) self._pid = None
def stop(self): self._log.debug("stop()") if self.query_commandAlive() == True: for sig, timeout in self.STOP_SIGNALS: try: os.kill(self._pid, sig) except OSError: self._pid = None return if timeout != None: giveup_time = time.time() + timeout while os.waitpid(self._pid, os.WNOHANG) == (0,0): time.sleep(0.1) if time.time() > giveup_time: break else: # Wait until there is a response os.waitpid(self._pid, 0) self._pid = None ###################################### # Implement specific property setters/getters
def stop(self): self._log.debug("stop()") try: if self.get_commandAlive() == True: for sig, timeout in self.STOP_SIGNALS: try: os.kill(self._pid, sig) except OSError: self._pid = None return if timeout != None: giveup_time = time.time() + timeout while os.waitpid(self._pid, os.WNOHANG) == (0,0): time.sleep(0.1) if time.time() > giveup_time: break else: # Wait until there is a response os.waitpid(self._pid, 0) self._pid = None finally: Resource.stop(self) # CF::LifeCycle
def stop(self): self._log.debug("stop()") if self.query_prop_commandAlive() == True: for sig, timeout in self.STOP_SIGNALS: try: os.kill(self._pid, sig) except OSError: self._pid = None return if timeout != None: giveup_time = time.time() + timeout while os.waitpid(self._pid, os.WNOHANG) == (0,0): time.sleep(0.1) if time.time() > giveup_time: break else: # Wait until there is a response os.waitpid(self._pid, 0) self._pid = None ###################################### # Implement specific property setters/getters
def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid, _WNOHANG=os.WNOHANG, _os_error=os.error): """Check if child process has terminated. Returns returncode attribute. This method is called by __del__, so it cannot reference anything outside of the local scope (nor can any methods it calls). """ if self.returncode is None: try: pid, sts = _waitpid(self.pid, _WNOHANG) if pid == self.pid: self._handle_exitstatus(sts) except _os_error: if _deadstate is not None: self.returncode = _deadstate return self.returncode
def reapProcess(self): """Try to reap a process (without blocking) via waitpid. This is called when sigchild is caught or a Process object loses its "connection" (stdout is closed) This ought to result in reaping all zombie processes, since it will be called twice as often as it needs to be. (Unfortunately, this is a slightly experimental approach, since UNIX has no way to be really sure that your process is going to go away w/o blocking. I don't want to block.) """ try: pid, status = os.waitpid(self.pid, os.WNOHANG) except: log.msg('Failed to reap %d:' % self.pid) log.err() pid = None if pid: self.processEnded(status) unregisterReapProcessHandler(pid, self)
def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid, _WNOHANG=os.WNOHANG, _os_error=os.error, _ECHILD=errno.ECHILD): """Check if child process has terminated. Returns returncode attribute. This method is called by __del__, so it cannot reference anything outside of the local scope (nor can any methods it calls). """ if self.returncode is None: try: pid, sts = _waitpid(self.pid, _WNOHANG) if pid == self.pid: self._handle_exitstatus(sts) except _os_error as e: if _deadstate is not None: self.returncode = _deadstate if e.errno == _ECHILD: # This happens if SIGCLD is set to be ignored or # waiting for child processes has otherwise been # disabled for our process. This child is dead, we # can't get the status. # http://bugs.python.org/issue15756 self.returncode = 0 return self.returncode
def poll(self, flag=os.WNOHANG): if self.returncode is None: while True: try: pid, sts = os.waitpid(self.pid, flag) except os.error as e: if e.errno == errno.EINTR: continue # Child process not yet created. See #1731717 # e.errno == errno.ECHILD == 10 return None else: break if pid == self.pid: if os.WIFSIGNALED(sts): self.returncode = -os.WTERMSIG(sts) else: assert os.WIFEXITED(sts) self.returncode = os.WEXITSTATUS(sts) return self.returncode
def reap_children(): """Use this function at the end of test_main() whenever sub-processes are started. This will help ensure that no extra children (zombies) stick around to hog resources and create problems when looking for refleaks. """ # Reap all our dead child processes so we don't leave zombies around. # These hog resources and might be causing some of the buildbots to die. if hasattr(os, 'waitpid'): any_process = -1 while True: try: # This will raise an exception on Windows. That's ok. pid, status = os.waitpid(any_process, os.WNOHANG) if pid == 0: break except: break
def wait(self): """Wait for child process to terminate. Returns returncode attribute.""" while self.returncode is None: try: pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0) except OSError as e: if e.errno != errno.ECHILD: raise # This happens if SIGCLD is set to be ignored or waiting # for child processes has otherwise been disabled for our # process. This child is dead, we can't get the status. pid = self.pid sts = 0 # Check the pid and loop as waitpid has been known to return # 0 even without WNOHANG in odd situations. issue14396. if pid == self.pid: self._handle_exitstatus(sts) return self.returncode
def sigint_handler(sig, frame): print("Killing sub-process...") if process_handle is not None: global kill_retry_count while process_handle.returncode is None and kill_retry_count < kill_retry_max: kill_retry_count += 1 print("Killing sub-process ({})...".format(kill_retry_count)) try: os.killpg(os.getpgid(process_handle.pid), signal.SIGTERM) os.waitpid(process_handle.pid, os.WNOHANG) except ProcessLookupError: break try: process_handle.wait(1) except subprocess.TimeoutExpired: pass if working_dir is not None: rmdir(working_dir) sys.exit(0)
def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid, _WNOHANG=os.WNOHANG, _os_error=os.error, _ECHILD=errno.ECHILD): """Check if child process has terminated. Returns returncode attribute. This method is called by __del__, so it cannot reference anything outside of the local scope (nor can any methods it calls). """ if self.returncode is None: try: pid, sts = _waitpid(self.pid, _WNOHANG) if pid == self.pid: self._handle_exitstatus(sts) except _os_error as e: if _deadstate is not None: self.returncode = _deadstate elif e.errno == _ECHILD: # This happens if SIGCLD is set to be ignored or # waiting for child processes has otherwise been # disabled for our process. This child is dead, we # can't get the status. # http://bugs.python.org/issue15756 self.returncode = 0 return self.returncode
def wait_impl(self, cpid): option = os.WNOHANG if sys.platform.startswith('aix'): # Issue #11185: wait4 is broken on AIX and will always return 0 # with WNOHANG. option = 0 for i in range(10): # wait4() shouldn't hang, but some of the buildbots seem to hang # in the forking tests. This is an attempt to fix the problem. spid, status, rusage = os.wait4(cpid, option) if spid == cpid: break time.sleep(1.0) self.assertEqual(spid, cpid) self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) self.assertTrue(rusage)
def launch(self): self.check() received = False def set_received(*_): nonlocal received received = True signal.signal(signal.SIGUSR1, set_received) pid = self.fork_zeo() if not received: while not signal.sigtimedwait([signal.SIGUSR1], 1): pid, waitres = os.waitpid(pid, os.WNOHANG) if pid: log.error('Database server failed to start (check log above).') sys.exit(1) settings.DB_URI = urlunsplit(('zeo', '', self.zeo_path, '', '')) log.debug('Launched built-in ZEO') return pid
def abort(self, reason=''): if self.id is None: self.save() if self.proc: self.proc.terminate() elif self.pid: os.kill(self.pid, signal.SIGTERM) pid, stat = os.waitpid(self.pid, os.WNOHANG) if pid == 0: os.kill(self.pid, signal.SIGKILL) pid, stat = os.waitpid(self.pid, os.WNOHANG) if pid == 0: Log.error('Unable to kill job: %s (%i) with pid: %i' % (self.name, self.id, self.pid), reason) self.status = 'Aborted' self.remove_lock_file() self.stop_date = datetime.utcnow() self.running = False Log.warning('Job aborted: %s (%i)' % (self.name, self.id), reason) self.save()
def poll(self, flag=os.WNOHANG): if self.returncode is None: while True: try: pid, sts = os.waitpid(self.pid, flag) except OSError as e: if e.errno == errno.EINTR: continue # Child process not yet created. See #1731717 # e.errno == errno.ECHILD == 10 return None else: break if pid == self.pid: if os.WIFSIGNALED(sts): self.returncode = -os.WTERMSIG(sts) else: assert os.WIFEXITED(sts) self.returncode = os.WEXITSTATUS(sts) return self.returncode