本文整理汇总了Python中signal.CTRL_C_EVENT属性的典型用法代码示例。如果您正苦于以下问题:Python signal.CTRL_C_EVENT属性的具体用法?Python signal.CTRL_C_EVENT怎么用?Python signal.CTRL_C_EVENT使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类signal
的用法示例。
在下文中一共展示了signal.CTRL_C_EVENT属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: raise_sigint
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def raise_sigint():
"""
Raising the SIGINT signal in the current process and all sub-processes.
os.kill() only issues a signal in the current process (without subprocesses).
CTRL+C on the console sends the signal to the process group (which we need).
"""
if hasattr(signal, 'CTRL_C_EVENT'):
# windows. Need CTRL_C_EVENT to raise the signal in the whole process group
os.kill(os.getpid(), signal.CTRL_C_EVENT)
else:
# unix.
pgid = os.getpgid(os.getpid())
if pgid == 1:
os.kill(os.getpid(), signal.SIGINT)
else:
os.killpg(os.getpgid(os.getpid()), signal.SIGINT)
示例2: test_CTRL_C_EVENT
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def test_CTRL_C_EVENT(self):
from ctypes import wintypes
import ctypes
# Make a NULL value by creating a pointer with no argument.
NULL = ctypes.POINTER(ctypes.c_int)()
SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int),
wintypes.BOOL)
SetConsoleCtrlHandler.restype = wintypes.BOOL
# Calling this with NULL and FALSE causes the calling process to
# handle Ctrl+C, rather than ignore it. This property is inherited
# by subprocesses.
SetConsoleCtrlHandler(NULL, 0)
self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT")
示例3: test_kill_terminate
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_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)
示例4: test_CTRL_C_EVENT
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def test_CTRL_C_EVENT(self):
from ctypes import wintypes
import ctypes
# Make a NULL value by creating a pointer with no argument.
NULL = ctypes.POINTER(ctypes.c_int)()
SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int),
wintypes.BOOL)
SetConsoleCtrlHandler.restype = wintypes.BOOL
# Calling this with NULL and FALSE causes the calling process to
# handle CTRL+C, rather than ignore it. This property is inherited
# by subprocesses.
SetConsoleCtrlHandler(NULL, 0)
self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT")
示例5: send_sigint
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def send_sigint(self):
# `signal.CTRL_C_EVENT` is also sent to the test process itself.
# See https://docs.python.org/3.6/library/os.html#os.kill
# So we need to wait the signal and ignore it.
# We can NOT ignore the signal by modifying the signal handler here.
# If we temporary ignores the signal, the signal will sent again
# when the signal handler is restored.
# If we ignore the signal permanently, we couldn't interrupt the test.
if os.name == 'nt':
try:
os.kill(self.p.pid, signal.CTRL_C_EVENT)
while True:
pass
except KeyboardInterrupt:
pass
else:
os.kill(self.p.pid, signal.SIGINT)
示例6: fn_terminate_analysis
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def fn_terminate_analysis(self):
"""Sends the SIGTERM or ctrl+c event, to terminate child processes.
This function sends the ctrl+c event to the child process if the
execution platform is Windows, and the SIGTERM signal on others.
Unfortunately, this has the unintended side-effect of sending ctrl+c
to the main process as well, which is handled elsewhere.
"""
if self.jandroid_process == None:
return
# Windows
if self.execution_platform == 'windows':
self.jandroid_process.send_signal(signal.CTRL_C_EVENT)
# Other platforms.
else:
self.jandroid_process.send_signal(signal.SIGTERM)
self.fn_set_stop_analysis_options()
self.main_window.setStatusbar('Analysis terminated')
示例7: send_keyboard_interrupt
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def send_keyboard_interrupt(proc):
"""
Sends a KeyboardInterrupt to the specified child process.
"""
if is_windows:
try:
# Send KeyboardInterrupt to self, and therefore, to child processes
try:
os.kill(0, signal.CTRL_C_EVENT)
except AttributeError:
# Python 2.6 and below
import ctypes
ctypes.windll.kernel32.GenerateConsoleCtrlEvent(0, 0)
# Immediately throws KeyboardInterrupt from the simulated CTRL-C
proc.wait()
except KeyboardInterrupt:
# Ignore the simulated CTRL-C
pass
else:
os.kill(proc.pid, signal.SIGINT)
示例8: _default_handler
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_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)
示例9: sigint_crossplatform
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def sigint_crossplatform(process: subprocess.Popen) -> None:
"""
Send a SIGINT, cross-platform.
The reason is because the subprocess module
doesn't have an API to send a SIGINT-like signal
both on Posix and Windows with a single method.
However, a subprocess.Popen class has the method
'send_signal' that gives more flexibility in this terms.
:param process: the process to send the signal to.
:return: None
"""
if os.name == "posix":
process.send_signal(signal.SIGINT) # pylint: disable=no-member
elif os.name == "nt":
process.send_signal(signal.CTRL_C_EVENT) # pylint: disable=no-member
else:
raise ValueError("Other platforms not supported.")
示例10: reap_process_group
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_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)
示例11: test_app
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def test_app():
p = subprocess.Popen(
'declaracad', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for i in range(20):
time.sleep(1)
if i == 10:
if sys.platform == 'win32':
sig = signal.CTRL_C_EVENT
else:
sig = signal.SIGINT
p.send_signal(sig)
p.poll()
if p.returncode is not None:
break
stdout, stderr = p.communicate()
#for line in stdout.split(b"\n"):
# print(stdout)
assert b'Workbench stopped' in stdout
示例12: run_app
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def run_app(please_stop, server_is_ready):
proc = subprocess.Popen(
["python", "active_data\\app.py", "--settings", "tests/config/elasticsearch.json"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=-1
#creationflags=CREATE_NEW_PROCESS_GROUP
)
while not please_stop:
line = proc.stdout.readline()
if not line:
continue
if line.find(" * Running on") >= 0:
server_is_ready.go()
Log.note("SERVER: {{line}}", {"line": line.strip()})
proc.send_signal(signal.CTRL_C_EVENT)
# read_alternate_settings
示例13: send_signal
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_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))
示例14: handle_interrupt
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def handle_interrupt(self, process):
"""A three level stop mechanism for children - INT -> TERM -> KILL"""
msg = "from {} {{}} pid {}".format(os.getpid(), process.pid)
if self._wait(process, self.suicide_timeout) is None:
self.info("KeyboardInterrupt", msg.format("SIGINT"))
process.send_signal(signal.CTRL_C_EVENT if sys.platform == "win32" else signal.SIGINT)
if self._wait(process, self.interrupt_timeout) is None:
self.info("KeyboardInterrupt", msg.format("SIGTERM"))
process.terminate()
if self._wait(process, self.terminate_timeout) is None:
self.info("KeyboardInterrupt", msg.format("SIGKILL"))
process.kill()
process.communicate()
示例15: onClose
# 需要导入模块: import signal [as 别名]
# 或者: from signal import CTRL_C_EVENT [as 别名]
def onClose(self, wasClean, code, reason):
"""Called when browser tab is closed."""
global websocketserving
self.connection = None
# We r done serving, let everyone else know...
websocketserving = False
# The cleanest way to get a fresh browser tab going in spyder
# is to force vpython to be reimported each time the code is run.
#
# Even though this code is repeated in stop_server below we also
# need it here because in spyder the script may have stopped on its
# own ( because it has no infinite loop in it ) so the only signal
# that the tab has been closed comes via the websocket.
if _in_spyder:
_undo_vpython_import_in_spyder()
# We want to exit, but the main thread is running.
# Only the main thread can properly call sys.exit, so have a signal
# handler call it on the main thread's behalf.
if platform.system() == 'Windows':
if threading.main_thread().is_alive() and not _in_spyder:
# On windows, if we get here then this signal won't be caught
# by our signal handler. Just call it ourselves.
os.kill(os.getpid(), signal.CTRL_C_EVENT)
else:
stop_server()
else:
os.kill(os.getpid(), signal.SIGINT)