473,721 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\li b\threading.py" , line 444, in __bootstrap
self.run()
File "C:\Python24\li b\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\P ythonPackages\U tilities\db_inv entory.py", line
3, in ?
import wmi
File "C:\Python24\li b\site-packages\wmi.py ", line 204, in ?
obj = GetObject ("winmgmts:" )
File "C:\Python24\li b\site-packages\win32c om\client\__ini t__.py",
line 73, in GetObject
return Moniker(Pathnam e, clsctx)
File "C:\Python24\li b\site-packages\win32c om\client\__ini t__.py",
line 88, in Moniker
moniker, i, bindCtx = pythoncom.MkPar seDisplayName(P athname)
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 5595
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.CoIni tialize first:

<code>
import pythoncom
import wmi

pythoncom.CoIni tialize ()
c = wmi.WMI ()
#
# do things
#
pythoncom.CoUni nitialize ()
</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.CoIni tialize first:

<code>
import pythoncom
import wmi

pythoncom.CoIni tialize ()
c = wmi.WMI ()
#
# do things
#
pythoncom.CoUni nitialize ()
</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.CoIn itialize first:

<code>
import pythoncom
import wmi

pythoncom.CoIn itialize ()
c = wmi.WMI ()
#
# do things
#
pythoncom.CoUn initialize ()
</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.CoIni tialize first:
<code>
import pythoncom
import wmi
pythoncom.CoIni tialize ()
c = wmi.WMI ()
#
# do things
#
pythoncom.CoUni nitialize ()
</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_Compute rSystem():
mem = int(i.TotalPhys icalMemory)
compname = i.Name
for i in c.Win32_Process or ():
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_Compute rSystem():
mem = int(i.TotalPhys icalMemory)
compname = i.Name
for i in c.Win32_Process or ():
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=F alse)

for i in c.Win32_Compute rSystem (
['TotalPhysicalM emory', 'Name']
):
mem = int (i.TotalPhysica lMemory)
compnam = i.Name

for i in c.Win32_Process or (['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=F alse)

ComputerSystem = c.Win32_Compute rSystem
Processor = c.Win32_Process or

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
1433
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 any error during their processing, I want to capture that exception from host and log it. Is it possible to capture the exception that a thread throws from the main
16
2670
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 option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
3
1813
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 but it doesn't. All they have to do now is create a new database using the default wrkgroup, close it and then open it again whilst joined to my workgroup and they can import again. Can someone please help me stop these people importing from the FE...
9
3916
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. There is no further explanation. What are the kinds of things that make this happen? Thanks from an obvious rookie. Gordon
7
2009
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. Currently, I am allowing the parent to be edited by itself. In order to get to a child table, a user needs to select a specific parent table ID. I have set this up using panels, and putting a drop-down list in the first panel, with its contents...
3
1484
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 only the parent table displayed, it works fine, and then you can display the child table and all data is as expectect. If I display the child table, and do the exact same thing, try to add 200 rows, only a a few (max 10) are added and...
5
2474
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 imports any aml file, at the moment its importing to a hardcoded (name only) table but i can manage to work out how to take the xml file name and use that for the table name. My problem is some of the xml files do not follow the structural...
2
2455
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 Memory: '\
0
489
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 file has been used for code-signing before (to sign an InfoPath form), so therefore the file contains a valid code-signing key. I have the correct password to the PFX file (because I get a different error
0
8834
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9212
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9056
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8002
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5973
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4483
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2569
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.