python类killpg()的实例源码

launcher.py 文件源码 项目:core-framework 作者: RedhawkSDR 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def terminate(self):
        for child in self.__children:
            child.terminate()
        self.__children = []

        for sig, timeout in self.STOP_SIGNALS:
            try:
                log.debug('Killing process group %s with signal %s', self.__process.pid, sig)
                os.killpg(self.__process.pid, sig)
            except OSError:
                pass
            giveup_time = time.time() + timeout
            while self.__process.poll() is None:
                if time.time() > giveup_time:
                    break
                time.sleep(0.1)
            if self.__process.poll() is not None:
                break
        self.__process.wait()
        self.__process = None
io_helpers.py 文件源码 项目:core-framework 作者: RedhawkSDR 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __terminate(self,pid):
        sp = self._processes[pid]
        for sig, timeout in self._STOP_SIGNALS:
            try:
                # the group id is used to handle child processes (if they
                # exist) of the component being cleaned up
                if _domainless._DEBUG == True:
                    print "_OutputBase: __terminate () making killpg call on pid " + str(pid) + " with signal " + str(sig)
                _os.killpg(pid, sig)
            except OSError:
                log.error("_OutputBase: __terminate() OSERROR ===============")
                pass
            if timeout != None:
                giveup_time = _time.time() + timeout
            while sp.poll() == None:
                if _time.time() > giveup_time: break
                _time.sleep(0.1)
            if sp.poll() != None: break
        sp.wait()
mpetests.py 文件源码 项目:PyExPool 作者: eXascaleInfolab 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def terminationHandler(cls, signal=None, frame=None, terminate=True):
        """Signal termination handler

        signal  - raised signal
        frame  - origin stack frame
        terminate  - whether to terminate the application
        """
        #if signal == signal.SIGABRT:
        #   os.killpg(os.getpgrp(), signal)
        #   os.kill(os.getpid(), signal)

        if cls._execpool:
            del cls._execpool  # Destructors are caled later
            # Define _execpool to avoid unnessary trash in the error log, which might
            # be caused by the attempt of subsequent deletion on destruction
            cls._execpool = None  # Note: otherwise _execpool becomes undefined
        if terminate:
            sys.exit()  # exit(0), 0 is the default exit code.
tools.py 文件源码 项目:Dallinger 作者: Dallinger 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def stop(self, signal=None):
        """Stop the heroku local subprocess and all of its children.
        """
        signal = signal or self.int_signal
        self.out.log("Cleaning up local Heroku process...")
        if self._process is None:
            self.out.log("No local Heroku process was running.")
            return

        try:
            os.killpg(os.getpgid(self._process.pid), signal)
            self.out.log("Local Heroku process terminated.")
        except OSError:
            self.out.log("Local Heroku was already terminated.")
            self.out.log(traceback.format_exc())
        finally:
            self._process = None
mx.py 文件源码 项目:mx 作者: graalvm 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def _kill_process(pid, sig):
    """
    Sends the signal `sig` to the process identified by `pid`. If `pid` is a process group
    leader, then signal is sent to the process group id.
    """
    pgid = os.getpgid(pid)
    try:
        logvv('[{} sending {} to {}]'.format(os.getpid(), sig, pid))
        if pgid == pid:
            os.killpg(pgid, sig)
        else:
            os.kill(pid, sig)
        return True
    except:
        log('Error killing subprocess ' + str(pid) + ': ' + str(sys.exc_info()[1]))
        return False
heartbreaker.py 文件源码 项目:heartbreaker 作者: lokori 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def killProc(self):
        global proc
        try:
            proc.terminate()
            #os.killpg(proc.pid, signal.SIGKILL)
            #print "after terminate: ", proc.pid
        except:
            pass  
        try:
            proc.poll()
        except:
            pass
            #os.killpg(proc.pid, signal.SIGTERM)
        try:    
            del proc
        except:
            pass
        if os.path.isfile('.target_fail'):
            os.remove('.target_fail')
        if os.path.isfile('.target_resp'):
            os.remove('.target_resp')
