本文整理汇总了Python中psutil.Popen.poll方法的典型用法代码示例。如果您正苦于以下问题:Python Popen.poll方法的具体用法?Python Popen.poll怎么用?Python Popen.poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil.Popen
的用法示例。
在下文中一共展示了Popen.poll方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cmd
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import poll [as 别名]
#.........这里部分代码省略.........
:type timeout: float
:param tries: How many times you want the command to be retried ?
:type tries: int
:param delay_min: Minimum delay to sleep after every attempt communicate must be True.
:type delay: float, int
:param delay_max: Maximum delay to sleep after every attempt communicate must be True.
:type delay: float, int
* Delay will be a random number in range (`delay_min`, `delay_max`)
* Set kwargs with any argument of the :mod:`subprocess`.Popen constructor excepting
stdin, stdout and stderr.
"""
# convert log argument to logging functions
log_debug = log_warning = log_exception = None
if isinstance(log, logging.Logger):
log_debug, log_warning, log_exception = log.debug, log.warning, log.exception
elif hasattr(log, '__call__'):
log_debug = log_warning = log_exception = log
# create a list and a string of the arguments
if isinstance(command, string_types):
if user is not None:
command = 'sudo -u {0} {1}'.format(user, command)
args_list, args_string = shlex.split(to_bytes(command)), command
else:
if user is not None:
command = ['sudo', '-u', user] + command
args_list = [to_bytes(a) for a in command if a is not None]
args_string = ' '.join([to_unicode(a) for a in command if a is not None])
# log the execution
if log_debug:
log_debug('Execute {0}{1}{2}'.format(
'' if input is None else 'echo {0}|'.format(repr(input)),
args_string,
'' if cli_input is None else ' < {0}'.format(repr(cli_input))))
for trial in xrange(tries): # noqa
# create the sub-process
try:
process = Popen(
args_list,
stdin=subprocess.PIPE,
stdout=None if cli_output else subprocess.PIPE,
stderr=None if cli_output else subprocess.PIPE, **kwargs)
except OSError as e:
# unable to execute the program (e.g. does not exist)
if log_exception:
log_exception(e)
if fail:
raise
return {'process': None, 'stdout': '', 'stderr': e, 'returncode': 2}
# write to stdin (answer to questions, ...)
if cli_input is not None:
process.stdin.write(to_bytes(cli_input))
process.stdin.flush()
# interact with the process and wait for the process to terminate
if communicate:
data = {}
# thanks http://stackoverflow.com/questions/1191374/subprocess-with-timeout
def communicate_with_timeout(data=None):
data['stdout'], data['stderr'] = process.communicate(input=input)
thread = threading.Thread(target=communicate_with_timeout, kwargs={'data': data})
thread.start()
thread.join(timeout=timeout)
if thread.is_alive():
try:
process.terminate()
thread.join()
except OSError as e:
# Manage race condition with process that may terminate just after the call to
# thread.is_alive() !
if e.errno != errno.ESRCH:
raise
stdout, stderr = data['stdout'], data['stderr']
else:
# get a return code that may be None of course ...
process.poll()
stdout = stderr = None
result = {
'process': process,
'stdout': stdout,
'stderr': stderr,
'returncode': process.returncode
}
if process.returncode == 0:
break
# failed attempt, may retry
do_retry = trial < tries - 1
delay = random.uniform(delay_min, delay_max)
if log_warning:
log_warning('Attempt {0} out of {1}: {2}'.format(trial+1, tries,
'Will retry in {0} seconds'.format(delay) if do_retry else 'Failed'))
# raise if this is the last try
if fail and not do_retry:
raise subprocess.CalledProcessError(process.returncode, args_string, stderr)
if do_retry:
time.sleep(delay)
return result
示例2: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import poll [as 别名]
#.........这里部分代码省略.........
if self.watcher is not None:
format_kwargs['sockets'] = self.watcher._get_sockets_fds()
for option in self.watcher.optnames:
if option not in format_kwargs\
and hasattr(self.watcher, option):
format_kwargs[option] = getattr(self.watcher, option)
cmd = replace_gnu_args(self.cmd, **format_kwargs)
if '$WID' in cmd or (self.args and '$WID' in self.args):
msg = "Using $WID in the command is deprecated. You should use "\
"the python string format instead. In you case, this means "\
"replacing the $WID in your command by $(WID)."
warnings.warn(msg, DeprecationWarning)
self.cmd = cmd.replace('$WID', str(self.wid))
if self.args is not None:
if isinstance(self.args, string_types):
args = shlex.split(bytestring(replace_gnu_args(
self.args, **format_kwargs)))
else:
args = [bytestring(replace_gnu_args(arg, **format_kwargs))
for arg in self.args]
args = shlex.split(bytestring(cmd)) + args
else:
args = shlex.split(bytestring(cmd))
logger.debug("process args: %s", args)
return args
@debuglog
def poll(self):
return self._worker.poll()
@debuglog
def is_alive(self):
return self.poll() is None
@debuglog
def send_signal(self, sig):
"""Sends a signal **sig** to the process."""
logger.debug("sending signal %s to %s" % (sig, self.pid))
return self._worker.send_signal(sig)
@debuglog
def stop(self):
"""Stop the process and close stdout/stderr
If the corresponding process is still here
(normally it's already killed by the watcher),
a SIGTERM is sent, then a SIGKILL after 1 second.
The shutdown process (SIGTERM then SIGKILL) is
normally taken by the watcher. So if the process
is still there here, it's a kind of bad behavior
because the graceful timeout won't be respected here.
"""
try:
try:
if self._worker.poll() is None:
return self._worker.terminate()
finally:
if self._worker.stderr is not None:
self._worker.stderr.close()
示例3: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import poll [as 别名]
#.........这里部分代码省略.........
self.args, **format_kwargs)))
else:
args = [bytestring(replace_gnu_args(arg, **format_kwargs))
for arg in self.args]
args = shlex.split(bytestring(cmd), posix=not IS_WINDOWS) + args
else:
args = shlex.split(bytestring(cmd), posix=not IS_WINDOWS)
if self.shell:
# subprocess.Popen(shell=True) implies that 1st arg is the
# requested command, remaining args are applied to sh.
args = [' '.join(quote(arg) for arg in args)]
shell_args = format_kwargs.get('shell_args', None)
if shell_args and IS_WINDOWS:
logger.warn("shell_args won't apply for "
"windows platforms: %s", shell_args)
elif isinstance(shell_args, string_types):
args += shlex.split(bytestring(replace_gnu_args(
shell_args, **format_kwargs)))
elif shell_args:
args += [bytestring(replace_gnu_args(arg, **format_kwargs))
for arg in shell_args]
elif format_kwargs.get('shell_args', False):
logger.warn("shell_args is defined but won't be used "
"in this context: %s", format_kwargs['shell_args'])
logger.debug("process args: %s", args)
return args
def returncode(self):
return self._worker.returncode
@debuglog
def poll(self):
return self._worker.poll()
@debuglog
def is_alive(self):
return self.poll() is None
@debuglog
def send_signal(self, sig):
"""Sends a signal **sig** to the process."""
logger.debug("sending signal %s to %s" % (sig, self.pid))
return self._worker.send_signal(sig)
@debuglog
def stop(self):
"""Stop the process and close stdout/stderr
If the corresponding process is still here
(normally it's already killed by the watcher),
a SIGTERM is sent, then a SIGKILL after 1 second.
The shutdown process (SIGTERM then SIGKILL) is
normally taken by the watcher. So if the process
is still there here, it's a kind of bad behavior
because the graceful timeout won't be respected here.
"""
try:
try:
if self.is_alive():
try:
return self._worker.terminate()
except AccessDenied:
# It can happen on Windows if the process
示例4: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import poll [as 别名]
#.........这里部分代码省略.........
except OverflowError:
if not ctypes:
raise
# versions of python < 2.6.2 don't manage unsigned int for
# groups like on osx or fedora
os.setgid(-ctypes.c_int(-self.gid).value)
if self.uid:
os.setuid(self.uid)
logger.debug('cmd: ' + cmd)
logger.debug('args: ' + str(args))
if args is not None:
if isinstance(args, str):
args_ = shlex.split(bytestring(args))
else:
args_ = args[:]
args_ = shlex.split(bytestring(cmd)) + args_
else:
args_ = shlex.split(bytestring(cmd))
logger.debug('Running %r' % ' '.join(args_))
self._worker = Popen(args_, cwd=self.working_dir,
shell=self.shell, preexec_fn=preexec_fn,
env=self.env, close_fds=True, stdout=PIPE,
stderr=PIPE, executable=executable)
self.started = time.time()
@debuglog
def poll(self):
return self._worker.poll()
@debuglog
def send_signal(self, sig):
"""Sends a signal **sig** to the process."""
return self._worker.send_signal(sig)
@debuglog
def stop(self):
"""Terminate the process."""
try:
if self._worker.poll() is None:
return self._worker.terminate()
finally:
self._worker.stderr.close()
self._worker.stdout.close()
def age(self):
"""Return the age of the process in seconds."""
return time.time() - self.started
def info(self):
"""Return process info.
The info returned is a mapping with these keys:
- **mem_info1**: Resident Set Size Memory in bytes (RSS)
- **mem_info2**: Virtual Memory Size in bytes (VMS).
- **cpu**: % of cpu usage.
- **mem**: % of memory usage.
- **ctime**: process CPU (user + system) time in seconds.
- **pid**: process id.
示例5: Fly
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import poll [as 别名]
class Fly(object):
def __init__(self, wid, cmd, working_dir, shell, uid=None, gid=None, env=None):
self.wid = wid
self.working_dir = working_dir
self.shell = shell
self.env = env
self.cmd = cmd.replace("$WID", str(self.wid))
self.uid = to_uid(uid)
self.gid = to_gid(gid)
def preexec_fn():
os.setsid()
if self.gid:
try:
os.setgid(self.gid)
except OverflowError:
if not ctypes:
raise
# versions of python < 2.6.2 don't manage unsigned int for
# groups like on osx or fedora
os.setgid(-ctypes.c_int(-self.gid).value)
if self.uid:
os.setuid(self.uid)
self._worker = Popen(
self.cmd.split(),
cwd=self.working_dir,
shell=self.shell,
preexec_fn=preexec_fn,
env=self.env,
close_fds=True,
)
self.started = time.time()
def poll(self):
return self._worker.poll()
def send_signal(self, sig):
return self._worker.send_signal(sig)
def stop(self):
if self._worker.poll() is None:
return self._worker.terminate()
def age(self):
return time.time() - self.started
def info(self):
""" return process info """
info = _INFOLINE % get_info(self._worker)
lines = ["%s: %s" % (self.wid, info)]
for child in self._worker.get_children():
info = _INFOLINE % get_info(child)
lines.append(" %s" % info)
return "\n".join(lines)
def children(self):
return ",".join(["%s" % child.pid for child in self._worker.get_children()])
def send_signal_child(self, pid, signum):
pids = [child.pid for child in self._worker.get_children()]
if pid in pids:
child.send_signal(signum)
return "ok"
else:
return "error: child not found"
def send_signal_children(self, signum):
for child in self._worker.get_children():
child.send_signal(signum)
return "ok"
@property
def pid(self):
return self._worker.pid
示例6: Run
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import poll [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()
示例7: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import poll [as 别名]
class Process(object):
"""Wraps a process.
Options:
- **wid**: the process unique identifier. This value will be used to
replace the *$WID* string in the command line if present.
- **cmd**: the command to run. May contain *$WID*, which will be
replaced by **wid**.
- **working_dir**: the working directory to run the command in. If
not provided, will default to the current working directory.
- **shell**: if *True*, will run the command in the shell
environment. *False* by default. **warning: this is a
security hazard**.
- **uid**: if given, is the user id or name the command should run
with. The current uid is the default.
- **gid**: if given, is the group id or name the command should run
with. The current gid is the default.
- **env**: a mapping containing the environment variables the command
will run with. Optional.
"""
def __init__(self, wid, cmd, working_dir=None, shell=False, uid=None,
gid=None, env=None):
self.wid = wid
if working_dir is None:
self.working_dir = get_working_dir()
else:
self.working_dir = working_dir
self.shell = shell
self.env = env
self.cmd = cmd.replace('$WID', str(self.wid))
if uid is None:
self.uid = None
else:
self.uid = to_uid(uid)
if gid is None:
self.gid = None
else:
self.gid = to_gid(gid)
def preexec_fn():
os.setsid()
if self.gid:
try:
os.setgid(self.gid)
except OverflowError:
if not ctypes:
raise
# versions of python < 2.6.2 don't manage unsigned int for
# groups like on osx or fedora
os.setgid(-ctypes.c_int(-self.gid).value)
if self.uid:
os.setuid(self.uid)
self._worker = Popen(self.cmd.split(), cwd=self.working_dir,
shell=self.shell, preexec_fn=preexec_fn,
env=self.env, close_fds=True, stdout=PIPE,
stderr=PIPE)
self.started = time.time()
@debuglog
def poll(self):
return self._worker.poll()
@debuglog
def send_signal(self, sig):
"""Sends a signal **sig** to the process."""
return self._worker.send_signal(sig)
@debuglog
def stop(self):
"""Terminate the process."""
if self._worker.poll() is None:
return self._worker.terminate()
def age(self):
"""Return the age of the process in seconds."""
return time.time() - self.started
def info(self):
"""Return process info.
The info returned is a mapping with these keys:
- **mem_info1**: Resident Set Size Memory in bytes (RSS)
- **mem_info2**: Virtual Memory Size in bytes (VMS).
- **cpu**: % of cpu usage.
- **mem**: % of memory usage.
- **ctime**: process CPU (user + system) time in seconds.
- **pid**: process id.
- **username**: user name that owns the process.
- **nice**: process niceness (between -20 and 20)
#.........这里部分代码省略.........