本文整理匯總了Python中signal.CTRL_BREAK_EVENT屬性的典型用法代碼示例。如果您正苦於以下問題:Python signal.CTRL_BREAK_EVENT屬性的具體用法?Python signal.CTRL_BREAK_EVENT怎麽用?Python signal.CTRL_BREAK_EVENT使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類signal
的用法示例。
在下文中一共展示了signal.CTRL_BREAK_EVENT屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_kill_terminate
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def test_kill_terminate(self):
# subprocess.Popen()'s terminate(), kill() and send_signal() do
# not raise exception after the process is gone. psutil.Popen
# diverges from that.
cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
with psutil.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as proc:
proc.terminate()
proc.wait()
self.assertRaises(psutil.NoSuchProcess, proc.terminate)
self.assertRaises(psutil.NoSuchProcess, proc.kill)
self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
signal.SIGTERM)
if WINDOWS and sys.version_info >= (2, 7):
self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
signal.CTRL_C_EVENT)
self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
signal.CTRL_BREAK_EVENT)
示例2: process_stop
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def process_stop(self):
"""Stop soapy_power process"""
with self._shutdown_lock:
if self.process:
if self.process.poll() is None:
try:
if sys.platform == 'win32':
self.process.send_signal(signal.CTRL_BREAK_EVENT)
else:
self.process.terminate()
except ProcessLookupError:
pass
self.process.wait()
self.process = None
# Close pipe used for communication with soapy_power process
self.pipe_read.close()
self.pipe_read = None
self.pipe_read_fd = None
self.pipe_write_fd = None
self.pipe_write_handle = None
示例3: terminate_process
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def terminate_process(proc: Any, timeout: int = 15) -> None:
if proc.poll() is None:
# Sigint (keyboard interupt)
if sys.platform.startswith("win"):
proc.send_signal(signal.CTRL_BREAK_EVENT)
else:
proc.send_signal(signal.SIGINT)
try:
start = time.time()
while (proc.poll() is None) and (time.time() < (start + timeout)):
time.sleep(0.02)
# Flat kill
finally:
proc.kill()
示例4: _default_handler
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def _default_handler(signum, *args):
''' The default signal handler. Don't register with built-in
signal.signal! This needs to be used on the subprocess await
death workaround.
'''
# All valid cpython windows signals
sigs = {
signal.SIGABRT: SIGABRT,
# signal.SIGFPE: 'fpe', # Don't catch this
# signal.SIGSEGV: 'segv', # Don't catch this
# signal.SIGILL: 'illegal', # Don't catch this
signal.SIGINT: SIGINT,
signal.SIGTERM: SIGTERM,
# Note that signal.CTRL_C_EVENT and signal.CTRL_BREAK_EVENT are
# converted to SIGINT in _await_signal
}
try:
exc = sigs[signum]
except KeyError:
exc = DaemonikerSignal
_sketch_raise_in_main(exc)
示例5: terminate
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def terminate(self):
"""Tries to do something saner on Windows that the stdlib.
Windows:
self.detached/CREATE_NEW_PROCESS_GROUP determines what can be used:
- If set, only SIGBREAK can be sent and it is sent to a single process.
- If not set, in theory only SIGINT can be used and *all processes* in
the processgroup receive it. In practice, we just kill the process.
See http://msdn.microsoft.com/library/windows/desktop/ms683155.aspx
The default on Windows is to call TerminateProcess() always, which is not
useful.
On Posix, always send SIGTERM.
"""
try:
if sys.platform == 'win32' and self.detached:
return self.send_signal(signal.CTRL_BREAK_EVENT)
super(Popen, self).terminate()
except OSError:
# The function will throw if the process terminated in-between. Swallow
# this.
pass
示例6: reap_process_group
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def reap_process_group(*args):
def sigterm_handler(*args):
# Give a one-second grace period for other processes to clean up.
time.sleep(SIGTERM_GRACE_PERIOD_SECONDS)
# SIGKILL the pgroup (including ourselves) as a last-resort.
if sys.platform == "win32":
atexit.unregister(sigterm_handler)
os.kill(0, signal.CTRL_BREAK_EVENT)
else:
os.killpg(0, signal.SIGKILL)
# Set a SIGTERM handler to handle SIGTERMing ourselves with the group.
if sys.platform == "win32":
atexit.register(sigterm_handler)
else:
signal.signal(signal.SIGTERM, sigterm_handler)
# Our parent must have died, SIGTERM the group (including ourselves).
if sys.platform == "win32":
os.kill(0, signal.CTRL_C_EVENT)
else:
os.killpg(0, signal.SIGTERM)
示例7: terminate
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def terminate(proc):
if flags:
proc.send_signal(CTRL_BREAK_EVENT)
else:
proc.terminate()
示例8: send_signal
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def send_signal(self, sig):
"""Send a signal to the process
"""
if sig == signal.SIGTERM:
self.terminate()
elif sig == signal.CTRL_C_EVENT:
os.kill(self.pid, signal.CTRL_C_EVENT)
elif sig == signal.CTRL_BREAK_EVENT:
os.kill(self.pid, signal.CTRL_BREAK_EVENT)
else:
raise ValueError("Unsupported signal: {}".format(sig))
示例9: _kill
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def _kill(proc, gid):
"""Kills the process as gracefully as possible:
* Send CTRL_BREAK_EVENT (to the process group, there's no other way)
* Give main process 30s to quit.
* Call TerminateProcess on the process we spawned.
Unfortunately, we don't have a mechanism to do 'TerminateProcess' to the
process's group, so this could leak processes on Windows.
Returns the process's returncode.
"""
# TODO(iannucci): Use a Job Object for process management. Use subprocess42
# for reference.
del gid # gid is not supported on Windows
try:
# pylint: disable=no-member
proc.send_signal(signal.CTRL_BREAK_EVENT)
except OSError:
pass
# TODO(iannucci): This API changes in python3 to raise an exception on
# timeout.
proc.wait(timeout=30)
if proc.returncode is not None:
return proc.returncode
try:
proc.terminate()
except OSError:
pass
return proc.wait()
示例10: interrupt
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def interrupt(self) -> None:
import signal
try:
self._inferior.send_signal(signal.SIGINT)
except ValueError: # pragma: no cover
# On Windows, SIGINT is not supported, and CTRL_C_EVENT does nothing.
self._inferior.send_signal(signal.CTRL_BREAK_EVENT)
示例11: test_CTRL_BREAK_EVENT
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def test_CTRL_BREAK_EVENT(self):
self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
示例12: _ctrl_handler
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def _ctrl_handler(sig):
"""Handle a sig event and return 0 to terminate the process"""
if sig == signal.CTRL_C_EVENT:
pass
elif sig == signal.CTRL_BREAK_EVENT:
pass
else:
print("UNKNOWN EVENT")
return 0
示例13: test_ctrl_signals
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def test_ctrl_signals(self):
p = psutil.Process(get_test_subprocess().pid)
p.send_signal(signal.CTRL_C_EVENT)
p.send_signal(signal.CTRL_BREAK_EVENT)
p.kill()
p.wait()
self.assertRaises(psutil.NoSuchProcess,
p.send_signal, signal.CTRL_C_EVENT)
self.assertRaises(psutil.NoSuchProcess,
p.send_signal, signal.CTRL_BREAK_EVENT)
示例14: test_ctrl_signals
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def test_ctrl_signals(self):
p = psutil.Process(self.spawn_testproc().pid)
p.send_signal(signal.CTRL_C_EVENT)
p.send_signal(signal.CTRL_BREAK_EVENT)
p.kill()
p.wait()
self.assertRaises(psutil.NoSuchProcess,
p.send_signal, signal.CTRL_C_EVENT)
self.assertRaises(psutil.NoSuchProcess,
p.send_signal, signal.CTRL_BREAK_EVENT)
示例15: interrupt_handled_subprocess
# 需要導入模塊: import signal [as 別名]
# 或者: from signal import CTRL_BREAK_EVENT [as 別名]
def interrupt_handled_subprocess(
cmd, verbose=False, return_object=True, write_to_stdout=False, combine_stderr=True,
block=True, nospin=True, env=None
):
"""Given a :class:`subprocess.Popen` instance, wrap it in exception handlers.
Terminates the subprocess when and if a `SystemExit` or `KeyboardInterrupt` are
processed.
Arguments:
:param str cmd: A command to run
:param bool verbose: Whether to run with verbose mode enabled, default False
:param bool return_object: Whether to return a subprocess instance or a 2-tuple, default True
:param bool write_to_stdout: Whether to write directly to stdout, default False
:param bool combine_stderr: Whether to combine stdout and stderr, default True
:param bool block: Whether the subprocess should be a blocking subprocess, default True
:param bool nospin: Whether to suppress the spinner with the subprocess, default True
:param Optional[Dict[str, str]] env: A dictionary to merge into the subprocess environment
:return: A subprocess, wrapped in exception handlers, as a context manager
:rtype: :class:`subprocess.Popen` obj: An instance of a running subprocess
"""
obj = run(
cmd, verbose=verbose, return_object=True, write_to_stdout=False,
combine_stderr=False, block=True, nospin=True, env=env,
)
try:
yield obj
except (SystemExit, KeyboardInterrupt):
if os.name == "nt":
os.kill(obj.pid, signal.CTRL_BREAK_EVENT)
else:
os.kill(obj.pid, signal.SIGINT)
obj.wait()
raise