本文整理汇总了Python中os.kill方法的典型用法代码示例。如果您正苦于以下问题:Python os.kill方法的具体用法?Python os.kill怎么用?Python os.kill使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.kill方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: kill
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def kill(name, sig=signal.SIGTERM):
"""Send a signal to job ``name`` via :func:`os.kill`.
.. versionadded:: 1.29
Args:
name (str): Name of the job
sig (int, optional): Signal to send (default: SIGTERM)
Returns:
bool: `False` if job isn't running, `True` if signal was sent.
"""
pid = _job_pid(name)
if pid is None:
return False
os.kill(pid, sig)
return True
示例2: test_SIGHUP_tty
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def test_SIGHUP_tty(self):
# When not daemonized, SIGHUP should shut down the server.
try:
from signal import SIGHUP
except ImportError:
return self.skip('skipped (no SIGHUP) ')
# Spawn the process.
p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'))
p.write_conf(
extra='test_case_name: "test_SIGHUP_tty"')
p.start(imports='cherrypy.test._test_states_demo')
# Send a SIGHUP
os.kill(p.get_pid(), SIGHUP)
# This might hang if things aren't working right, but meh.
p.join()
示例3: test_SIGTERM
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def test_SIGTERM(self):
'SIGTERM should shut down the server whether daemonized or not.'
self._require_signal_and_kill('SIGTERM')
# Spawn a normal, undaemonized process.
p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'))
p.write_conf(
extra='test_case_name: "test_SIGTERM"')
p.start(imports='cherrypy.test._test_states_demo')
# Send a SIGTERM
os.kill(p.get_pid(), signal.SIGTERM)
# This might hang if things aren't working right, but meh.
p.join()
if os.name in ['posix']:
# Spawn a daemonized process and test again.
p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'),
wait=True, daemonize=True)
p.write_conf(
extra='test_case_name: "test_SIGTERM_2"')
p.start(imports='cherrypy.test._test_states_demo')
# Send a SIGTERM
os.kill(p.get_pid(), signal.SIGTERM)
# This might hang if things aren't working right, but meh.
p.join()
示例4: process_exist
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def process_exist(process_name):
"""
Detect if process exist/ running and kill it.
:param process_name: process name to check
:return: True if processis killed else False
"""
if platform.system() == 'Windows':
import signal
import wmi
c = wmi.WMI()
for process in c.Win32_Process():
if process_name in process.Name:
log(process_name + ' exist...')
log(str(process.ProcessId) + ' ' + str(process.Name))
log("Having Windows explorer won't allow dd.exe to write ISO image properly."
"\nKilling the process..")
try:
os.kill(process.ProcessId, signal.SIGTERM)
return True
except:
log('Unable to kill process ' + str(process.ProcessId))
return False
示例5: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def __init__(self, pid, parent=None):
"""
Create a kill command
The pid argument can either be a real pid (e.g. kill 12345) or a path
to a file containing the pid.
If the pid is coming from a file, it will be resolved at the time that
execute is called. Before that time, the command will be stored
internally as ["kill", "/path/to/file"]. This is not a real command,
but it is meaningful if you print the command object.
"""
# This will not be a valid command if pid is a file path.
command = ["kill", pid]
super(KillCommand, self).__init__(command, parent)
# Is it a numeric pid or a path to a pid file?
try:
self.pid = int(pid)
self.fromFile = False
except ValueError:
self.pid = pid
self.fromFile = True
示例6: execute
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def execute(self):
pid = self.getPid()
if pid is None:
self.result = 0
return True
try:
retval = kill(pid)
self.result = 0
out.info('Command "kill {}" returned {}\n'.format(pid, retval))
except Exception as e:
out.info('Command "kill {}" raised exception {}\n'.format(pid, e))
self.result = e
return (self.result == 0)
示例7: shell_background
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def shell_background(cmd_, **kwargs):
"""Run process in background, join on exit."""
args = format_cmd(cmd_, **kwargs)
process = sp.Popen(args)
try:
yield process
finally:
if process.poll() is None:
process.terminate()
time.sleep(1)
if process.poll() is None:
process.kill()
time.sleep(1)
if process.poll() is None:
raise ValueError(
"Cannot kill process %d - please kill manually" % process.pid)
time.sleep(1)
示例8: testInterruptCaught
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def testInterruptCaught(self):
default_handler = signal.getsignal(signal.SIGINT)
result = unittest.TestResult()
unittest.installHandler()
unittest.registerResult(result)
self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
result.breakCaught = True
self.assertTrue(result.shouldStop)
try:
test(result)
except KeyboardInterrupt:
self.fail("KeyboardInterrupt not handled")
self.assertTrue(result.breakCaught)
示例9: testSecondInterrupt
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def testSecondInterrupt(self):
result = unittest.TestResult()
unittest.installHandler()
unittest.registerResult(result)
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
result.breakCaught = True
self.assertTrue(result.shouldStop)
os.kill(pid, signal.SIGINT)
self.fail("Second KeyboardInterrupt not raised")
try:
test(result)
except KeyboardInterrupt:
pass
else:
self.fail("Second KeyboardInterrupt not raised")
self.assertTrue(result.breakCaught)
示例10: testTwoResults
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def testTwoResults(self):
unittest.installHandler()
result = unittest.TestResult()
unittest.registerResult(result)
new_handler = signal.getsignal(signal.SIGINT)
result2 = unittest.TestResult()
unittest.registerResult(result2)
self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)
result3 = unittest.TestResult()
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
try:
test(result)
except KeyboardInterrupt:
self.fail("KeyboardInterrupt not handled")
self.assertTrue(result.shouldStop)
self.assertTrue(result2.shouldStop)
self.assertFalse(result3.shouldStop)
示例11: testHandlerReplacedButCalled
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def testHandlerReplacedButCalled(self):
# If our handler has been replaced (is no longer installed) but is
# called by the *new* handler, then it isn't safe to delay the
# SIGINT and we should immediately delegate to the default handler
unittest.installHandler()
handler = signal.getsignal(signal.SIGINT)
def new_handler(frame, signum):
handler(frame, signum)
signal.signal(signal.SIGINT, new_handler)
try:
pid = os.getpid()
os.kill(pid, signal.SIGINT)
except KeyboardInterrupt:
pass
else:
self.fail("replaced but delegated handler doesn't raise interrupt")
示例12: test_run_async_exit_code
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def test_run_async_exit_code(self):
def run_with_exit_code_0(process_idx):
sys.exit(0)
def run_with_exit_code_11(process_idx):
os.kill(os.getpid(), signal.SIGSEGV)
with warnings.catch_warnings(record=True) as ws:
async_.run_async(4, run_with_exit_code_0)
# There should be no AbnormalExitWarning
self.assertEqual(
sum(1 if issubclass(
w.category, async_.AbnormalExitWarning) else 0
for w in ws), 0)
with warnings.catch_warnings(record=True) as ws:
async_.run_async(4, run_with_exit_code_11)
# There should be 4 AbnormalExitWarning
self.assertEqual(
sum(1 if issubclass(
w.category, async_.AbnormalExitWarning) else 0
for w in ws), 4)
示例13: deleteoldstub
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def deleteoldstub():
checkfilename = 'AdobePush.exe' #The exe in the startup will be saved by the name of AdobePush. When the exe will be updated the old exe will be deleted.
checkdir = 'C://Users//' + currentuser + '//AppData//Roaming//Microsoft//Windows//Start Menu//Programs//Startup//'
dircontent = os.listdir(checkdir)
try:
try:
pids = getpid('AdobePush.exe')
for id in pids:
os.kill(int(id), signal.SIGTERM)
except Exception as e:
print e
if checkfilename in dircontent:
os.remove(checkdir + checkfilename)
except Exception as e:
print e
#Function to copy the exe to startup
示例14: test_lock_out_of_context_single
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def test_lock_out_of_context_single(self):
r, w = pipe()
g = gevent.spawn(lambda r: r.get(), r)
gevent.sleep(SHORTTIME)
with raises(GIPCLocked):
with r:
pass
# The context manager can't close `r`, as it is locked in `g`.
g.kill(block=False)
# Ensure killing via 'context switch', i.e. yield control to other
# coroutines (otherwise the subsequent close attempt will fail with
# `GIPCLocked` error).
gevent.sleep(-1)
# Close writer first. otherwise, `os.close(r._fd)` would block on Win.
w.close()
r.close()
示例15: test_lock_out_of_context_pair
# 需要导入模块: import os [as 别名]
# 或者: from os import kill [as 别名]
def test_lock_out_of_context_pair(self):
with raises(GIPCLocked):
with pipe(True) as (h1, h2):
# Write more to pipe than pipe buffer can hold
# (makes `put` block when there is no reader).
# Buffer is quite large on Windows.
gw = gevent.spawn(lambda h: h.put(LONGERTHANBUFFER), h1)
gevent.sleep(SHORTTIME)
# Context manager tries to close h2 reader, h2 writer, and
# h1 writer first. Fails upon latter, must still close
# h1 reader after that.
assert not h1._writer._closed
assert h1._reader._closed
assert h2._writer._closed
assert h2._reader._closed
# Kill greenlet (free lock on h1 writer), close h1 writer.
gw.kill(block=False)
gevent.sleep(-1)
h1.close()
assert h1._writer._closed