コード例 #1
0
ファイル: gs.py プロジェクト: spaceconcordia/ground-control
def is_subprocess_running(subprocess):
    if subprocess is None:
        return False
    if subprocess.poll() is None:
        return True
    else:
        return False
コード例 #2
0
def disconnect_device():
    global connected, processes
    data = 'CONNECTED'
    errors = ''
    try:
        if not connected:
            raise Exception('You are not connected.')
        client_socket = sockets[0]
        client_socket.send("X")
        time.sleep(5)
        client_socket.shutdown(socket.SHUT_RDWR)
        while 1:
            try:
                byte = client_socket.recv(1)
                if "" == byte:
                    break
            except:
                break
        for subprocess in processes:
            if subprocess.poll() is None:
                subprocess.kill()
        del processes[:]
        client_socket.close()
        del sockets[:]
        connected = False
        data = "DISCONNECTED"
    except Exception as f:
        errors = str(f)
    output = OrderedDict([('command', 'disconnect'), ('data', data),
                          ('errors', errors)])
    return json.dumps(output)
コード例 #3
0
ファイル: killableprocess.py プロジェクト: AMITHSHAH/Test
    def wait(self, timeout=None, group=True):
        """Wait for the process to terminate. Returns returncode attribute.
        If timeout seconds are reached and the process has not terminated,
        it will be forcefully killed. If timeout is -1, wait will not
        time out."""
        
        if timeout is not None:
            timeout = timeout * 1000

        if self.returncode is not None:
            return self.returncode

        starttime = datetime.datetime.now()

        if mswindows:
            if timeout is None:
                timeout = -1
            rc = winprocess.WaitForSingleObject(self._handle, timeout)
            
            if rc != winprocess.WAIT_TIMEOUT: 
                while (starttime - datetime.datetime.now()).microseconds < timeout or ( winprocess.QueryInformationJobObject(self._job, 8)['BasicInfo']['ActiveProcesses'] > 0 ):
                    time.sleep(.5)            
            
            if (starttime - datetime.datetime.now()).microseconds > timeout:
                self.kill(group)
            else:
                self.returncode = winprocess.GetExitCodeProcess(self._handle)
        else:
            if sys.platform == 'linux2' or sys.platform == 'cygwin':
                def group_wait():
                    os.waitpid(self.pid, 0)
                    return self.returncode
            elif sys.platform == 'darwin':
                def group_wait():
                    try:
                        while 1:
                            os.killpg(self.pid, signal.SIG_DFL)
                            time.sleep(.5)
                    except exceptions.OSError:
                        return self.returncode

            if timeout is None:
                if group is True:
                    return group_wait()
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            while (starttime - datetime.datetime.now()).microseconds < timeout or ( returncode is False ):
                if group is True:
                    return group_wait()
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
            return self.returncode        
                
        return self.returncode
コード例 #4
0
    def wait(self, timeout=None, group=True):
        """Wait for the process to terminate. Returns returncode attribute.
        If timeout seconds are reached and the process has not terminated,
        it will be forcefully killed. If timeout is -1, wait will not
        time out."""
        
        if timeout is not None:
            timeout = timeout * 1000

        if self.returncode is not None:
            return self.returncode

        starttime = datetime.datetime.now()

        if mswindows:
            if timeout is None:
                timeout = -1
            rc = winprocess.WaitForSingleObject(self._handle, timeout)
            
            if rc != winprocess.WAIT_TIMEOUT: 
                while (starttime - datetime.datetime.now()).microseconds < timeout or ( winprocess.QueryInformationJobObject(self._job, 8)['BasicInfo']['ActiveProcesses'] > 0 ):
                    time.sleep(.5)            
            
            if (starttime - datetime.datetime.now()).microseconds > timeout:
                self.kill(group)
            else:
                self.returncode = winprocess.GetExitCodeProcess(self._handle)
        else:
            if sys.platform == 'linux2' or sys.platform == 'cygwin':
                def group_wait():
                    os.waitpid(self.pid, 0)
                    return self.returncode
            elif sys.platform == 'darwin':
                def group_wait():
                    try:
                        while 1:
                            os.killpg(self.pid, signal.SIG_DFL)
                            time.sleep(.5)
                    except exceptions.OSError:
                        return self.returncode
                        
            if timeout is None:
                if group is True:
                    return group_wait()
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            while (starttime - datetime.datetime.now()).microseconds < timeout or ( returncode is False ):
                if group is True:
                    return group_wait()
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
            return self.returncode        
                
        return self.returncode
