Hi all,
I'm trying to run an update cursor on a series of feature classes.
Each class has its own cursor, with different things going on in it.
Each one works fine, except the last feature class.
This feature class will work correctly maybe 2/9 times, otherwise giving the following error.
RuntimeError: Cannot acquire a lock
Sometimes it'll work again if I run it a second time, or if I reopen the project, but neither of these is consistent.
Code sample below:
with arcpy.da.UpdateCursor(tmkFC, ['tmk_txt', 'Docs']) as cursor:
for row in cursor:
if row[0] in tmkDict:
row[1] = "<br>".join(tmkDict[row[0]])
else:
row[1] = None
cursor.updateRow(row)
Does anyone have any ideas as to why this may be happening?
Edit: Found the error page, is not helpful in this case.: 160706: Cannot acquire a lock.—ArcGIS Pro | Documentation
Also, 7) this feature class has significantly more records than the others.
Solved! Go to Solution.
It's probably not the same gremlin you're dealing with, but sometimes when I call cursors from a .pyt, I get random schema locks when the attribute table is open that go away when I close it.
Spitballing reasons:
This is what the docs have to say:
- Exclusive locks are applied when changes are made to a table or feature class. Editing and saving a feature class in a map, changing a table's schema, or using an insert cursor on a feature class in aPythonIDE are examples of when an exclusive lock is applied by ArcGIS.
Update and insert cursors cannot be created for a table or feature class if an exclusive lock exists for that dataset. TheUpdateCursororInsertCursorfunction fails because of an exclusive lock on the dataset. If these functions successfully create a cursor, they apply an exclusive lock on the dataset so that two scripts cannot create an update or insert cursor on the same dataset.
Cursors supportwithstatements to reset iteration and aid in removal of locks. However, using adelstatement to delete the object or wrapping the cursor in a function to have the cursor object go out of scope should be considered to guard against all locking cases.
So, try deleting your cursors (outside of the with block) to be absolutely sure that they don't lock anything anymore:
with arcpy.da.XyzCursor(table, fields) as cursor:
for row in cursor:
pass
del cursor
It's probably not the same gremlin you're dealing with, but sometimes when I call cursors from a .pyt, I get random schema locks when the attribute table is open that go away when I close it.
I cannot believe it.
That was the problem.
For future reference, it must be closed, not just inactive.
Thanks!