def isRunning(process_name):
try:
process=len(os.popen('tasklist | findstr '+process_name).readlines())
if process >=1 :
return True
else:
return False
except:
print("程序错误")
return False
def task_killer():
while 1:
if isRunning('Taskmgr.exe'):
os.system('taskkill /IM Taskmgr.exe')
time.sleep(1)
以上代码用来干掉 Taskmgr.exe 进程,使用 Threading 模块的 Thread 实现多线程
t1 = threading.Thread(target=task_killer)
t1.start()
现在想问怎么终止这个线程,t1.join 没用,还会堵塞,t1.join(1)也没用,google 找了以下代码也没用
def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
'''强制关闭某一个线程'''
_async_raise(thread.ident, SystemExit)
谢谢!
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.