473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Importing WMI in a child Thread throws an error

Hi,

I am trying to create a post logon script which does various tasks,
like setup a printer based on location. While most of it works very
fast, I have a second Python script that I run that scans the PC using
WMI (among other things) and writes the following to a database: Name,
Username, Machine, IP, Login Date,CPU,Memory.

The problem I have is that since I import WMI, it takes a long time
and we have users complaining about it. So I stuck the import
statement into a separate thread and set it to a daemon so it could do
its thing in the background and the rest of the script would finish
and exit.

Unfortunately, I get the following error when I do this:

Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python24\lib\threading.py", line 444, in __bootstrap
self.run()
File "C:\Python24\lib\threading.py", line 424, in run
self.__target(*self.__args, **self.__kwargs)
File "//serverName/Scripts/PostLogon_MT.py", line 35, in InvThread
import db_inventory
File "\\serverName\PythonPackages\Utilities\db_inventor y.py", line
3, in ?
import wmi
File "C:\Python24\lib\site-packages\wmi.py", line 204, in ?
obj = GetObject ("winmgmts:")
File "C:\Python24\lib\site-packages\win32com\client\__init__.py",
line 73, in GetObject
return Moniker(Pathname, clsctx)
File "C:\Python24\lib\site-packages\win32com\client\__init__.py",
line 88, in Moniker
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147221020, 'Invalid syntax', None, None)

If I import the module without using threading, it works fine, but
takes forever. Even after it threw that error above, the process
didn't stop and I had to kill it with a control+C.

I am using Windows XP with Python 2.4. Thanks for any help you can
give.

Mike

Feb 27 '07 #1
5 5518
ky******@gmail.com wrote:
The problem I have is that since I import WMI, it takes a long time
and we have users complaining about it. So I stuck the import
statement into a separate thread and set it to a daemon so it could do
its thing in the background and the rest of the script would finish
and exit.
Two things:

1) If you run WMI in a thread, you'll need to call
pythoncom.CoInitialize first:

<code>
import pythoncom
import wmi

pythoncom.CoInitialize ()
c = wmi.WMI ()
#
# do things
#
pythoncom.CoUninitialize ()
</code>

2) If you need a bit of speed running WMI, see the post
I sent a few days ago to someone else:

http://mail.python.org/pipermail/pyt...ry/005550.html

TJG
Feb 27 '07 #2
On Feb 27, 3:32 pm, Tim Golden <m...@timgolden.me.ukwrote:
kyoso...@gmail.com wrote:
The problem I have is that since I import WMI, it takes a long time
and we have users complaining about it. So I stuck the import
statement into a separate thread and set it to a daemon so it could do
its thing in the background and the rest of the script would finish
and exit.

Two things:

1) If you run WMI in a thread, you'll need to call
pythoncom.CoInitialize first:

<code>
import pythoncom
import wmi

pythoncom.CoInitialize ()
c = wmi.WMI ()
#
# do things
#
pythoncom.CoUninitialize ()
</code>

2) If you need a bit of speed running WMI, see the post
I sent a few days ago to someone else:

http://mail.python.org/pipermail/pyt...ry/005550.html

TJG
Thanks! This works for my problem. It appears to cut the real time
required for my script to run by 30-50%. I tried to figure out how to
apply your answer to the other fellow, but I am actually querying WMI
for the amount of RAM and the CPU type and I just don't see how to use
your example in these cases. I am new to the WMI paradigm.

Thanks again,

Mike

Feb 28 '07 #3
ky******@gmail.com wrote:
On Feb 27, 3:32 pm, Tim Golden <m...@timgolden.me.ukwrote:
>kyoso...@gmail.com wrote:
>>The problem I have is that since I import WMI, it takes a long time
and we have users complaining about it. So I stuck the import
statement into a separate thread and set it to a daemon so it could do
its thing in the background and the rest of the script would finish
and exit.
Two things:

1) If you run WMI in a thread, you'll need to call
pythoncom.CoInitialize first:

<code>
import pythoncom
import wmi

pythoncom.CoInitialize ()
c = wmi.WMI ()
#
# do things
#
pythoncom.CoUninitialize ()
</code>

2) If you need a bit of speed running WMI, see the post
I sent a few days ago to someone else:

http://mail.python.org/pipermail/pyt...ry/005550.html

TJG

Thanks! This works for my problem. It appears to cut the real time
required for my script to run by 30-50%. I tried to figure out how to
apply your answer to the other fellow, but I am actually querying WMI
for the amount of RAM and the CPU type and I just don't see how to use
your example in these cases. I am new to the WMI paradigm.
If you want to post some specific code examples, I'm
happy to talk you through possible optimisations.