__init__.py 文件源码 项目:aetros-cli 作者: aetros 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def raise_sigint():
    """
    Raising the SIGINT signal in the current process and all sub-processes.

    os.kill() only issues a signal in the current process (without subprocesses).
    CTRL+C on the console sends the signal to the process group (which we need).
    """
    if hasattr(signal, 'CTRL_C_EVENT'):
        # windows. Need CTRL_C_EVENT to raise the signal in the whole process group
        os.kill(os.getpid(), signal.CTRL_C_EVENT)
    else:
        # unix.
        pgid = os.getpgid(os.getpid())
        if pgid == 1:
            os.kill(os.getpid(), signal.SIGINT)
        else:
            os.killpg(os.getpgid(os.getpid()), signal.SIGINT)
wsgi.py 文件源码 项目:bilean 作者: openstack 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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')
wsgi.py 文件源码 项目:bilean 作者: openstack 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def reload(self):
        """Reload and re-apply configuration settings.

        Existing child processes are sent a SIGHUP signal and will exit after
        completing existing requests. New child processes, which will have the
        updated configuration, are spawned. This allows preventing
        interruption to the service.
        """
        def _has_changed(old, new, param):
            old = old.get(param)
            new = getattr(new, param)
            return (new != old)

        old_conf = self.stash_conf_values()
        has_changed = functools.partial(_has_changed, old_conf, self.conf)
        cfg.CONF.reload_config_files()
        os.killpg(self.pgid, signal.SIGHUP)
        self.stale_children = self.children
        self.children = set()

        # Ensure any logging config changes are picked up
        logging.setup(cfg.CONF, self.name)

        self.configure_socket(old_conf, has_changed)
        self.start_wsgi()
process.py 文件源码 项目:clusterfuzz-tools 作者: google 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def kill_last_pid():
  """Kill the last pid. See:
    https://github.com/google/clusterfuzz-tools/issues/299"""
  # We have found that, when invoking `sv stop python-daemon`, the process
  # in call() isn't killed. Therefore, we need to explicitly kill it and
  # all of its children.
  #
  # We hope that pid recycling is not that fast.
  try:
    with open(LAST_PID_FILE, 'r') as f:
      pid = int(f.read().strip())
      os.killpg(pid, signal.SIGKILL)
  except:  # pylint: disable=bare-except
    pass
  finally:
    try:
      os.remove(LAST_PID_FILE)
    except:  # pylint: disable=bare-except
      pass
common.py 文件源码 项目:clusterfuzz-tools 作者: google 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def kill(proc):
  """Kill a process multiple times.
    See: https://github.com/google/clusterfuzz-tools/pull/301"""
  try:
    for sig in [signal.SIGTERM, signal.SIGTERM,
                signal.SIGKILL, signal.SIGKILL]:
      logger.debug('Killing pid=%s with %s', proc.pid, sig)
      # Process leader id is the group id.
      os.killpg(proc.pid, sig)

      # Wait for any shutdown stacktrace to be dumped.
      time.sleep(3)

    raise error.KillProcessFailedError(proc.args, proc.pid)
  except OSError as e:
    if e.errno != NO_SUCH_PROCESS_ERRNO:
      raise
zynthian_engine.py 文件源码 项目:zynthian-ui 作者: zynthian 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def stop(self, wait=0.2):
        if self.proc:
            self.start_loading()
            try:
                logging.info("Stoping Engine " + self.name)
                pid=self.proc.pid
                #self.proc.stdout.close()
                #self.proc.stdin.close()
                #os.killpg(os.getpgid(pid), signal.SIGTERM)
                self.proc.terminate()
                if wait>0: sleep(wait)
                try:
                    self.proc.kill()
                    os.killpg(pid, signal.SIGKILL)
                except:
                    pass
            except Exception as err:
                logging.error("Can't stop engine %s => %s" % (self.name,err))
            self.proc=None
            self.stop_loading()
rism3d_pc.py 文件源码 项目:PC_plus 作者: MTS-Strathclyde 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def run_and_timeout(self):
        """Run subprocess

        Returns
        -------
        out : int
            Exit code of subprocess
        """
        self.start()
        self.join(self.timeout)

        if self.is_alive():
            os.killpg(self.p.pid, signal.SIGTERM)
            self.join()
            print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
            print(self.cmd)
            print('Has run out of time')
            print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
            raise RuntimeError("3D-RISM calc didn't finish in time.")
