why does this script not work with threading python

2024/10/5 23:29:51

so i've been trying to ifnd a way to access task manager. I've tried a few methods including the wmi module and the windows tasklist but neither suit my need. wmi is way too slow and tasklist becomes too slow when i access it multiple times concurrently in something using multiprocessing. so i found this script which works quite nicely but i can't get it to work with threading.

import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Process")
for objItem in colItems:print "Name: ", objItem.Nameprint "File location: ", objItem.ExecutablePath

this is the error:

Exception in thread Thread-1:
Traceback (most recent call last):File "C:\Python27\lib\threading.py", line 810, in __bootstrap_innerself.run()File "C:\Python27\lib\threading.py", line 763, in runself.__target(*self.__args, **self.__kwargs)File "C:\python practice\stuff.py", line 5, in idkobjWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")File "C:\Python27\lib\site-packages\pypiwin32-219-py2.7-win32.egg\win32com\cli
ent\__init__.py", line 95, in Dispatchdispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,c
lsctx)File "C:\Python27\lib\site-packages\pypiwin32-219-py2.7-win32.egg\win32com\cli
ent\dynamic.py", line 114, in _GetGoodDispatchAndUserNamereturn (_GetGoodDispatch(IDispatch, clsctx), userName)File "C:\Python27\lib\site-packages\pypiwin32-219-py2.7-win32.egg\win32com\cli
ent\dynamic.py", line 91, in _GetGoodDispatchIDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II
D_IDispatch)
com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
Answer

You need to call CoInitialize() in order to use win32com.client:

import pythoncom
import win32com.client as clientpythoncom.CoInitialize()strComputer = "."
objWMIService = client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Process")for objItem in colItems:print "Name: ", objItem.Nameprint "File location: ", objItem.ExecutablePath

For more background information see using win32com with multithreading

https://en.xdnf.cn/q/70433.html

Related Q&A

django-admin command not working in Mac OS

I started Django in Mac OS and after installing Django using pip, I tried to initiated a new project using the command django-admin startproject mysite. I get the error -bash: django-admin: command not…

HeartBleed python test script

I came across this Python script that tests the server for the HeartBleed vulnerability: Would someone be able to explain the content of the "hello", what is being sent and how was this cont…

How to convert a wand image object to numpy array (without OpenCV)?

I am converting pdf files to image using Wand. Then, I do further image processing using ndimage. I would like to directly convert the Wand image into a ndarray... I have seen the answer here, but it u…

Import error running unittest in Python3

I have a problem importing files in Python 3.6. My directories tree is as given below:project/app/├── __init__.py├── a.py└── b.pytest/├── __init__.py├── test_a.py└── test_b.pyIt works…

python: obtaining the OSs argv[0], not sys.argv[0]

(This question was asked here, but the answer was Linux-specific; Im running on FreeBSD and NetBSD systems which (EDIT: ordinarily) do not have /proc.)Python seems to dumb down argv[0], so you dont get…

Why does mypy not accept a list[str] as a list[Optional[str]]?

Example 1: from typing import List, Optionaldef myfunc() -> List[Optional[str]]:some_list = [x for x in "abc"]return some_listMypy complains on example 1:Incompatible return value type (go…

How to do I groupby, count and then plot a bar chart in Pandas?

I have a Pandas dataframe that looks like the following.year month class ---- ----- ----- 2015 1 1 2015 1 1 2015 1 2 2015 1 2 ...I want to be able to create 2 bar chart seri…

How do I execute more code after closing a PyQt window?

Heres an example below:if __name__ == __main__:import sysif (sys.flags.interactive != 1) or not hasattr(QtCore, PYQT_VERSION):QtGui.QApplication.instance().exec_()print "you just closed the pyqt w…

Tor doesnt work with urllib2

I am trying to use tor for anonymous access through privoxy as a proxy using urllib2.System info: Ubuntu 14.04, recently upgraded from 13.10 through dist-upgrade.This is a piece of code I am using for …

Python Selenium Chrome disable prompt for Trying to download multiple files

I am currently running a Python automator which needs to download multiple files within the same session using Selenium Chromedriver.The problem is that when the browser attempts to download the second…