本文整理汇总了Python中psutil.Popen.send_signal方法的典型用法代码示例。如果您正苦于以下问题:Python Popen.send_signal方法的具体用法?Python Popen.send_signal怎么用?Python Popen.send_signal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil.Popen
的用法示例。
在下文中一共展示了Popen.send_signal方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import send_signal [as 别名]
#.........这里部分代码省略.........
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()
if self._worker.stdout is not None:
self._worker.stdout.close()
except NoSuchProcess:
pass
def age(self):
"""Return the age of the process in seconds."""
return time.time() - self.started
示例2: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import send_signal [as 别名]
#.........这里部分代码省略.........
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
# dies after poll returns (unlikely)
pass
finally:
self.close_output_channels()
except NoSuchProcess:
pass
def close_output_channels(self):
示例3: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import send_signal [as 别名]
#.........这里部分代码省略.........
# 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.
- **username**: user name that owns the process.
- **nice**: process niceness (between -20 and 20)
- **cmdline**: the command line the process was run with.
"""
示例4: Fly
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import send_signal [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
示例5: Run
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import send_signal [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()
示例6: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import send_signal [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)
#.........这里部分代码省略.........