resume.py 文件源码 项目:tensorflow-layer-library 作者: bioinf-jku 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
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)
ping_object.py 文件源码 项目:any_ping_indicator 作者: leggedrobotics 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def stop(self):
        """Join the thread. Kill the running subprocess and interrupt the sleeping.
        :return:
        """
        self.is_running = False
        if self.thread is not None:
            # kill subprocess
            if self.process is not None:
                print("kill process")
                try:
                    os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
                except ProcessLookupError as e:
                    print(e)
            # interrupt sleeping
            self.stop_event.set()
            self.stop_event = None
            self.thread.join()
            self.thread = None
start_controller.py 文件源码 项目:parsl 作者: Parsl 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def close(self):
        ''' Terminate the controller process and it's child processes.

        Args:
              - None
        '''
        if self.reuse :
            logger.debug("Ipcontroller not shutting down: reuse enabled")
            return

        try:
            pgid = os.getpgid(self.proc.pid)
            status = os.killpg(pgid, signal.SIGTERM)
            time.sleep(0.2)
            os.killpg(pgid, signal.SIGKILL)
            try:
                self.proc.wait(timeout=1)
                x = self.proc.returncode
                logger.debug("Controller exited with {0}".format(x))
            except subprocess.TimeoutExpired :
                logger.warn("Ipcontroller process:{0} cleanup failed. May require manual cleanup".format(self.proc.pid))

        except Exception as e:
            logger.warn("Failed to kill the ipcontroller process[{0}]: {1}".format(self.proc.pid,
                                                                                   e))
__init__.py 文件源码 项目:siphon-cli 作者: getsiphon 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def stop(self):
        if self.process is None:
            return
        try:
            os.killpg(os.getpgid(self.process.pid), self.stop_signal)
        except OSError:
            # Process is already gone
            pass
        else:
            kill_time = time.time() + self.kill_after
            while time.time() < kill_time:
                if self.process.poll() is not None:
                    break
                time.sleep(0.25)
            else:
                try:
                    os.killpg(os.getpgid(self.process.pid), 9)
                except OSError:
                    # Process is already gone
                    pass
        self.process = None
receiver.py 文件源码 项目:ssh 作者: GDGVIT 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def econn():
    global conn, address, ssh_server
    ssh_server_check()
    ssh_server = subprocess.Popen(['sudo', '/usr/sbin/sshd', '-p', '2222', '-f', '/etc/ssh/shareinator', '-D'],
                                  preexec_fn=os.setsid)
    socket_create()
    socket_bind('', 9999)
    # print('Waiting...')
    try:
        conn, address = s.accept()
        return str(conn.recv(1024), encoding='utf-8')
    except (KeyboardInterrupt, EOFError):
        # print(' Keyboard Interrupt')
        os.killpg(os.getpgid(ssh_server.pid), signal.SIGTERM)

        return 0
        # print(str(conn.recv(1024), encoding='utf-8'))
        # confirmation = input("Do you want to accept the connection? ")
emulator.py 文件源码 项目:anom-py 作者: Bogdanp 项目源码 文件源码 阅读 107 收藏 0 点赞 0 评论 0
def stop(self):
        """Stop the emulator process.

        Returns:
          int: The process return code or None if the process isn't
          currently running.
        """
        if self._proc is not None:
            if self._proc.poll() is None:
                try:
                    os.killpg(self._proc.pid, signal.SIGTERM)
                    _, returncode = os.waitpid(self._proc.pid, 0)
                    self._logger.debug("Emulator process exited with code %d.", returncode)
                    return returncode
                except ChildProcessError:  # pragma: no cover
                    return self._proc.returncode
            return self._proc.returncode  # pragma: no cover
        return None  # pragma: no cover
killableprocess.py 文件源码 项目:jupyter-powershell 作者: vors 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def kill(self, group=True):
        """Kill the process. If group=True, all sub-processes will also be killed."""
        self.kill_called = True
        if mswindows:
            if group and self._job:
                winprocess.TerminateJobObject(self._job, 127)
            else:
                try:
                    winprocess.TerminateProcess(self._handle, 127)
                except:
                    # TODO: better error handling here
                    pass
            self.returncode = 127
        else:
            if group:
                try:
                    os.killpg(self.pid, signal.SIGKILL)
                except: pass
            else:
                os.kill(self.pid, signal.SIGKILL)
            super(Popen, self).kill()
            self.returncode = -9
