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.Name
print "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_inner
self.run()
File "C:\Python27\lib\threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\python practice\stuff.py", line 5, in idk
objWMIService = 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 Dispatch
dispatch, 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 _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python27\lib\site-packages\pypiwin32-219-py2.7-win32.egg\win32com\cli
ent\dynamic.py", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II
D_IDispatch)
com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.
In fact, a Python process cannot run threads in parallel but it can run them concurrently through context switching during I/O bound operations. This limitation is actually enforced by GIL. The Python Global Interpreter Lock (GIL) prevents threads within the same process to be executed at the same time.
Creating Thread Using Threading ModuleDefine a new subclass of the Thread class. Override the __init__(self [,args]) method to add additional arguments. Then, override the run(self [,args]) method to implement what the thread should do when started.
Python is NOT a single-threaded language. Python processes typically use a single thread because of the GIL. Despite the GIL, libraries that perform computationally heavy tasks like numpy, scipy and pytorch utilise C-based implementations under the hood, allowing the use of multiple cores.
You need to call CoInitialize()
in order to use win32com.client
:
import pythoncom
import win32com.client as client
pythoncom.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.Name
print "File location: ", objItem.ExecutablePath
For more background information see using win32com with multithreading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With