コード例 #5
0
def process_wait(subprocess, timeout):
	start = time.time()
	
	while (time.time() - start) < timeout:
		if subprocess.poll() is not None:
			return True
	
	return False
コード例 #6
0
def stop_etcd(subprocess, data_dir):
    if subprocess and subprocess.poll() is None:
        log.info(f"stopping etcd server")
        subprocess.terminate()
        subprocess.wait()

    log.info(f"deleting etcd data dir: {data_dir}")
    shutil.rmtree(data_dir, ignore_errors=True)
コード例 #7
0
ファイル: __init__.py プロジェクト: lalmeras/clickable
def _subprocess_run(subprocess,
                    return_stdout=True,
                    return_stderr=True,
                    write_stdout=sys.stdout,
                    write_stderr=sys.stdout,
                    oneline_mode=True,
                    oneline_timing=.01):
    if _terminal_width() is None and oneline_mode:
        logger.warn('online_mode disabled as terminal width is not found')
        oneline_mode = False
    if oneline_mode and write_stdout != write_stderr:
        raise Exception('write_stdout == write_stderr needed for oneline_mode')
    whole = ''
    curlines = {'stdout': None, 'stderr': None}
    waiting = []
    streams = {
        subprocess.stdout.fileno(): {
            'stream': 'stdout',
            'return': return_stdout,
            'write': write_stdout,
            'pipe': subprocess.stdout
        },
        subprocess.stderr.fileno(): {
            'stream': 'stderr',
            'return': return_stderr,
            'write': write_stderr,
            'pipe': subprocess.stderr
        },
    }
    while True:
        ret = select.select(streams.keys(), [], [])

        for fd in ret[0]:
            stream_desc = streams[fd]
            stream = streams[fd]['stream']
            output = stream_desc['pipe'].read().decode('utf-8')
            if stream_desc['return']:
                whole += output
            if not oneline_mode and stream_desc['write'] is not None:
                stream_desc['write'].write(output)
            if oneline_mode:
                _handle_oneline(waiting, curlines, stream, output)

            # in oneline_mode, output only one line by cycle
            if oneline_mode and len(waiting) > 0:
                _write_line(waiting.pop(0), write_stdout, oneline_timing)

        if oneline_mode:
            for w in waiting:
                _write_line(w, write_stdout, oneline_timing)
            _write_line('', write_stdout, oneline_timing)

        # end loop when program ends
        if subprocess.poll() is not None:
            break
    return whole
コード例 #8
0
    def _get_is_subprocess_running(self, subprocess: subprocess.Popen) -> bool:
        """It indicates whether simulator is running as subprocess.Popen or not.

        Args:
            subprocess (subprocess.Popen): Simulator subprocess running

        Returns:
            bool: Flag which indicates subprocess is running or not.
        """
        if subprocess.poll() is None:
            return True
        else:
            return False
コード例 #9
0
    def _flush_stderr(self, subprocess):
        ''' read and return stderr with minimal blocking '''

        poll_stderr = select.poll()
        poll_stderr.register(subprocess.stderr, select.POLLIN)
        stderr = ''

        while subprocess.poll() is None:
            if poll_stderr.poll(1):
                line = subprocess.stderr.readline()
                display.vvvv(u"stderr line: {0}".format(to_text(line)), host=self.host)
                stderr = stderr + line
            else:
                break

        return stderr