trivup.py 文件源码 项目:trivup 作者: edenhill 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def stop (self, wait_term=True, force=False):
        if self.state != 'started':
            return

        self.dbg('Stopping')
        try:
            os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM)
        except OSError as e:
            self.log('killpg() failed: already dead? (%s): ignoring' % str(e))
            wait_term = False

        if wait_term:
            # Wait for termination
            self.wait_stopped(timeout=10, force=force)
        else:
            self.state = 'stopped'

        self.dbg('now %s, runtime %ds' % (self.state, self.runtime()))

        self.stdout_fd.close()
        self.stderr_fd.close()
        self.proc = None
neo4j_helper.py 文件源码 项目:ccdetection 作者: tommiu 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def startConsole(self, path, port=1):
        """
        Import the php file/project AST from 'path' into the neo4j 
        database and start the neo4j console, using the 'SPAWN_SCRIPT' file.
        """
        process = subprocess.call(
                            [
                    Configurator.getPath(Configurator.KEY_SPAWN_SCRIPT),
                    Configurator.getPath(Configurator.KEY_BASE_DIR) + "/config", 
                    path, str(port), 
                    "%d%s" % (Neo4jHelper.HEAP_SIZE[0], 
                              Neo4jHelper.HEAP_SIZE[1])
                    ],
                            preexec_fn=os.setsid
                            )

        def signalHandler(signalnum, handler):
            os.killpg(process.pid, signal.SIGINT)

        signal.signal(signal.SIGINT, signalHandler)
        signal.signal(signal.SIGTERM, signalHandler)
timeshare.py 文件源码 项目:TikZ 作者: ellisk42 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def execute(self,dt):
        if self.finished: return "finished"
        if not self.running:
            self.process = Process(target = executeInProcessGroup, args = (self,))
            self.process.start()
            print "timeshare child PID:",self.process.pid
            os.setpgid(self.process.pid,self.process.pid)
            print "timeshare process group",os.getpgid(self.process.pid)
            assert os.getpgid(self.process.pid) == self.process.pid
            print "my process group",os.getpgrp(),"which should be",os.getpgid(0)
            assert os.getpgid(self.process.pid) != os.getpgid(0)
            self.running = True
        else:
            os.killpg(self.process.pid, signal.SIGCONT)

        self.process.join(dt)
        if self.process.is_alive():
            os.killpg(self.process.pid, signal.SIGSTOP)
            return "still running"
        else:
            self.finished = True
            return self.q.get()
CommandRunner.py 文件源码 项目:PBSuite 作者: dbrowneup 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def exe(cmd, timeout=-1):
    """
    Executes a command through the shell.
    timeout in minutes! so 1440 mean is 24 hours.
    -1 means never
    """
    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, \
                            stderr=subprocess.STDOUT, close_fds=True,\
                            preexec_fn=os.setsid)
    signal.signal(signal.SIGALRM, alarm_handler)
    if timeout > 0:
        signal.alarm(int(timeout*60))  
    try:
        stdoutVal, stderrVal =  proc.communicate()
        signal.alarm(0)  # reset the alarm
    except Alarm:
        logging.error(("Command was taking too long. "
                       "Automatic Timeout Initiated after %d" % (timeout)))
        os.killpg(proc.pid, signal.SIGTERM)
        proc.kill()
        return 214,None,None

    retCode = proc.returncode
    return retCode,stdoutVal,stderrVal
__init__.py 文件源码 项目:hacker-scripts 作者: restran 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def stop(self):
        if self.process is None:
            return
        try:
            os.killpg(os.getpgid(self.process.pid), self.stop_signal)
        except OSError:
            # Process is already gone
            pass
        else:
            kill_time = time.time() + self.kill_after
            while time.time() < kill_time:
                if self.process.poll() is not None:
                    break
                time.sleep(0.25)
            else:
                try:
                    os.killpg(os.getpgid(self.process.pid), 9)
                except OSError:
                    # Process is already gone
                    pass
        self.process = None
