Python – 函数无法在新线程中运行
发布时间:2020-12-20 11:25:00 所属栏目:Python 来源:网络整理
导读:我试图使用此函数杀死 Windows上的notepad.exe进程: import thread,wmi,osprint 'CMD: Kill command called'def kill(): c = wmi.WMI () Commands=['notepad.exe'] if Commands[0]!='All': print 'CMD: Killing: ',Commands[0] for process in c.Win32_Proc
我试图使用此函数杀死
Windows上的notepad.exe进程:
import thread,wmi,os print 'CMD: Kill command called' def kill(): c = wmi.WMI () Commands=['notepad.exe'] if Commands[0]!='All': print 'CMD: Killing: ',Commands[0] for process in c.Win32_Process (): if process.Name==Commands[0]: process.Terminate() else: print 'CMD: trying to kill all processes' for process in c.Win32_Process (): if process.executablepath!=inspect.getfile(inspect.currentframe()): try: process.Terminate() except: print 'CMD: Unable to kill: ',proc.name kill() #Works thread.start_new_thread( kill,() ) #Not working 当我调用这样的函数时,它就像一个魅力: 杀() 但是当在一个新线程中运行该函数时它崩溃了,我不知道为什么. 解决方法import thread,os import pythoncom print 'CMD: Kill command called' def kill(): pythoncom.CoInitialize() . . . 在线程中运行Windows函数可能很棘手,因为它通常涉及COM对象.使用pythoncom.CoInitialize()通常允许你这样做.另外,您可能需要查看threading库.处理比线程更容易. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |