我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用errno.ECHILD。
def kill_cmd(self): if self.cmd_pid > 1: try: os.close(self.cmd_fd) except OSError, e: if e.errno == errno.EBADF: pass # already closed else: raise e try: os.kill(self.cmd_pid, signal.SIGKILL) os.waitpid(self.cmd_pid, 0) except OSError, e: if e.errno not in [errno.ECHILD, errno.ESRCH]: raise Exception('unhandled errno: %d' % e.errno) self.cmd_pid = -1
def _kill_ssh(self): if self.ssh_pid > 1: try: os.kill(self.ssh_pid, signal.SIGTERM) os.waitpid(self.ssh_pid, 0) except OSError, e: if e.errno not in [errno.ECHILD, errno.ESRCH]: raise Exception('unhandled errno: %d' % e.errno) self.self_pid = -1 try: os.close(self.ssh_fd) except OSError, e: if e.errno == errno.EBADF: pass # already closed else: print 'WHAT?', e raise e
def close_session(self, authkey): session = self.sessions[authkey] # (Session, RemoteSession) tuple released = [] # loop through share allocators for a in self.allocators: allocator = self.allocators[a] released.extend(self.allocators[a].close_session(session)) try: session[LOCAL].terminate() # don't bother being nice. except OSError, e: if e.errno not in [errno.ECHILD, errno.ESRCH]: raise Exception('unhandled errno: %d' % e.errno) del self.sessions[authkey] # re-add released resources to the remote master broker if this broker # is configured to share if released and self.is_sharing(): self.update_sharing() if not self.allocating: # handover in progress. shut down when no sessions with allocations # remain if not self.sessions: self.shutdown(Exit('broker restarted. please reconnect'))
def shutdown(self, details=None): # terminate all processes started by the broker if self.hsl: self.hsl.terminate() if self.brl: self.brl.terminate() if self.wlan_lister: self.wlan_lister.terminate() if self.pm_lister: self.pm_lister.terminate() self.stop_sharing() for session in self.sessions.values(): try: # send SIGTERM, not SIGKILL, so that Session.shutdown() runs session[LOCAL].terminate() except OSError, e: if e.errno not in [errno.ECHILD, errno.ESRCH]: raise Exception('unhandled errno: %d' % e.errno) Control.shutdown(self, details) # does not return. always do last
def _waitpid(self, wanted_pid): if wanted_pid: if wanted_pid not in self.dict: raise PtraceError("Unknown PID: %r" % wanted_pid, pid=wanted_pid) debug("Wait process %s" % wanted_pid) try: pid, status = waitpid(wanted_pid, 0) except OSError, err: if err.errno == ECHILD: process = self[wanted_pid] raise process.processTerminated() else: raise err else: debug("Wait any process") pid, status = waitpid(-1, 0) if wanted_pid and pid != wanted_pid: raise PtraceError("Unwanted PID: %r (instead of %s)" % (pid, wanted_pid), pid=pid) return pid, status
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 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 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 run(command): child_pid = os.fork() if child_pid == 0: os.execlp(command[0], *command) else: while True: try: os.waitpid(child_pid, 0) except OSError as error: if error.errno == errno.ECHILD: # No child processes. # It has exited already. break elif error.errno == errno.EINTR: # Interrupted system call. # This happens when resizing the terminal. pass else: # An actual error occurred. raise
def wait_on_children(self): """Wait on children exit.""" while self.running: try: pid, status = os.wait() if os.WIFEXITED(status) or os.WIFSIGNALED(status): self._remove_children(pid) self._verify_and_respawn_children(pid, status) except OSError as err: if err.errno not in (errno.EINTR, errno.ECHILD): raise except KeyboardInterrupt: LOG.info(_LI('Caught keyboard interrupt. Exiting.')) os.killpg(0, signal.SIGTERM) break except exception.SIGHUPInterrupt: self.reload() continue eventlet.greenio.shutdown_safe(self.sock) self.sock.close() LOG.debug('Exited')
def wait(self, timeout=0): """ Wait for child process to terminate. Returns returncode attribute. """ if self.returncode is None: try: pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0) except OSError, 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. sts = 0 self._handle_exitstatus(sts) return self.returncode
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(self, timeout=None, check_interval=0.01): # Instead of a blocking OS call, this version of wait() uses logic # borrowed from the eventlet 0.2 processes.Process.wait() method. if timeout is not None: endtime = time.time() + timeout try: while True: status = self.poll() if status is not None: return status if timeout is not None and time.time() > endtime: raise TimeoutExpired(self.args, timeout) eventlet.sleep(check_interval) except OSError as e: if e.errno == errno.ECHILD: # no child process, this happens if the child process # already died and has been cleaned up return -1 else: raise
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
def test_mockErrorECHILDInReapProcess(self): """ Test that reapProcess doesn't log anything when waitpid raises a C{OSError} with errno C{ECHILD}. """ self.mockos.child = False cmd = b'/mock/ouch' self.mockos.waitChild = (0, 0) d = defer.Deferred() p = TrivialProcessProtocol(d) proc = reactor.spawnProcess(p, cmd, [b'ouch'], env=None, usePTY=False) self.assertEqual(self.mockos.actions, [("fork", False), "waitpid"]) self.mockos.raiseWaitPid = OSError() self.mockos.raiseWaitPid.errno = errno.ECHILD # This should not produce any errors proc.reapProcess()
def wait_on_children(self): while self.running: try: pid, status = os.wait() if os.WIFEXITED(status) or os.WIFSIGNALED(status): self._remove_children(pid) self._verify_and_respawn_children(pid, status) except OSError as err: if err.errno not in (errno.EINTR, errno.ECHILD): raise except KeyboardInterrupt: LOG.info('Caught keyboard interrupt. Exiting.') break except glare_exc.SIGHUPInterrupt: self.reload() continue eventlet.greenio.shutdown_safe(self.sock) self.sock.close() LOG.debug('Exited')
def is_finished_task(self, task_id): """Determine if task has finished. Calling os.waitpid removes finished child process zombies. """ pid = self.pid_dict[task_id] try: (childpid, status) = os.waitpid(pid, os.WNOHANG) except OSError as ex: if ex.errno != errno.ECHILD: # should not happen self.log_error("Process hasn't exited with errno.ECHILD: %s" % task_id) raise # the process is already gone return False if childpid != 0: prefix = "Task #%s" % task_id self.log_info(get_process_status(status, prefix)) return True return False
def _wait_event_pid(self, wanted_pid, blocking=True): """ Wait for a process event from the specified process identifier. If blocking=False, return None if there is no new event, otherwise return an object based on ProcessEvent. """ process = None while not process: try: pid, status = self._waitpid(wanted_pid, blocking) except OSError as err: if err.errno == ECHILD: process = self.dict[wanted_pid] return process.processTerminated() else: raise err if not blocking and not pid: return None try: process = self.dict[pid] except KeyError: warning("waitpid() warning: Unknown PID %r" % pid) return process.processStatus(status)
def wait(self): """Wait for child process to terminate. Returns returncode attribute.""" if 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. sts = 0 self._handle_exitstatus(sts) return self.returncode
def _reapChildren(self): """Cleans up self._children whenever children die.""" while True: try: pid, status = os.waitpid(-1, os.WNOHANG) except OSError, e: if e[0] == errno.ECHILD: break raise if pid <= 0: break if self._children.has_key(pid): # Sanity check. if self._children[pid]['file'] is not None: self._children[pid]['file'].close() del self._children[pid]
def _try_cleanup_process(cls, pid): try: ret_pid, status = os.waitpid(pid, os.WNOHANG) except OSError as e: if errno_from_exception(e) == errno.ECHILD: return if ret_pid == 0: return assert ret_pid == pid subproc = cls._waiting.pop(pid) subproc.io_loop.add_callback_from_signal( subproc._set_returncode, status)