plugin.py 文件源码 项目:certbot-external-auth 作者: EnigmaBridge 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _cleanup_http01_challenge(self, achall):
        # pylint: disable=missing-docstring,unused-argument
        if self.conf("test-mode"):
            assert self._httpd is not None, (
                "cleanup() must be called after perform()")
            if self._httpd.poll() is None:
                logger.debug("Terminating manual command process")
                os.killpg(self._httpd.pid, signal.SIGTERM)
            else:
                logger.debug("Manual command process already terminated "
                             "with %s code", self._httpd.returncode)
            shutil.rmtree(self._root)

    #
    # Installer section
    #
test_twisted.py 文件源码 项目:maas 作者: maas 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test__terminates_with_kill_and_killpg(self):
        protocol = SignalPrinterProtocol()
        process = yield self.startSignalPrinter(protocol, pgrp=True)
        # Capture the pid now; it gets cleared when the process exits.
        pid = process.pid
        # Terminate and wait for it to exit.
        self.terminateSignalPrinter(process, protocol)
        yield protocol.done.addErrback(suppress, ProcessTerminated)
        # os.kill was called once then os.killpg was called twice because the
        # subprocess made itself a process group leader.
        self.assertThat(
            twisted_module._os_kill, MockCallsMatch(
                mock.call(pid, signal.SIGTERM),
            ))
        self.assertThat(
            twisted_module._os_killpg, MockCallsMatch(
                mock.call(pid, signal.SIGQUIT),
                mock.call(pid, signal.SIGKILL),
            ))
run_one_injection.py 文件源码 项目:sassifi 作者: NVlabs 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def is_timeout(app, pr): # check if the process is active every 'factor' sec for timeout threshold 
    factor = 0.25
    retcode = None
    tt = cp.TIMEOUT_THRESHOLD * sp.apps[app][2] # sp.apps[app][2] = expected runtime
    if tt < 10: tt = 10

    to_th = tt / factor
    while to_th > 0:
        retcode = pr.poll()
        if retcode is not None:
            break
        to_th -= 1
        time.sleep(factor)

    if to_th == 0:
        os.killpg(pr.pid, signal.SIGKILL) # pr.kill()
        print "timeout"
        return [True, pr.poll()]
    else:
        return [False, retcode]

###############################################################################
# Run the actual injection run 
###############################################################################
senti_client.py 文件源码 项目:SentiStrength_for_python 作者: bobvdvelde 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def run_server(self, language):
        if language!=self.language and self.sentistrength:
            logger.warning("wrong language running, trying to switch")
            os.killpg(self.sentistrength.pid,15)
            time.sleep(1)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('0.0.0.0',self.port))
        except ConnectionRefusedError:
            try:
                logger.info("server not found, trying to launch server")
                self.sentistrength = subprocess.Popen(["java -jar SentiStrengthCom.jar sentidata ./%s/ listen 8181 trinary" %language], shell=True, preexec_fn=os.setsid)
                time.sleep(1)
                sock.connect(('0.0.0.0',self.port))
                self.language = language
            except:
                raise Exception("unable to start server, is there a process already running? ")
        return sock
senti_client.py 文件源码 项目:SentiStrength_for_python 作者: bobvdvelde 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def stop_server(self, port=None,pid=None):
        if port and pid:
            logger.warn("this function requires EITHER a port OR a pid, ignores pid if both")
        if port:
            instance = [instance for instance in self.instances if instance['port']==port]
        elif pid:
            instance = [instance for instance in self.instances if instance['pid']==pid]
        else:
            instance = self.instances

        if not instance:
            logger.warn("Instance not found!")
            return False
        instance = instance[0]

        os.killpg(instance['instance'].pid, 15)
        time.sleep(1)
        if not check_exists(instance['port']):
            logger.info('Stopped {pid} instance at port {port}'.format(**instance))
            self.instances.remove(instance)
            return True
        else:
            logger.warn('Unable to stop {pid} instance running at {port}!!'.format(**instance))
            return False


问题


面经


文章

微信
公众号

扫码关注公众号