本文整理汇总了Python中psutil.Popen.kill方法的典型用法代码示例。如果您正苦于以下问题:Python Popen.kill方法的具体用法?Python Popen.kill怎么用?Python Popen.kill使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil.Popen
的用法示例。
在下文中一共展示了Popen.kill方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import kill [as 别名]
def run(base_dir, timeout):
"""run the program.
return (ok?, msg)"""
cmd = ['./a.out']
out_path = os.path.join(base_dir, 'test.out')
in_path = os.path.join(base_dir, 'test.in')
with open(out_path, 'w') as fout, open(in_path) as fin:
p = Popen(cmd, stdin=fin, stdout=fout, cwd=base_dir)
try:
p.wait(timeout)
except TimeoutExpired:
p.kill()
return False, 'time limit exceed'
else:
if p.returncode == 0:
return True, ''
else:
return False, 'runtime error'
示例2: __init__
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import kill [as 别名]
#.........这里部分代码省略.........
else:
#update binaries if they have changed
if os.path.getmtime(binDir) > os.path.getmtime(self.startDir):
shutil.rmtree(self.startDir)
shutil.copytree(binDir, self.startDir)
def start(self, ini, xml):
"""schedule the process to start"""
self.jobQueue.put(("_start", (ini, xml)))
def _start(self, ini, xml):
"""start the process"""
if self.isRunning:
return
# write the Halcyon.ini config file
f = open(os.path.join(self.startDir, 'Halcyon.ini'), 'w')
f.write(ini)
f.close()
# write the Regions.cfg file
if not os.path.exists(os.path.join(self.startDir, 'Regions')):
os.mkdir(os.path.join(self.startDir, 'Regions'))
f = open(os.path.join(self.startDir, 'Regions', 'default.xml'), 'w')
f.write(xml)
f.close()
self.proc = Popen(self.startString.split(" "), cwd=self.startDir, stdout=DEVNULL, stderr=DEVNULL)
#write a pidfile
f = open(self.pidFile, 'w')
f.write(str(self.proc.pid))
f.close()
def kill(self):
"""immediately terminate the process"""
if os.path.exists(self.pidFile):
os.remove(self.pidFile)
try:
self.proc.kill()
except psutil.NoSuchProcess:
pass
def saveOar(self, reportUrl, uploadUrl):
"""schedule an oar save and upload to MGM"""
try:
self.jobQueue.put(("_saveOar", (reportUrl, uploadUrl,)))
except:
return False
return True
def _saveOar(self, reportUrl, uploadUrl):
"""perform the actual oar load"""
if not self.isRunning:
print "Save oar aborted, region is not running"
requests.post(reportUrl, data={"Status": "Error: Region is not running"}, verify=False)
return
oarFile = os.path.join(self.startDir, '%s.oar' % self.name)
statusFile = os.path.join(self.startDir, '%s.oarstatus' % self.name)
requests.post(reportUrl, data={"Status": "Saving..."}, verify=False)
#wait for statusfile to be written on completion
while self.isRunning and not os.path.exists(statusFile):
time.sleep(5)
def is_open(file_name):
示例3: Run
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import kill [as 别名]
#.........这里部分代码省略.........
:type path: str
"""
logger.error("%s not found", path)
e3.log.debug('PATH=%s', os.environ['PATH'])
raise OSError(errno.ENOENT,
'No such file or directory, %s not found' % path)
# Try to send an helpful message if one of the executable has not
# been found.
if isinstance(cmds[0], basestring):
if which(cmds[0], default=None) is None:
not_found(cmds[0])
else:
for cmd in cmds:
if which(cmd[0], default=None) is None:
not_found(cmd[0])
def wait(self):
"""Wait until process ends and return its status.
:return: exit code of the process
:rtype: int
"""
if self.status is not None:
# Wait has already been called
return self.status
# If there is no pipe in the loop then just do a wait. Otherwise
# in order to avoid blocked processes due to full pipes, use
# communicate.
if self.output_file.fd != subprocess.PIPE and \
self.error_file.fd != subprocess.PIPE and \
self.input_file.fd != subprocess.PIPE:
self.status = self.internal.wait()
else:
tmp_input = None
if self.input_file.fd == subprocess.PIPE:
tmp_input = self.input_file.get_command()
(self.out, self.err) = self.internal.communicate(tmp_input)
self.status = self.internal.returncode
self.close_files()
return self.status
def poll(self):
"""Check the process status and set self.status if available.
This method checks whether the underlying process has exited
or not. If it hasn't, then it just returns None immediately.
Otherwise, it stores the process' exit code in self.status
and then returns it.
:return: None if the process is still alive; otherwise, returns
the process exit status.
:rtype: int | None
"""
if self.status is not None:
# Process is already terminated and wait been called
return self.status
result = self.internal.poll()
if result is not None:
# Process is finished, call wait to finalize it (closing handles,
# ...)
return self.wait()
else:
return None
def kill(self):
"""Kill the process."""
self.internal.kill()
def interrupt(self):
"""Send SIGINT CTRL_C_EVENT to the process."""
# On windows CTRL_C_EVENT is available and SIGINT is not;
# and the other way around on other platforms.
interrupt_signal = getattr(signal, 'CTRL_C_EVENT', signal.SIGINT)
self.internal.send_signal(interrupt_signal)
def is_running(self):
"""Check whether the process is running.
:rtype: bool
"""
if psutil is None:
# psutil not imported, use our is_running function
return is_running(self.pid)
else:
return self.internal.is_running()
def children(self):
"""Return list of child processes (using psutil).
:rtype: list[psutil.Process]
"""
if psutil is None:
raise NotImplementedError('Run.children() require psutil')
return self.internal.children()