示例#1
0
def kill( pid, signal = 15, timeout = 0 ):
	'''kills the process specified by pid that may be a process id or a
	Child object. The process is killed with the provided signal (by
	default 15). If the process is not dead after <timeout> seconds the
	function exist anyway'''
	# a dispatcher function required to activate the minimal timeout
	def fake_dispatcher(): return True
	notifier.dispatcher_add( fake_dispatcher )

	if isinstance( pid, Child ):
		if pid.pid:
			pid = pid.pid
		else:
			return pid.exitcode

	os.kill( pid, signal )
	countdown = CountDown( timeout )
	while countdown():
		dead_pid, sts = os.waitpid( pid, os.WNOHANG )
		if dead_pid == pid:
			break
		notifier.step()
	else:
		# remove dispatcher function
		notifier.dispatcher_remove( fake_dispatcher )
		return None

	# remove dispatcher function
	notifier.dispatcher_remove( fake_dispatcher )

	if os.WIFSIGNALED( sts ):
		return -os.WTERMSIG( sts )

	return os.WEXITSTATUS( sts )
示例#2
0
def runit():
    global proc
    print "runit ...",
    proc = notifier.popen.Process("/bin/sleep 5")
    proc = notifier.popen.Process("/bin/ls -ltr")
    proc.signal_connect("stdout", stdout)
    proc.signal_connect("stderr", stderr)
    proc.signal_connect("killed", died)
    proc.start()
    while True:
        if proc.is_alive():
            notifier.step()
        else:
            break
示例#3
0
def runit():
	global proc
	print 'runit ...',
	proc = notifier.popen.Process( '/bin/sleep 5' )
	proc = notifier.popen.Process( '/bin/ls -ltr' )
	proc.signal_connect( 'stdout', stdout )
	proc.signal_connect( 'stderr', stderr )
	proc.signal_connect( 'killed', died )
	proc.start()
	while True:
		if proc.is_alive():
			notifier.step()
		else:
			break
示例#4
0
def run(command, timeout=0, stdout=True, stderr=True, shell=True):
    '''Runs a child process with the <command> and waits <timeout>
	seconds for its termination. If <stdout> is True the standard output
	is written to a temporary file. The same can be done for the standard
	error output with the argument <stderr>. If <shell> is True the
	command is passed to a shell. The return value is a Child
	object. The member variable <pid> is set if the process is still
	running after <timeout> seconds otherwise <exitcode> is set.'''

    # a dispatcher function required to activate the minimal timeout
    def fake_dispatcher():
        return True

    notifier.dispatcher_add(fake_dispatcher)

    countdown = CountDown(timeout)
    out = err = None
    if stdout:
        out = tempfile.NamedTemporaryFile()
    if stderr:
        err = tempfile.NamedTemporaryFile()

    if isinstance(command, basestring):
        command = shlex.split(command)
    child = subprocess.Popen(command, shell=shell, stdout=out, stderr=err)

    while countdown():
        exitcode = child.poll()
        if exitcode != None:
            break
        notifier.step()

    # remove dispatcher function
    notifier.dispatcher_remove(fake_dispatcher)

    # prepare return code
    ret = Child(stdout=out, stderr=err)
    if child.returncode == None:
        ret.pid = child.pid
    else:
        # move to beginning of files
        if out:
            out.seek(0)
        if err:
            err.seek(0)
        ret.exitcode = child.returncode

    return ret
示例#5
0
def run( command, timeout = 0, stdout = True, stderr = True, shell = True ):
	'''Runs a child process with the <command> and waits <timeout>
	seconds for its termination. If <stdout> is True the standard output
	is written to a temporary file. The same can be done for the standard
	error output with the argument <stderr>. If <shell> is True the
	command is passed to a shell. The return value is a Child
	object. The member variable <pid> is set if the process is still
	running after <timeout> seconds otherwise <exitcode> is set.'''
	# a dispatcher function required to activate the minimal timeout
	def fake_dispatcher(): return True
	notifier.dispatcher_add( fake_dispatcher )

	countdown = CountDown( timeout )
	out = err = None
	if stdout:
		out = tempfile.NamedTemporaryFile()
	if stderr:
		err = tempfile.NamedTemporaryFile()

	if isinstance( command, basestring ):
		command = shlex.split( command )
	child = subprocess.Popen( command, shell = shell, stdout = out, stderr = err )

	while countdown():
		exitcode = child.poll()
		if exitcode != None:
			break
		notifier.step()

	# remove dispatcher function
	notifier.dispatcher_remove( fake_dispatcher )

	# prepare return code
	ret = Child( stdout = out, stderr = err )
	if child.returncode == None:
		ret.pid = child.pid
	else:
		# move to beginning of files
		if out:
			out.seek( 0 )
		if err:
			err.seek( 0 )
		ret.exitcode = child.returncode

	return ret
示例#6
0
def kill(pid, signal=15, timeout=0):
    '''kills the process specified by pid that may be a process id or a
	Child object. The process is killed with the provided signal (by
	default 15). If the process is not dead after <timeout> seconds the
	function exist anyway'''

    # a dispatcher function required to activate the minimal timeout
    def fake_dispatcher():
        return True

    notifier.dispatcher_add(fake_dispatcher)

    if isinstance(pid, Child):
        if pid.pid:
            pid = pid.pid
        else:
            return pid.exitcode

    os.kill(pid, signal)
    countdown = CountDown(timeout)
    while countdown():
        dead_pid, sts = os.waitpid(pid, os.WNOHANG)
        if dead_pid == pid:
            break
        notifier.step()
    else:
        # remove dispatcher function
        notifier.dispatcher_remove(fake_dispatcher)
        return None

    # remove dispatcher function
    notifier.dispatcher_remove(fake_dispatcher)

    if os.WIFSIGNALED(sts):
        return -os.WTERMSIG(sts)

    return os.WEXITSTATUS(sts)