python在子线程中想要使用WMI查看一个进程是否活着,但是在子线程中调用时会出现如下错误:
后来查找原因发现需要在每次使用WMI时进行初始化:
#check_exsit
def check_exsit(process_name):
#在线程中使用需要加初始化和去初始化方法
pythoncom.CoInitialize()
WMI = win32com.client.GetObject(
'winmgmts:')
processCodeCov = WMI.ExecQuery(
'select * from Win32_Process where Name="%s"' % process_name)
if len(processCodeCov) >
0:
#判断操作
print
'%s is exists' % process_name
pythoncom.CoUninitialize()
return
True
else:
print
'%s is not exists' % process_name
pythoncom.CoUninitialize()
return
False
while
True:
if check_exsit(
'gui_flash_programmer.exe') ==
False:
lab[i].configure(text =
"Flash programmer2 not open...")
lab[i].configure(bg =
'blue')
else:
lab[i].configure(bg =
'blue')
break
time.sleep(
0.3)
lab[i].configure(bg =
'yellow')
time.sleep(
0.3)
添加初始化方法后可以正常使用