当前位置: 首页>>代码示例>>Python>>正文


Python Popen.kill方法代码示例

本文整理汇总了Python中gevent.subprocess.Popen.kill方法的典型用法代码示例。如果您正苦于以下问题:Python Popen.kill方法的具体用法?Python Popen.kill怎么用?Python Popen.kill使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gevent.subprocess.Popen的用法示例。


在下文中一共展示了Popen.kill方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: proc

# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import kill [as 别名]
def proc(input, output, args, env=None, stderr=None):
	"""Run a subprocess, passing input to its stdin and sending its stdout to output,
	with each item newline-seperated.
	Args is either a string to be shell-interpreted, or a list of args.
	stderr is either not redirected (default), mixed in with stdout (pass subprocess.STDOUT),
	or redirected to a given file.
	"""
	from gevent.subprocess import Popen, PIPE, STDOUT

	if isinstance(args, unicode):
		args = args.encode('utf8')
	if isinstance(args, str):
		shell = True
	else:
		shell = False

	group = gevent.pool.Group()

	proc = None
	try:
		proc = Popen(args, shell=shell, env=env, stdin=PIPE, stdout=PIPE, stderr=stderr)

		@group.spawn
		def do_input():
			for item in input:
				item = item.encode('utf8') if isinstance(item, unicode) else str(item)
				proc.stdin.write('{}\n'.format(item))
				proc.stdin.flush()
			proc.stdin.close()

		@group.spawn
		def do_output():
			for line in proc.stdout:
				output.put(line.rstrip('\n'))
			output.close()

		proc.wait()
		group.join()
	finally:
		if proc and proc.poll() is None:
			try:
				proc.kill()
			except OSError as e:
				if e.errno != errno.ESRCH:
					raise
开发者ID:ekimekim,项目名称:pylibs,代码行数:47,代码来源:conc.py

示例2: get_git_refs

# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import kill [as 别名]
def get_git_refs():
    if DISABLE_NEW_EXTENSIONS:
        return 'Disabled', 403

    git_endpoint = request.values.get('ep', None)
    if git_endpoint is None:
        return jsonify(error={'message': 'Missing endpoint'}), 400

    if not git_endpoint.endswith('.git'):
        return jsonify(error={'message': 'Invalid git endpoint'}), 400

    git_path = config.get('MINEMELD_GIT_PATH', None)
    if git_path is None:
        return jsonify(error={'message': 'MINEMELD_GIT_PATH not set'}), 500

    git_args = [git_path, 'ls-remote', '-t', '-h', git_endpoint]

    git_process = Popen(
        args=git_args,
        close_fds=True,
        shell=False,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )

    timeout = Timeout(20.0)
    timeout.start()
    try:
        git_stdout, git_stderr = git_process.communicate()

    except Timeout:
        git_process.kill()
        return jsonify(error={'message': 'Timeout accessing git repo'}), 400

    finally:
        timeout.cancel()

    if git_process.returncode != 0:
        LOG.error('Error running {}: {}'.format(git_args, git_stderr))
        return jsonify(error={'message': 'Error running git: {}'.format(git_stderr)}), 400

    return jsonify(result=[line.rsplit('/', 1)[-1] for line in git_stdout.splitlines()])
开发者ID:jtschichold,项目名称:minemeld-core,代码行数:44,代码来源:extensionsapi.py

示例3: run

# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import kill [as 别名]

#.........这里部分代码省略.........
      err_to_out (bool): redirect stderr to stdout if True, default is False
      input (str or tuple of str): str will be flushed to stdin after executed command, default is None
      force (bool): executing full operations even if envs.common.dry_run is True
      **kwargs (dict): add only for supporting dry-run replacing

    Return:
      str if freturn is False: string that contained all stdout messages
      tuple if freturn is True:
        string that contained all stdout messages
        string that contained all stderr
        int that mean return code of command

    """
    # hack for dry-run
    if envs.common.dry_run and not force:
        from dry_operations import run
        return run(command, use_sudo, user, group, freturn, err_to_out, input, **kwargs)

    logger = envs.connect.logger
    interactive = envs.common.interactive
    parallel = envs.common.parallel
    host_string = ''.join((envs.connect.user,
                           '@',
                           envs.connect.host))
    logger.debug('executing run function')
    logger.debug('arguments for executing and another locals: %s', locals())

    command = command_patching_for_sudo(command, use_sudo, user, group)

    # logging
    write_message_to_log(command, 'in: ')

    stderr = PIPE
    if err_to_out:
        stderr = STDOUT
    logger.debug('stderr: %s', stderr)
    # open new connect
    if envs.connect.host in envs.common.localhost:
        logger.debug('executing command %s with shell=True', command)
        p = Popen(command, stdout=PIPE, stderr=stderr, stdin=PIPE, shell=True)
    else:
        scommand = [
            envs.common.ssh_binary,
            envs.common.ssh_port_option,
            str(envs.connect.port),
            host_string,
        ]
        scommand += envs.common.ssh_args.split()
        scommand += envs.connect.con_args.split()
        scommand += [command]
        logger.debug('executing command %s', scommand)
        p = Popen(scommand, stdout=PIPE, stderr=stderr, stdin=PIPE)
    # flush input
    if input:
        if type(input) is str:
            input = [input]
        for s in input:
            s = str(s)
            if s[-1] not in ('\n', '\r'):
                s += '\n'
            logger.debug('flushing input %s', s)
            p.stdin.write(s)
            p.stdin.flush()
    # run another command
    if parallel:
        gevent.sleep(0)
        logger.debug('run another command with gevent.sleep(0)')
    # processing std loop
    threads = []
    if interactive:
        args = (p, copy(envs.common), copy(envs.connect))
        gin = gevent.spawn(in_loop, *args)
        logger.debug('executing in_loop with args %s', args)
        threads.append(gin)

    args = (p, copy(envs.common), copy(envs.connect))
    gout = gevent.spawn(out_loop, *args)
    logger.debug('executing out_loop with args %s', args)
    threads.append(gout)

    if not err_to_out:
        args = (p, copy(envs.common), copy(envs.connect), True)
        gerr = gevent.spawn(out_loop, *args)
        logger.debug('executing err_loop with args %s', args)
        threads.append(gerr)

    gevent.joinall(threads)
    logger.debug('child process has terminated with status %s', p.returncode)
    #TODO: check returncode if returncode==None
    sumout = gout.value
    sumerr = gerr.value if not err_to_out else ''
    status = p.returncode
    if p.poll() is None:
        p.terminate()
        p.kill()
    if freturn:
        logger.debug('return sumout %s, sumerr %s, status %s', sumout, sumerr, status)
        return (sumout, sumerr, status)
    logger.debug('return sumout %s', sumout)
    return sumout
开发者ID:Friz-zy,项目名称:factory,代码行数:104,代码来源:operations.py


注:本文中的gevent.subprocess.Popen.kill方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。