コード例 #10
0
def main():
    config = HarnessConfiguration.instance().read()

    if len(config.testables) != 1:
        config.errors.append(
            "Fatal Error: Exactly one testable may be passed.")
        return 1

    def id_generator(zsize, zchars):
        return "".join(random.choice(chars) for x in xrange(zsize))

    # Generate the words we will send to the testable
    words = [
        id_generator(8, string.ascii_lowercase) + "%0.2d" % i
        for i in range(0, 100)
    ]

    # Execute the testable
    subprocess.Popen([config.testables[0]],
                     stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)

    subprocess.stdin.write(words.join(" "))
    subprocess.stdin.close()

    # Grab all the output
    output = subprocess.stdout.readlines().join("\n")

    # Kill the process if it's still running
    if subprocess.poll() == None:
        subprocess.kill()

    testResults = []
    if "WordsExist" in config.tests:
        testResults.append(testWordsExist(output, words))
    if "LineLengths" in config.tests:
        testResults.append(testLineLengths(output))
    if "Paragraphs" in config.tests:
        testResults.append(testLineLengths(output))

    json.dump(testResults, sys.stdout)
    sys.stderr.write("\n")

    return 0
コード例 #11
0
 def __track_progress(self, subprocess):
     if not self.busy or subprocess is None:
         return -1
     buf = BytesIO()
     while subprocess.poll() is None:
         if self.installed:
             time.sleep(1)
             continue
         out = subprocess.stdout.read(1)
         buf.write(out)
         if out in (b'\r', b'\n'):
             for value in buf.getvalue().split():
                 if isinstance(value, bytes):
                     value = value.decode("utf-8")
                 if "%" in value:
                     self.progress = int(value.replace("%", ""))
             buf.truncate(0)
     self.installed = (not self.installed)
     self.busy = False
     self.progress = -1
コード例 #12
0
ファイル: plmanager.py プロジェクト: pombredanne/concoord
 def _waitforall(self, subprocesslist, timeout=300):
     start = time.time()
     while len(subprocesslist) > 0:
         todo = []
         for subprocess in subprocesslist:
             rtv = subprocess.poll()
             if rtv is None:
                 todo.append(subprocess)
             else:
                 self.procs.remove(subprocess)
         if len(todo) > 0:
             time.sleep(0.1)
             now = time.time()
             if now - start > timeout:
                 for p in todo:
                     os.kill(p.pid, signal.SIGKILL)
                     os.waitpid(p.pid, os.WNOHANG)
                     self.procs.remove(p)
                 return -len(todo)
         subprocesslist = todo
     return 0
