我是靠谱客的博主 明理小土豆,这篇文章主要介绍python在子线程中使用WMI报错-2147221020-moniker,i,bindCTX=pythoncom.MKParseDisplayName(Pathname) 我在一个python脚本中用到了WMI,用于确保杀死超时却未能自己结束的进程 (已经先尝试了Ctrl+Break中止)。 测试代码运行正常,但当我把这个函数放在子线程中使用时,却发现报错: python在子线程中使用WMI报错-2147221020-moniker,i,bindCTX=pythoncom.MKParseDis,现在分享给大家,希望可以做个参考。
我在一个python脚本中用到了WMI,用于确保杀死超时却未能自己结束的进程 (已经先尝试了Ctrl+Break中止)。
测试代码运行正常,但当我把这个函数放在子线程中使用时,却发现报错:
python在子线程中使用WMI报错-2147221020-moniker,i,bindCTX=pythoncom.MKParseDisplayName(Pathname)
解决方法:
后来在网上检索,发现必须添加初始化函数和去初始化函数,所以在一个子线程中可使用的函数代码类似于:
import pythoncom
import subprocess
import logging
def task_kill_timeout(timeout):
pythoncom.CoInitialize()
WMI = win32com.client.GetObject('winmgmts:')
all_process = WMI.ExecQuery('SELECT * FROM Win32_Process where Name="aaa.exe" or Name="bbb.exe" or Name="ccc.exe"')
for process in all_process:
t = process.CreationDate
t = t[:t.find('.')]
start_time = time.strptime(str(t), '%Y%m%d%H%M%S' )
time_passed_by = time.time() - time.mktime(start_time)
if time_passed_by > timeout:
logging.error( 'Run taskkill %s' % process.name)
print 'Run taskkill %s' % process.name
subprocess.Popen('taskkill /F /pid %s' % process.processid,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
)
time.sleep(1.0)
pythoncom.CoUninitialize ()
在C:Python27Libsite-packagesisapiinstall.py中,在方法的后面添加上:
<span style="font-size:18px;"> pythoncom.CoUninitialize ()</span>
<span style="font-size:18px;"> pythoncom.CoUninitialize ()</span>
最后
以上就是明理小土豆最近收集整理的关于python在子线程中使用WMI报错-2147221020-moniker,i,bindCTX=pythoncom.MKParseDisplayName(Pathname) 我在一个python脚本中用到了WMI,用于确保杀死超时却未能自己结束的进程 (已经先尝试了Ctrl+Break中止)。 测试代码运行正常,但当我把这个函数放在子线程中使用时,却发现报错: python在子线程中使用WMI报错-2147221020-moniker,i,bindCTX=pythoncom.MKParseDis的全部内容,更多相关python在子线程中使用WMI报错-2147221020-moniker,i,bindCTX=pythoncom.MKParseDisplayName(Pathname)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复