Script crashing ArcMap - problems related to locks I think

549
3
06-05-2012 11:04 AM
MattFolsom
New Contributor
I have been having a heck of a time trying to get this script to run.  The best I get is it will run once, and crash the app the second time.  This makes me think it is a locking issue.  I haven't been able to get a clear reference on how this is handled in python / arcpy.  I am running 10.0 sp3, the referenced layers are in a file geodatabase for now.  I eventually wish to modify a SDE feature class, but I want to work out the bugs before I point this at my database. The commented out lines are my attempts to isolate problematic portions of script.  The long term goal of this script will be to copy the geometry from one feature class to another with a matching "MC911_ID" field, but I am stuck here thus far.  Any assistance is greatly appreciated.    

import arcpy

try:
    drow, drows = None, None

    deltas  = arcpy.mapping.Layer("Deltas")
    base = arcpy.mapping.Layer("Baseline")
    dShape = arcpy.Describe(deltas).shapeFieldName
    bShape = arcpy.Describe(base).shapeFieldName
    drows = arcpy.SearchCursor("Deltas")
    dcount = 0
    bcount = 0
    arcpy.SelectLayerByAttribute_management("Baseline","CLEAR_SELECTION")
    arcpy.SelectLayerByAttribute_management("Deltas","CLEAR_SELECTION")
    for drow in drows:
        dID = drow.MC911_ID
        print("drow-" + str(dID))
        brow, brows = None, None
        brows = arcpy.UpdateCursor("Baseline",'"MC911_ID" = ' + str(dID))
        for brow in brows:
            bID = brow.MC911_ID
##            brow.MC911ID =  'Row Checked: ' + str(datetime.datetime.now())
            brows.updateRow(brow)
            bcount = bcount +1
            if bID == dID:
##                brow.setValue("MC911ID", 'Matched') 
##                brows.updateRow(brow)          
                print("Match-" + str(bID))
##                brows.close()
                break
        del brow
        del brows
        dcount = dcount +1
##    drows.close()
    print dcount
    print bcount
    print('Script Complete')      

except Exception as e:
    if not arcpy.GetMessages() == "":
        arcpy.AddMessage(arcpy.GetMessages(2))
        print("I am so sorry, there is a problem...")
        print e.message
        print dcount
        print bcount

finally:
    # Regardless of whether the script succeeds or not, delete 
    #  the row and cursor
    #

    if drow:
        del drow
    if drows:
        del drows

    if deltas:
        del deltas
    if base:
        del base
Tags (2)
0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Try putting some tests in to see if locks are being held.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000024000000

Also it would help to post your error messages. I also find running scripts in functions help clean up problems like this between runs.
0 Kudos
MattFolsom
New Contributor
I'm going to try the funtions suggestion.  As for the TestSchemaLock it always returns FALSE, but with the code below it still executes and updates the geometry.  The diffference is I am using shapefiles now.  I know my process is solid, it is just dealing with the locks and different data sources that drives me nuts.  I stripped down the script from the original post to eliminate possible complications.  Specifically the Try: block.  I occasionally would get a warning stating the Exception class was not supported.  I switched the source of the layers referenced in the script back to the File Geodatabase and ran the script again.  ArcMap immediately folds and crashes. 

import arcpy

deltas = arcpy.mapping.Layer("Deltas")
base = arcpy.mapping.Layer("Base")

lockTest = arcpy.TestSchemaLock(base)
if lockTest:
    print('Schema is not locked')
else:
    print('Schema IS locked')
dShape = arcpy.Describe(deltas).shapeFieldName
bShape = arcpy.Describe(base).shapeFieldName
drows = arcpy.SearchCursor(deltas)
dcount = 0
bcount = 0
arcpy.SelectLayerByAttribute_management("Base","CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management("Deltas","CLEAR_SELECTION")
for drow in drows:
    dID = drow.MC911_ID
    print("drow-" + str(dID))
    brow, brows = None, None
    brows = arcpy.UpdateCursor(base,'"MC911_ID" = ' + str(dID))
    for brow in brows:
        bID = brow.MC911_ID
        brow.MC911ID =  'Row Checked: ' + str(datetime.datetime.now())
        brows.updateRow(brow)
        bcount = bcount +1
        if bID == dID:
            brow.setValue("MC911ID", 'Matched')
            brow.setValue(bShape, drow.getValue(dShape))
            brows.updateRow(brow)          
            print("Match-" + str(bID))
            break
    del brow
    del brows
    dcount = dcount +1
del drow
del drows
print dcount
print bcount
print('Script Complete')      
0 Kudos
MattFolsom
New Contributor
I have changed course and have written the script to run independant of ArcMap and the problem seems to have resolved.  Thanks for yout assistance.
0 Kudos