コード例 #13
0
ファイル: MadLib.py プロジェクト: merepro/galah
def main():
    config = HarnessConfiguration.instance().read()

    if len(config.testables) != 1:
        config.errors.append("Fatal Error: Exactly one testable may be passed.")
        return 1

    def id_generator(zsize, zchars):
        return "".join(random.choice(chars) for x in xrange(zsize))

    # Generate the words we will send to the testable
    words = [id_generator(8, string.ascii_lowercase) + "%0.2d" % i for i in range(0, 100)]

    # Execute the testable
    subprocess.Popen([config.testables[0]], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    subprocess.stdin.write(words.join(" "))
    subprocess.stdin.close()

    # Grab all the output
    output = subprocess.stdout.readlines().join("\n")

    # Kill the process if it's still running
    if subprocess.poll() == None:
        subprocess.kill()

    testResults = []
    if "WordsExist" in config.tests:
        testResults.append(testWordsExist(output, words))
    if "LineLengths" in config.tests:
        testResults.append(testLineLengths(output))
    if "Paragraphs" in config.tests:
        testResults.append(testLineLengths(output))

    json.dump(testResults, sys.stdout)
    sys.stderr.write("\n")

    return 0
コード例 #14
0
class Popen(subprocess.Popen):
    kill_called = False
    if mswindows:

        def _execute_child(self, args, executable, preexec_fn, close_fds, cwd,
                           env, universal_newlines, startupinfo, creationflags,
                           shell, p2cread, p2cwrite, c2pread, c2pwrite,
                           errread, errwrite):
            if not isinstance(args, types.StringTypes):
                args = subprocess.list2cmdline(args)

            if startupinfo is None:
                startupinfo = winprocess.STARTUPINFO()

            if None not in (p2cread, c2pwrite, errwrite):
                startupinfo.dwFlags |= winprocess.STARTF_USESTDHANDLES

                startupinfo.hStdInput = int(p2cread)
                startupinfo.hStdOutput = int(c2pwrite)
                startupinfo.hStdError = int(errwrite)
            if shell:
                startupinfo.dwFlags |= winprocess.STARTF_USESHOWWINDOW
                startupinfo.wShowWindow = winprocess.SW_HIDE
                comspec = os.environ.get("COMSPEC", "cmd.exe")
                args = comspec + " /c " + args

            # We create a new job for this process, so that we can kill
            # the process and any sub-processes
            self._job = winprocess.CreateJobObject()

            creationflags |= winprocess.CREATE_SUSPENDED
            creationflags |= winprocess.CREATE_UNICODE_ENVIRONMENT

            hp, ht, pid, tid = winprocess.CreateProcess(
                executable,
                args,
                None,
                None,  # No special security
                1,  # Must inherit handles!
                creationflags,
                winprocess.EnvironmentBlock(env),
                cwd,
                startupinfo)

            self._child_created = True
            self._handle = hp
            self._thread = ht
            self.pid = pid
            self.tid = tid

            winprocess.AssignProcessToJobObject(self._job, hp)
            winprocess.ResumeThread(ht)

            if p2cread is not None:
                p2cread.Close()
            if c2pwrite is not None:
                c2pwrite.Close()
            if errwrite is not None:
                errwrite.Close()
            time.sleep(.1)

    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:
                winprocess.TerminateJobObject(self._job, 127)
            else:
                winprocess.TerminateProcess(self._handle, 127)
            self.returncode = 127
        else:
            if group:
                try:
                    os.killpg(self.pid, signal.SIGKILL)
                except:
                    pass
            else:
                os.kill(self.pid, signal.SIGKILL)
            self.returncode = -9

    def wait(self, timeout=None, group=True):
        """Wait for the process to terminate. Returns returncode attribute.
        If timeout seconds are reached and the process has not terminated,
        it will be forcefully killed. If timeout is -1, wait will not
        time out."""

        if timeout is not None:
            timeout = timeout * 1000

        if self.returncode is not None:
            return self.returncode

        starttime = datetime.datetime.now()

        if mswindows:
            if timeout is None:
                timeout = -1
            rc = winprocess.WaitForSingleObject(self._handle, timeout)

            if rc != winprocess.WAIT_TIMEOUT:
                while (starttime -
                       datetime.datetime.now()).microseconds < timeout or (
                           winprocess.QueryInformationJobObject(
                               self._job, 8)['BasicInfo']['ActiveProcesses'] >
                           0):
                    time.sleep(.5)

            if (starttime - datetime.datetime.now()).microseconds > timeout:
                self.kill(group)
            else:
                self.returncode = winprocess.GetExitCodeProcess(self._handle)
        else:
            if (sys.platform == 'linux2') or (sys.platform
                                              in ('sunos5', 'solaris')):

                def group_wait(timeout):
                    try:
                        os.waitpid(self.pid, 0)
                    except OSError, e:
                        pass  # If wait has already been called on this pid, bad things happen
                    return self.returncode
            elif sys.platform == 'darwin':

                def group_wait(timeout):
                    try:
                        count = 0
                        if timeout is None and self.kill_called:
                            timeout = 10  # Have to set some kind of timeout or else this could go on forever
                        if timeout is None:
                            while 1:
                                os.killpg(self.pid, signal.SIG_DFL)
                        while ((count * 2) <= timeout):
                            os.killpg(self.pid, signal.SIG_DFL)
                            time.sleep(.5)
                            count += .5
                    except exceptions.OSError:
                        return self.returncode

            if timeout is None:
                if group is True:
                    return group_wait(timeout)
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            while (starttime - datetime.datetime.now()
                   ).microseconds < timeout or (returncode is False):
                if group is True:
                    return group_wait(timeout)
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
            return self.returncode
コード例 #15
0
    def wait(self, timeout=None, group=True):
        """Wait for the process to terminate. Returns returncode attribute.
        If timeout seconds are reached and the process has not terminated,
        it will be forcefully killed. If timeout is -1, wait will not
        time out."""

        if timeout is not None:
            # timeout is now in milliseconds
            timeout = timeout * 1000

        if self.returncode is not None:
            return self.returncode

        starttime = datetime.datetime.now()

        if mswindows:
            if timeout is None:
                timeout = -1
            rc = winprocess.WaitForSingleObject(self._handle, timeout)

            if rc != winprocess.WAIT_TIMEOUT:
                def check():
                    now = datetime.datetime.now()
                    diff = now - starttime
                    if (diff.seconds * 1000 * 1000 + diff.microseconds) < (timeout * 1000):
                        if self._job:
                            if (winprocess.QueryInformationJobObject(self._job, 8)['BasicInfo']['ActiveProcesses'] > 0):
                                return True
                        else:
                            return True
                    return False
                while check():
                    time.sleep(.5)

            now = datetime.datetime.now()
            diff = now - starttime
            if (diff.seconds * 1000 * 1000 + diff.microseconds) > (timeout * 1000):
                self.kill(group)
            else:
                self.returncode = winprocess.GetExitCodeProcess(self._handle)
        else:
            if (sys.platform == 'linux2') or (sys.platform in ('sunos5', 'solaris')):
                def group_wait(timeout):
                    try:
                        os.waitpid(self.pid, 0)
                    except OSError as e:
                        pass # If wait has already been called on this pid, bad things happen
                    return self.returncode
            elif sys.platform == 'darwin':
                def group_wait(timeout):
                    try:
                        count = 0
                        if timeout is None and self.kill_called:
                            timeout = 10 # Have to set some kind of timeout or else this could go on forever
                        if timeout is None:
                            while 1:
                                os.killpg(self.pid, signal.SIG_DFL)
                        while ((count * 2) <= timeout):
                            os.killpg(self.pid, signal.SIG_DFL)
                            # count is increased by 500ms for every 0.5s of sleep
                            time.sleep(.5); count += 500
                    except OSError:
                        return self.returncode

            if timeout is None:
                if group is True:
                    return group_wait(timeout)
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            now = datetime.datetime.now()
            diff = now - starttime
            while (diff.seconds * 1000 * 1000 + diff.microseconds) < (timeout * 1000) and ( returncode is False ):
                if group is True:
                    return group_wait(timeout)
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
                now = datetime.datetime.now()
                diff = now - starttime
            return self.returncode

        return self.returncode
コード例 #16
0
ファイル: killableprocess.py プロジェクト: 1901/sublime_sync
    def wait(self, timeout=None, group=True):
        """Wait for the process to terminate. Returns returncode attribute.
        If timeout seconds are reached and the process has not terminated,
        it will be forcefully killed. If timeout is -1, wait will not
        time out."""

        if timeout is not None:
            # timeout is now in milliseconds
            timeout = timeout * 1000

        if self.returncode is not None:
            return self.returncode

        starttime = datetime.datetime.now()

        if mswindows:
            if timeout is None:
                timeout = -1
            rc = winprocess.WaitForSingleObject(self._handle, timeout)

            if rc != winprocess.WAIT_TIMEOUT:
                def check():
                    now = datetime.datetime.now()
                    diff = now - starttime
                    if (diff.seconds * 1000 * 1000 + diff.microseconds) < (timeout * 1000):
                        if self._job:
                            if (winprocess.QueryInformationJobObject(self._job, 8)['BasicInfo']['ActiveProcesses'] > 0):
                                return True
                        else:
                            return True
                    return False
                while check():
                    time.sleep(.5)

            now = datetime.datetime.now()
            diff = now - starttime
            if (diff.seconds * 1000 * 1000 + diff.microseconds) > (timeout * 1000):
                self.kill(group)
            else:
                self.returncode = winprocess.GetExitCodeProcess(self._handle)
        else:
            if (sys.platform == 'linux2') or (sys.platform in ('sunos5', 'solaris')):
                def group_wait(timeout):
                    try:
                        os.waitpid(self.pid, 0)
                    except OSError as e:
                        pass # If wait has already been called on this pid, bad things happen
                    return self.returncode
            elif sys.platform == 'darwin':
                def group_wait(timeout):
                    try:
                        count = 0
                        if timeout is None and self.kill_called:
                            timeout = 10 # Have to set some kind of timeout or else this could go on forever
                        if timeout is None:
                            while 1:
                                os.killpg(self.pid, signal.SIG_DFL)
                        while ((count * 2) <= timeout):
                            os.killpg(self.pid, signal.SIG_DFL)
                            # count is increased by 500ms for every 0.5s of sleep
                            time.sleep(.5); count += 500
                    except OSError:
                        return self.returncode

            if timeout is None:
                if group is True:
                    return group_wait(timeout)
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            now = datetime.datetime.now()
            diff = now - starttime
            while (diff.seconds * 1000 * 1000 + diff.microseconds) < (timeout * 1000) and ( returncode is False ):
                if group is True:
                    return group_wait(timeout)
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
                now = datetime.datetime.now()
                diff = now - starttime
            return self.returncode

        return self.returncode
コード例 #17
0
                if group is True:
                    return group_wait(timeout)
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            now = datetime.datetime.now()
            diff = now - starttime
            while (diff.seconds * 1000 * 1000 + diff.microseconds) < (
                    timeout * 1000) and (returncode is False):
                if group is True:
                    return group_wait(timeout)
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
                now = datetime.datetime.now()
                diff = now - starttime
            return self.returncode

        return self.returncode

    # We get random maxint errors from subprocesses __del__
    __del__ = lambda self: None


def setpgid_preexec_fn():
    os.setpgid(0, 0)
コード例 #18
0
def _subprocess_run(subprocess,
                    return_stdout=True,
                    return_stderr=True,
                    write_stdout=sys.stderr,
                    write_stderr=sys.stderr,
                    oneline_mode=True,
                    oneline_timing=.01):
    """
    Launch a command. Output handling behavior is controlled by oneline_mode.
    """
    if _terminal_width() is None and oneline_mode:
        clog.stderr.warn('online_mode disabled as terminal width is not found')
        oneline_mode = False
    if oneline_mode and write_stdout != write_stderr:
        raise Exception('write_stdout == write_stderr needed for oneline_mode')
    whole = ''
    curlines = {'stdout': None, 'stderr': None}
    waiting = []
    streams = {
        subprocess.stdout.fileno(): {
            'stream': 'stdout',
            'return': return_stdout,
            'write': write_stdout,
            'pipe': subprocess.stdout
        },
        subprocess.stderr.fileno(): {
            'stream': 'stderr',
            'return': return_stderr,
            'write': write_stderr,
            'pipe': subprocess.stderr
        },
    }
    while True:
        # wait for stream output
        ret = select.select(streams.keys(), [], [])

        for fd in ret[0]:
            stream_desc = streams[fd]
            stream = streams[fd]['stream']
            # read stream
            output = stream_desc['pipe'].read()
            # store output if *return*
            if stream_desc['return']:
                whole += output
            if not oneline_mode and stream_desc['write'] is not None:
                # directly write to output if *write*
                stream_desc['write'].write(output)
            if oneline_mode:
                # write in one-line mode
                _handle_oneline(waiting, curlines, stream, output)

            # in oneline_mode, output only one line by cycle
            if oneline_mode and len(waiting) > 0:
                _write_line(waiting.pop(0), write_stdout, oneline_timing)

        if oneline_mode:
            # process remaining lines
            for w in waiting:
                _write_line(w, write_stdout, oneline_timing)
            _write_line('', write_stdout, oneline_timing)

        # end loop when program ends
        if subprocess.poll() is not None:
            break
    return whole
コード例 #19
0
ファイル: system_test.py プロジェクト: niclabs/tchsm-libdtc
def terminate_subprocess(subprocess):
	if subprocess.poll() is None:
		subprocess.terminate()
コード例 #20
0
def wait(subprocess):
    while subprocess.poll() == None:
        time.sleep(2)
コード例 #21
0
 def _get_is_subprocess_running(self, subprocess):
     if subprocess.poll() is None:
         return True
     else:
         return False
コード例 #22
0
class Popen(subprocess.Popen):
    kill_called = False
    if mswindows:

        def _execute_child(self, args, executable, preexec_fn, close_fds, cwd,
                           env, universal_newlines, startupinfo, creationflags,
                           shell, p2cread, p2cwrite, c2pread, c2pwrite,
                           errread, errwrite):
            if not isinstance(args, types.StringTypes):
                args = subprocess.list2cmdline(args)

            # Always or in the create new process group
            creationflags |= winprocess.CREATE_NEW_PROCESS_GROUP

            if startupinfo is None:
                startupinfo = winprocess.STARTUPINFO()

            if None not in (p2cread, c2pwrite, errwrite):
                startupinfo.dwFlags |= winprocess.STARTF_USESTDHANDLES

                startupinfo.hStdInput = int(p2cread)
                startupinfo.hStdOutput = int(c2pwrite)
                startupinfo.hStdError = int(errwrite)
            if shell:
                startupinfo.dwFlags |= winprocess.STARTF_USESHOWWINDOW
                startupinfo.wShowWindow = winprocess.SW_HIDE
                comspec = os.environ.get("COMSPEC", "cmd.exe")
                args = comspec + " /c " + args

            # determine if we can create create a job
            canCreateJob = winprocess.CanCreateJobObject()

            # set process creation flags
            creationflags |= winprocess.CREATE_SUSPENDED
            creationflags |= winprocess.CREATE_UNICODE_ENVIRONMENT
            if canCreateJob:
                # Uncomment this line below to discover very useful things about your environment
                #print "++++ killableprocess: releng twistd patch not applied, we can create job objects"
                creationflags |= winprocess.CREATE_BREAKAWAY_FROM_JOB

            # create the process
            hp, ht, pid, tid = winprocess.CreateProcess(
                executable,
                args,
                None,
                None,  # No special security
                1,  # Must inherit handles!
                creationflags,
                winprocess.EnvironmentBlock(env),
                cwd,
                startupinfo)
            self._child_created = True
            self._handle = hp
            self._thread = ht
            self.pid = pid
            self.tid = tid

            if canCreateJob:
                # We create a new job for this process, so that we can kill
                # the process and any sub-processes
                self._job = winprocess.CreateJobObject()
                winprocess.AssignProcessToJobObject(self._job, int(hp))
            else:
                self._job = None

            winprocess.ResumeThread(int(ht))
            ht.Close()

            if p2cread is not None:
                p2cread.Close()
            if c2pwrite is not None:
                c2pwrite.Close()
            if errwrite is not None:
                errwrite.Close()
            time.sleep(.1)

    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:
                winprocess.TerminateProcess(self._handle, 127)
            self.returncode = 127
        else:
            if group:
                try:
                    os.killpg(self.pid, signal.SIGKILL)
                except:
                    pass
            else:
                os.kill(self.pid, signal.SIGKILL)
            self.returncode = -9

    def wait(self, timeout=None, group=True):
        """Wait for the process to terminate. Returns returncode attribute.
        If timeout seconds are reached and the process has not terminated,
        it will be forcefully killed. If timeout is -1, wait will not
        time out."""
        if timeout is not None:
            # timeout is now in milliseconds
            timeout = timeout * 1000

        starttime = datetime.datetime.now()

        if mswindows:
            if timeout is None:
                timeout = -1
            rc = winprocess.WaitForSingleObject(self._handle, timeout)

            if (rc == winprocess.WAIT_OBJECT_0
                    or rc == winprocess.WAIT_ABANDONED
                    or rc == winprocess.WAIT_FAILED):
                # Object has either signaled, or the API call has failed.  In
                # both cases we want to give the OS the benefit of the doubt
                # and supply a little time before we start shooting processes
                # with an M-16.

                # Returns 1 if running, 0 if not, -1 if timed out
                def check():
                    now = datetime.datetime.now()
                    diff = now - starttime
                    if (diff.seconds * 1000 * 1000 +
                            diff.microseconds) < (timeout * 1000):
                        if self._job:
                            if (winprocess.QueryInformationJobObject(
                                    self._job,
                                    8)['BasicInfo']['ActiveProcesses'] > 0):
                                # Job Object is still containing active processes
                                return 1
                        else:
                            # No job, we use GetExitCodeProcess, which will tell us if the process is still active
                            self.returncode = winprocess.GetExitCodeProcess(
                                self._handle)
                            if (self.returncode == STILL_ACTIVE):
                                # Process still active, continue waiting
                                return 1
                        # Process not active, return 0
                        return 0
                    else:
                        # Timed out, return -1
                        return -1

                notdone = check()
                while notdone == 1:
                    time.sleep(.5)
                    notdone = check()

                if notdone == -1:
                    # Then check timed out, we have a hung process, attempt
                    # last ditch kill with explosives
                    self.kill(group)

            else:
                # In this case waitforsingleobject timed out.  We have to
                # take the process behind the woodshed and shoot it.
                self.kill(group)

        else:
            if sys.platform in ('linux2', 'sunos5', 'solaris') \
                    or sys.platform.startswith('freebsd'):

                def group_wait(timeout):
                    try:
                        os.waitpid(self.pid, 0)
                    except OSError, e:
                        pass  # If wait has already been called on this pid, bad things happen
                    return self.returncode
            elif sys.platform == 'darwin':

                def group_wait(timeout):
                    try:
                        count = 0
                        if timeout is None and self.kill_called:
                            timeout = 10  # Have to set some kind of timeout or else this could go on forever
                        if timeout is None:
                            while 1:
                                os.killpg(self.pid, signal.SIG_DFL)
                        while ((count * 2) <= timeout):
                            os.killpg(self.pid, signal.SIG_DFL)
                            # count is increased by 500ms for every 0.5s of sleep
                            time.sleep(.5)
                            count += 500
                    except exceptions.OSError:
                        return self.returncode

            if timeout is None:
                if group is True:
                    return group_wait(timeout)
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            now = datetime.datetime.now()
            diff = now - starttime
            while (diff.seconds * 1000 * 1000 + diff.microseconds) < (
                    timeout * 1000) and (returncode is False):
                if group is True:
                    return group_wait(timeout)
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
                now = datetime.datetime.now()
                diff = now - starttime
            return self.returncode
コード例 #23
0
            if timeout is None:
                if group is True:
                    return group_wait(timeout)
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            now = datetime.datetime.now()
            diff = now - starttime
            while (diff.seconds * 1000 * 1000 + diff.microseconds) < (timeout * 1000) and ( returncode is False ):
                if group is True:
                    return group_wait(timeout)
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
                now = datetime.datetime.now()
                diff = now - starttime
            return self.returncode
                
        return self.returncode
    # We get random maxint errors from subprocesses __del__
    __del__ = lambda self: None        
        
def setpgid_preexec_fn():
    os.setpgid(0, 0)
        
def runCommand(cmd, **kwargs):
    if sys.platform != "win32":