To make talking about it easier, I've slightly reworked
your code to make it run completely (ie I've added imports and
the initialisation of the data structures) and
to take advantage of the wmi module's built-in features.
Hopefully it's perfectly clear what's going on; I've just avoided
some of the more "raw" wmi in your code.
I've also turned off find_classes as you don't really want this
in running code; it's more for use the interpreter. The latest
version of the module 1.4 turns this off by default which makes
the startup time much faster.
<code>
import wmi
services = wmi.WMI(privileges=["security"], find_classes=False)
serials = {}
serials_reversed = {}
drive_letters = {}
for disk in services.Win32_PhysicalMedia():
serials[disk.Tag] = disk.SerialNumber
serials_reversed[disk.Tag] = disk.SerialNumber
for physical_disk in services.Win32_DiskDrive():
for partition in physical_disk.associators("Win32_DiskDriveToDiskPartition"):
for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
drive_letters[logical_disk.DeviceID.rstrip(":")] = physical_disk.DeviceID
print serials
print serials_reversed
print drive_letters
</code>
FWIW, the physical-logical drive stuff is straight out of the
Cookbook page:
http://timgolden.me.uk/python/wmi/cookbook.html#show-disk-partitions
This is on Windows XP and Vista both. It sometimes happens once then
not again for 50 tries. Sometimes it happens 5 times in a row. It
appears that an "OLE Error 0x80041002" is an "object not found error".
So the query is running and line 86 in util.py is trying to get the next
result from the query and is bombing out? For some reason it things
there are more results then there actually are? This code is a direct
port from working Perl code. Anyone have any ideas?
I've managed to produce this error by having the script running
continuously and then inserting a USB stick -- watch the serials
dictionary grow -- and then "ejecting" it, at which point, the
WMI query obviously gets caught out between what it thinks is
there and what is there when it comes to query. It's essentially
a race-condition, I suppose.
Given that this can happen, I can only suggest you catch the
exception, look for the specific 80041002 error code and
ignore it / log it. This is assuming that the circumstances
in which you're running it make it likely that the insertion
or removal of another physical disk is a plausible cause.
TJG
*****************************
Yes, I'm finding this error in a similar situation. A SATA relay is
connecting the drive to the PC running the WMI code about 5 seconds
before. It seems to me I just need a longer delay between the relay
switch and this code executing. Thanks for the tips and code cleanup.
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32