TJG
Feb 28 '07 #4
On Feb 28, 3:08 pm, Tim Golden <m...@timgolden.me.ukwrote:
kyoso...@gmail.com wrote:
On Feb 27, 3:32 pm, Tim Golden <m...@timgolden.me.ukwrote:
kyoso...@gmail.com wrote:
The problem I have is that since I import WMI, it takes a long time
and we have users complaining about it. So I stuck the import
statement into a separate thread and set it to a daemon so it could do
its thing in the background and the rest of the script would finish
and exit.
Two things:
1) If you run WMI in a thread, you'll need to call
pythoncom.CoInitialize first:
<code>
import pythoncom
import wmi
pythoncom.CoInitialize ()
c = wmi.WMI ()
#
# do things
#
pythoncom.CoUninitialize ()
</code>
2) If you need a bit of speed running WMI, see the post
I sent a few days ago to someone else:
>http://mail.python.org/pipermail/pyt...ry/005550.html
TJG
Thanks! This works for my problem. It appears to cut the real time
required for my script to run by 30-50%. I tried to figure out how to
apply your answer to the other fellow, but I am actually querying WMI
for the amount of RAM and the CPU type and I just don't see how to use
your example in these cases. I am new to the WMI paradigm.

If you want to post some specific code examples, I'm
happy to talk you through possible optimisations.

TJG
Sorry I didn't reply right away. Here's the straight WMI code I'm
using:

c = wmi.WMI()
for i in c.Win32_ComputerSystem():
mem = int(i.TotalPhysicalMemory)
compname = i.Name
for i in c.Win32_Processor ():
cputype = i.Name

This code was wrapped in your CoInitialize com objects.

Let me know if you need more code.

Mike

Mar 15 '07 #5
>If you want to post some specific code examples, I'm
>happy to talk you through possible optimisations.

TJG

Sorry I didn't reply right away. Here's the straight WMI code I'm
using:

c = wmi.WMI()
for i in c.Win32_ComputerSystem():
mem = int(i.TotalPhysicalMemory)
compname = i.Name
for i in c.Win32_Processor ():
cputype = i.Name
Well, don't know how much gain you'll get, but yo
could try the following quickies:

<code>
import wmi
c = wmi.WMI (find_classes=False)

for i in c.Win32_ComputerSystem (
['TotalPhysicalMemory', 'Name']
):
mem = int (i.TotalPhysicalMemory)
compnam = i.Name

for i in c.Win32_Processor (['Name']):
cputype = i.Name

</code>

If you were going to repeat these often (say, in
a loop, which doesn't seem likely given you
examples) you might gain a few nanosecs by
pulling the attribute lookup outside the loop:

<code>
import wmi
c = wmi.WMI (find_classes=False)

ComputerSystem = c.Win32_ComputerSystem
Processor = c.Win32_Processor

while True:
for computer_system in ComputerSystem (...): ...
for processor in Processor (...): ...

</code>

But, as everyone else on this list will tell you,
there's no point in optimising unless you know you
need to to, and unless you know where :-) That's
what modules like timeit profiler are for.

TJG
Mar 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: pothik05 | last post by:
Hi: I will appreciate your comment on the followings: From the main program I start threads. After starting the thread the main program do some other processing. If any of the threads throws...
16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
3
by: Mark | last post by:
All, As you can see below, I have had problems with inquisitive souls looking at data they shouldn't be. I though disabling the ability to create new databases using my workgroup would stop this...
9
by: jillandgordon | last post by:
I am trying to import an excel file into Access 97. It looks perfectly all right but, every time I try to import it, I get to the lst step and am told that it was not imported due to an error. ...
7
by: Neo Geshel | last post by:
Greetings. I have a serious problem. I have multiple sets of tables, several of which are chained more than two tables deep. That is, I have a parent, a child, and a great-grandchild table. ...
3
by: Bob Day | last post by:
Using VS 2003, VB.NET, SQL MSDE.. I am stumped. I have a datagrid with a parent/child relationship between two tables. If I add 200 rows from code in the same win form to the child table with...
5
by: BeruthialsCat | last post by:
First go with trying to import xml to a database and whilst i have managed to do what i want i find that the xml files we have here at work are gonna cause me problems. I have a function that...
2
by: Chen Houwu | last post by:
------------------sample code begin------------------------- import threading import wmi def run(*args): c = wmi.WMI () memory=c.Win32_LogicalMemoryConfiguration() info='Total Virtual...
0
by: Alun Jones | last post by:
I'm getting the above error in a dialog box from Visual Studio 2005 when trying to sign an assembly using a PFX file, and would like to know how to resolve the problem. Background: The PFX...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.