Script crashes

3003
12
03-24-2016 12:49 PM
BrianLomas
Occasional Contributor III

When I run the following script it just spins and spins then arcmap turns to "not responding". I'm trying to update a versioned parcel layer using a .mdb file. When I "quote" out the updatecursor everything seems to run fine and it seems to enter the edit session as well. Any ideas on what it could be? I'm stuck.

from time import strftime 
    
print "Start script: " + strftime("%Y-%m-%d %H:%M:%S") 
    
import arcpy
from arcpy import env  
     
sourceFC = r'M:\ParcelDatabase\...\Feb_2016_Ownership'
     
sourceFieldsList = ["PARCEL","SITUS", "CURRENT_OWNER", "CO_OWNER", "OWNER_MAILADDR", "OWNER_CITY_STATE", "OWNER_ZIPCODE", "TAX_DISTRICT", "TAX_CODE", "SEC", "TOWNSHIP", "RANGE", "LEGAL"]  
     
# Use list comprehension to build a dictionary from a da SearchCursor  
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}  

arcpy.env.workspace = r"Database Connections\parcelpubdelete.sde" 

updateFC = r"Database Connections\...\ArcGISSDE.SDE.TaxParcel"

updateFieldsList = ['PARCELID','SITEADDRESS','OWNERNME1', 'OWNERNME2', 'PSTLADDRESS', 'PSTLCITY', 'ZIPCODE', 'TAX_DIST', 'TAX_CODE', 'SEC', 'TWN', 'RGE', 'PRPRTYDSCRP']  

edit = arcpy.da.Editor(r"Database Connections\parcelpubdelete.sde")
edit.startEditing(False, True)
edit.startOperation()    
print "entered edit session"

with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:  
     for updateRow in updateRows:  
         # store the Join value of the row being updated in a keyValue variable  
         keyValue = updateRow[0]  
         # verify that the keyValue is in the Dictionary  
         if keyValue in valueDict:  
             # transfer the values stored under the keyValue from the dictionary to the updated fields.  
             for n in range (1,len(sourceFieldsList)):  
                 updateRow = valueDict[keyValue][n-1]  
             updateRows.updateRow(updateRow)  
     
del valueDict

edit.stopOperation()
edit.stopEditing(True) 
     
print "Finished script: " + strftime("%Y-%m-%d %H:%M:%S") 
0 Kudos
12 Replies
JoshuaBixby
MVP Esteemed Contributor

Without an explicit error message, it is difficult to offer suggestions.  I would add some additional print statements to see what part of the code in the update cursor is getting hung up.

0 Kudos
AdrianWelsh
MVP Honored Contributor

Brian,

This may be extra picky but it looks like there are five spaces for the indent in line 27 (where it should be four). I would hope python code could get around that, but I don't know.

If not that then, do you need to start an edit session for UpdateCursor? I was not aware that you needed to (but I do not have a ton of experience using UpdateCursor

(oh, just answered my question about the Edit session and cursors looking at this thread: can't edit row with arcpy.da.updatecursor in arcmap)

0 Kudos
BrianLomas
Occasional Contributor III

I kept getting an error saying I couldn't edit the layer outside an edit session even if I was in an edit session in Arcmap. So, I added the edit operation to the code and it stopped tripping the error.

0 Kudos
WesMiller
Regular Contributor III

You may also want to consider the number of records your dealing with. I would try creating a copy (for backup) and let it run for a while.

0 Kudos
JamesCrandall
MVP Frequent Contributor

Double-check and go over the field lists you have and verify there's no discrepancies between them and the actual sources. 

Also, are you using a Joined field to populate some other source?  If so, make sure you fully qualify the field's joined name, not just it's alias or original source name. (just a guess here).

0 Kudos
BrianLomas
Occasional Contributor III

I double checked the specified fields and everything seems to be good to go... The plan is to join a mdb and a versioned fc then update the fc with the attributes of the mdb.

0 Kudos
RuthBowers
New Contributor III

I'm not sure if this is the problem, but you probably shouldn't be using the name of the command "updateRow" for the name of a variable.  For example, instead of

     for updateRow in updateRows:

put

     for r in updateRows: 

Just wondering, because everything else looks okay at first glance.

0 Kudos
BrianLomas
Occasional Contributor III

I placed a few print statements and everything seemed to be running. So, I added a counter and the script is in fact running but it's only cycling through a record every second or so. We have over 10,000 rows that will have 12 different attributes changed, at this rate it will take entirely too long. When I run the script on a fdb or shp is runs in 2 seconds with the same number of records. I'm new to scripting so I'm not sure if there's something I'm doing wrong. Any suggestions?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Is the time to update a record linear, i.e., does it stay constant over time or get progressively slower?  There have been discussions on GeoNet about the degraded performance of update cursors over time on large record sets.  That said, the performance usually starts out reasonable and gets worse over time.  Also, 10,000 records isn't that large, but it is starting to enter the range where some people say they have issues.  Also, are you using a Parcel Fabric?  I have also heard of slow update performance with parcel fabric over regular feature classes in enterprise geodatabases.

Overall, I think you should open an incident with Esri Support.  If it is taking 1 sec/record from the start, something else is going on either in the application or backend database.