Python多线程中试用wmi报错


Python项目中试用wmi模块获得mac地址,

Python 全选
# 获得机器码
def getMID():
    global MID
    if MID is None:
        computeName = os.environ['COMPUTERNAME']
        # mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
        # MID = computeName + "_" + ("_".join(
        #     [mac[e:e + 2] for e in range(0, 11, 2)]))
        try:
            s = wmi.WMI()
            for nw in s.Win32_NetworkAdapterConfiguration():  # IPEnabled=0
                if MID is None and nw.MACAddress is not None:
                    MID = computeName + '_' + nw.MACAddress.replace(
                        ':', '_')  # 无线局域网适配器 WLAN 物理地址
                    break
        except Exception:
            MID = computeName + '-***'
        s.co
    return MID

单独运行没问题,可如果是在子线程(多进程)中运行,就会报错

wmi -2147221020 无效的语法

Python多线程中试用wmi报错

错误信息:

 
Traceback (most recent call last):
  File "wmi.py", line 1354, in connect
  File "wmi.py", line 258, in handle_com_error

wmi.x_wmi_uninitialised_thread: <x_wmi: Unexpected COM Error (-2147221020, '无效的语法', None, None)>

During handling of the above exception, another exception occurred:

 
Traceback (most recent call last):
  File "run_body.py", line 85, in new_chrome
  File "run_body.py", line 52, in refresh_ip
  File "libs_api.py", line 33, in get_data
  File "libs_api.py", line 23, in getMID
  File "wmi.py", line 1357, in connect

wmi.x_wmi_uninitialised_thread: <x_wmi: WMI returned a syntax error: you're probably running inside a thread without first calling pythoncom.CoInitialize[Ex] (no underlying exception)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "run_body.py", line 131, in run_main
  File "run_body.py", line 107, in new_chrome
  File "run_body.py", line 107, in new_chrome
  File "run_body.py", line 107, in new_chrome
  [Previous line repeated 138 more times]
  File "run_body.py", line 105, in new_chrome
  File "traceback.py", line 167, in format_exc
  File "traceback.py", line 121, in format_exception
  File "traceback.py", line 497, in __init__
  File "traceback.py", line 497, in __init__
  File "traceback.py", line 497, in __init__
  [Previous line repeated 421 more times]
  File "traceback.py", line 473, in __init__

RecursionError: maximum recursion depth exceeded while calling a Python object

解决方案

导入 pythoncom 模块

Python 全选
import pythoncom

初始化 试用wmi模块之前初始化

Python 全选
pythoncom.CoInitialize()

完整代码

Python 全选
# 获得机器码
def getMID():
    global MID
    if MID is None:
        computeName = os.environ['COMPUTERNAME']
        # mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
        # MID = computeName + "_" + ("_".join(
        #     [mac[e:e + 2] for e in range(0, 11, 2)]))
        try:
            pythoncom.CoInitialize()
            s = wmi.WMI()
            for nw in s.Win32_NetworkAdapterConfiguration():  # IPEnabled=0
                if MID is None and nw.MACAddress is not None:
                    MID = computeName + '_' + nw.MACAddress.replace(
                        ':', '_')  # 无线局域网适配器 WLAN 物理地址
                    break
        except Exception:
            MID = computeName + '-***'
        s.co
    return MID

如果还报错,就用

Python 全选
import pythoncom
pythoncom.CoInitialize()
import wmi

 

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:百度地图AK获取
下一篇:ASP.NET Core开发者学习路线图
评论列表

发表评论

评论内容
昵称:
关联文章

Python线试用wmi
C# 线入门系列(二)
C# 线入门系列(三)
C# 线入门系列(一)
SQLite Database 线访问需要注意的问题
.Net 线安全集合
Python退出主进程后子线不会退出的解决方案
Python VSCode调试的时候,代码无法试用相对路径
ManualResetEvent实现线的暂停与恢复
Python windows服务: Installing service ALicense Error installing service: 拒绝访问
WCF常见的:The content type text
Python使用html解析包Requests-html运行py文件没问题,Pyinstaller打包exe后运行
IIS应用程序池自动停止,重启
Python读取excel xlrd读取xlsx:Excel xlsx file; not supported
插入代码页面
PC端编译 Empty block statement no-empty
EFCore链接数据库
.NET Core使用编码GB2312‘GB2312‘ is not a supported encoding name 解决方案
EF 分页 SQL2008 Featch Next
Navicat延长试用,删除注册表无限试用

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码