Delete SearchCursor in Python after reference to it has gone

2289
3
Jump to solution
09-22-2014 01:38 AM
GeorgieCassar
Occasional Contributor

Hello,

Due to my unfinished python code (I had not deleted my search cursors), while testing it for a second time, the searchcursor creation failed because the table I am trying to access is locked by the previous session.

Now that the program has finished, how can I unlock the table when I have lost the reference to it ?

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

You need to restart your python session to release the cursor, I do not believe there is any other way to delete the cursor and release the lock. If you are running a script tool that is running in-process, this means you need to shut down the app (for example, ArcMap) entirely and start up again.

This is one of the downsides of the old pre 10.x cursors, you had to be very careful to have your python code delete your cursor, usually with a try/except/finally.

arcpy.da cursors are much better in this way because the with construct is usually used -- if you drop out of the with for any reason, the cursor is deleted by Python for you (since you have left variable scope).

View solution in original post

3 Replies
IanMurray
Frequent Contributor

Are you working from a script file(.py) or are you working in the python window interactively(either in IDLE or ArcMap?)

0 Kudos
GeorgieCassar
Occasional Contributor

Thanks Ian.  I was running my python in a windows command window.

As I didn't have ArcGIS open at all, I wasn't sure what to do.

Then I tried restarting the ArcGIS and SDE services which seems to have remedied the issue.

0 Kudos
curtvprice
MVP Esteemed Contributor

You need to restart your python session to release the cursor, I do not believe there is any other way to delete the cursor and release the lock. If you are running a script tool that is running in-process, this means you need to shut down the app (for example, ArcMap) entirely and start up again.

This is one of the downsides of the old pre 10.x cursors, you had to be very careful to have your python code delete your cursor, usually with a try/except/finally.

arcpy.da cursors are much better in this way because the with construct is usually used -- if you drop out of the with for any reason, the cursor is deleted by Python for you (since you have left variable scope).