本文整理汇总了Python中gevent.subprocess.Popen.terminate方法的典型用法代码示例。如果您正苦于以下问题:Python Popen.terminate方法的具体用法?Python Popen.terminate怎么用?Python Popen.terminate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.subprocess.Popen
的用法示例。
在下文中一共展示了Popen.terminate方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: copper_node
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import terminate [as 别名]
def copper_node(workdir):
logpath = os.path.join(workdir, 'copper.log')
unixpath = os.path.join(workdir, 'copper.sock')
httppath = os.path.join(workdir, 'copper.http')
confpath = os.path.join(workdir, 'copper.conf')
config = {
"listen": [
{
"net": "unix",
"type": "http",
"addr": httppath,
},
{
"net": "unix",
"addr": unixpath,
"allow-changes": True,
},
],
}
with open(confpath, 'w') as f:
# YAML parses valid JSON data
json.dump(config, f)
with open(logpath, 'wb') as logfile:
p = Popen(['copper-node', '-config=' + confpath], shell=False, cwd=workdir, stdout=logfile, stderr=logfile)
try:
while not os.path.exists(unixpath):
time.sleep(0.001)
rc = p.poll()
if rc is not None:
with open(logpath, 'rb') as logfile:
sys.stderr.write(logfile.read())
raise RuntimeError('copper-node exited with status %r' % (rc,))
yield {
'unix': unixpath,
'http': httppath,
}
finally:
if p.poll() is None:
p.terminate()
p.wait()
示例2: __init__
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import terminate [as 别名]
#.........这里部分代码省略.........
self.packaged_dir)
packaging.add_files_to_package(wheel_path, {
'config_file': os.path.join('./', config_file)
})
return wheel_path
def confirm_agent_running(self, agent_name, max_retries=5,
timeout_seconds=2):
running = False
retries = 0
while not running and retries < max_retries:
status = self.test_aip.status_agents()
print ("Status", status)
if len(status) > 0:
status_name = status[0][1]
assert status_name == agent_name
assert len(status[0][2]) == 2, 'Unexpected agent status message'
status_agent_status = status[0][2][1]
running = not isinstance(status_agent_status, int)
retries += 1
time.sleep(timeout_seconds)
return running
# def direct_stop_agent(self, agent_uuid):
# result = self.conn.call.stop_agent(agent_uuid)
# print result
def shutdown_platform(self):
"""
Stop platform here. First grab a list of all of the agents that are
running on the platform, then shutdown, then if any of the listed agent
pids are still running then kill them.
"""
# Handle cascading calls from multiple levels of fixtures.
if self._instance_shutdown:
return
running_pids = []
for agnt in self.list_agents():
pid = self.agent_pid(agnt['uuid'])
if pid is not None and int(pid) > 0:
running_pids.append(int(pid))
# First try and nicely shutdown the platform, which should clean all
# of the agents up automatically.
cmd = ['volttron-ctl']
cmd.extend(['shutdown', '--platform'])
try:
res = subprocess.check_output(cmd, env=self.env)
except CalledProcessError:
if self.p_process is not None:
try:
gevent.sleep(0.2)
self.p_process.terminate()
gevent.sleep(0.2)
except OSError:
self.logit('Platform process was terminated.')
else:
self.logit("platform process was null")
for pid in running_pids:
if psutil.pid_exists(pid):
self.logit("TERMINATING: {}".format(pid))
proc = psutil.Process(pid)
proc.terminate()
if self.use_twistd and self.t_process is not None:
self.t_process.kill()
self.t_process.wait()
elif self.use_twistd:
self.logit("twistd process was null")
if os.environ.get('PRINT_LOG'):
logpath = os.path.join(self.volttron_home, 'volttron.log')
if os.path.exists(logpath):
print("************************* Begin {}".format(logpath))
with open(logpath) as f:
for l in f.readlines():
print(l)
print("************************* End {}".format(logpath))
else:
print("######################### No Log Exists: {}".format(
logpath
))
if not self.skip_cleanup:
self.logit('Removing {}'.format(self.volttron_home))
shutil.rmtree(self.volttron_home, ignore_errors=True)
self._instance_shutdown = True
def __repr__(self):
return str(self)
def __str__(self):
data = []
data.append('volttron_home: {}'.format(self.volttron_home))
return '\n'.join(data)
示例3: run
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import terminate [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
示例4: __init__
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import terminate [as 别名]
#.........这里部分代码省略.........
# def direct_build_agentpackage(self, agent_dir):
# self.logit("Building agent_directory ", agent_dir)
# wheel_path = packaging.create_package(os.path.join('./', agent_dir),
# self.packaged_dir)
#
# return wheel_path
#
# def direct_send_agent(self, package, target):
# pparams = [VCTRL, SEND_AGENT, target, package]
# print (pparams, "CWD", os.getcwd())
# send_process = subprocess.call(pparams, env=self.env)
# print ("Done sending to", target)
#
# def direct_configure_agentpackage(self, agent_wheel, config_file):
# packaging.add_files_to_package(agent_wheel, {
# 'config_file':os.path.join('./', config_file)
# })
#
#
# def direct_build_install_agent(self, agent_dir, config_file):
# agent_wheel = self.build_agentpackage(agent_dir=agent_dir,
# config_file=config_file)
# self.direct_configure_agentpackage(agent_wheel, config_file)
# assert(agent_wheel is not None,"Agent wheel was not built")
#
# uuid = self.test_aip.install_agent(agent_wheel)
# #aip volttron_home, verify_agents
# return uuid
# # conn.call.start_agent()
# def direct_build_install_run_agent(self, agent_dir, config_file):
# agent_uuid = self.direct_build_install_agent(agent_dir, config_file)
# self.direct_start_agent(agent_uuid)
# return agent_uuid
#
# def direct_build_send_agent(self, agent_dir, config_file, target):
# agent_uuid = self.direct_buid_install_agent(agent_dir, config_file)
# self.direct_start_agent(agent_uuid)
# return agent_uuid
def confirm_agent_running(self, agent_name, max_retries=5, timeout_seconds=2):
running = False
retries = 0
while (not running and retries < max_retries):
status = self.test_aip.status_agents()
print ("Status", status)
if len(status) > 0:
status_name = status[0][1]
assert status_name == agent_name
assert len(status[0][2]) == 2, 'Unexpected agent status message'
status_agent_status = status[0][2][1]
running = not isinstance(status_agent_status, int)
retries += 1
time.sleep(timeout_seconds)
return running
# def direct_stop_agent(self, agent_uuid):
# result = self.conn.call.stop_agent(agent_uuid)
# print result
def shutdown_platform(self):
'''Stop platform here
This function will shutdown the platform and attempt to kill any
process that the platformwrapper has started.
'''
import signal
self.logit('shutting down platform: PIDS: {}'.format(self._started_pids))
while self._started_pids:
pid = self._started_pids.pop()
self.logit('ending pid: {}'.format(pid))
try:
os.kill(pid, signal.SIGTERM)
except:
self.logit('could not kill: {} '.format(pid))
if self._p_process != None:
try:
gevent.sleep(0.2)
self._p_process.terminate()
gevent.sleep(0.2)
except OSError:
self.logit('Platform process was terminated.')
else:
self.logit("platform process was null")
if self.use_twistd and self._t_process != None:
self._t_process.kill()
self._t_process.wait()
elif self.use_twistd:
self.logit("twistd process was null")
示例5: TaskExecutor
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import terminate [as 别名]
class TaskExecutor(object):
def __init__(self, balancer, index):
self.balancer = balancer
self.index = index
self.task = None
self.proc = None
self.pid = None
self.conn = None
self.state = None
self.key = str(uuid.uuid4())
self.checked_in = Event()
self.result = AsyncResult()
self.exiting = False
self.thread = gevent.spawn(self.executor)
def checkin(self, conn):
self.balancer.logger.debug('Check-in of worker #{0} (key {1})'.format(self.index, self.key))
self.conn = conn
self.state = WorkerState.IDLE
self.checked_in.set()
def get_status(self):
if not self.conn:
return None
try:
st = TaskStatus(0)
if issubclass(self.task.clazz, MasterProgressTask):
progress_subtask_info = self.conn.call_client_sync(
'taskproxy.get_master_progress_info'
)
if progress_subtask_info['increment_progress'] != 0:
progress_subtask_info['progress'] += progress_subtask_info['increment_progress']
progress_subtask_info['increment_progress'] = 0
self.conn.call_client_sync(
'taskproxy.set_master_progress_detail',
{
'progress': progress_subtask_info['progress'],
'increment_progress': progress_subtask_info['increment_progress']
}
)
if progress_subtask_info['active_tids']:
progress_to_increment = 0
concurent_weight = progress_subtask_info['concurent_subtask_detail']['average_weight']
for tid in progress_subtask_info['concurent_subtask_detail']['tids']:
subtask_status = self.balancer.get_task(tid).executor.get_status()
progress_to_increment += subtask_status.percentage * concurent_weight * \
progress_subtask_info['subtask_weights'][str(tid)]
for tid in set(progress_subtask_info['active_tids']).symmetric_difference(
set(progress_subtask_info['concurent_subtask_detail']['tids'])
):
subtask_status = self.balancer.get_task(tid).executor.get_status()
progress_to_increment += subtask_status.percentage * \
progress_subtask_info['subtask_weights'][str(tid)]
progress_subtask_info['progress'] += int(progress_to_increment)
if progress_subtask_info['pass_subtask_details']:
progress_subtask_info['message'] = subtask_status.message
st = TaskStatus(
progress_subtask_info['progress'], progress_subtask_info['message']
)
else:
st.__setstate__(self.conn.call_client_sync('taskproxy.get_status'))
return st
except RpcException as err:
self.balancer.logger.error(
"Cannot obtain status from task #{0}: {1}".format(self.task.id, str(err))
)
self.proc.terminate()
def put_status(self, status):
# Try to collect rusage at this point, when process is still alive
try:
kinfo = bsd.kinfo_getproc(self.pid)
self.task.rusage = kinfo.rusage
except LookupError:
pass
if status['status'] == 'ROLLBACK':
self.task.set_state(TaskState.ROLLBACK)
if status['status'] == 'FINISHED':
self.result.set(status['result'])
if status['status'] == 'FAILED':
error = status['error']
cls = TaskException
if error['type'] == 'task.TaskAbortException':
cls = TaskAbortException
if error['type'] == 'ValidationException':
cls = ValidationException
self.result.set_exception(cls(
code=error['code'],
message=error['message'],
stacktrace=error['stacktrace'],
extra=error.get('extra')
))
#.........这里部分代码省略.........
示例6: TaskExecutor
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import terminate [as 别名]
class TaskExecutor(object):
def __init__(self, balancer, index):
self.balancer = balancer
self.index = index
self.task = None
self.proc = None
self.pid = None
self.conn = None
self.state = None
self.key = str(uuid.uuid4())
self.checked_in = Event()
self.result = AsyncResult()
gevent.spawn(self.executor)
def checkin(self, conn):
self.balancer.logger.debug('Check-in of worker #{0} (key {1})'.format(self.index, self.key))
self.conn = conn
self.state = WorkerState.IDLE
self.checked_in.set()
def get_status(self):
if not self.conn:
return None
try:
st = TaskStatus(0)
st.__setstate__(self.conn.call_client_sync('taskproxy.get_status'))
return st
except RpcException as err:
self.balancer.logger.error("Cannot obtain status from task #{0}: {1}".format(self.task.id, str(err)))
self.proc.terminate()
def put_status(self, status):
# Try to collect rusage at this point, when process is still alive
try:
kinfo = bsd.kinfo_getproc(self.pid)
self.task.rusage = kinfo.rusage
except LookupError:
pass
if status['status'] == 'FINISHED':
self.result.set(status['result'])
if status['status'] == 'FAILED':
error = status['error']
self.result.set_exception(TaskException(
code=error['code'],
message=error['message'],
stacktrace=error['stacktrace'],
extra=error.get('extra')
))
def run(self, task):
self.result = AsyncResult()
self.task = task
self.task.set_state(TaskState.EXECUTING)
self.conn.call_client_sync('taskproxy.run', {
'id': task.id,
'class': task.clazz.__name__,
'filename': inspect.getsourcefile(task.clazz),
'args': task.args,
'debugger': task.debugger
})
try:
self.result.get()
except BaseException as e:
if not isinstance(e, TaskException):
self.balancer.dispatcher.report_error(
'Task {0} raised exception other than TaskException'.format(self.task.name),
e
)
self.task.error = serialize_error(e)
self.task.set_state(TaskState.FAILED, TaskStatus(0, str(e), extra={
"stacktrace": traceback.format_exc()
}))
self.task.ended.set()
self.balancer.task_exited(self.task)
self.state = WorkerState.IDLE
return
self.task.result = self.result.value
self.task.set_state(TaskState.FINISHED, TaskStatus(100, ''))
self.task.ended.set()
self.balancer.task_exited(self.task)
self.state = WorkerState.IDLE
def abort(self):
self.balancer.logger.info("Trying to abort task #{0}".format(self.task.id))
# Try to abort via RPC. If this fails, kill process
try:
self.conn.call_client_sync('taskproxy.abort')
except RpcException as err:
self.balancer.logger.warning("Failed to abort task #{0} gracefully: {1}".format(self.task.id, str(err)))
self.balancer.logger.warning("Killing process {0}".format(self.pid))
self.proc.terminate()
#.........这里部分代码省略.........