Select to view content in your preferred language

Python WITH statement does not release da cursor schema locks

10141
11
03-04-2013 12:51 PM
LT
by
Regular Contributor
According to this page
http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000

The Python WITH statement should release all data access cursor locks.  It doesn't release schema locks.  *sr.LOCK files persist.  For example, foo remains locked after the following code is run:

import arcpy
with arcpy.da.SearchCursor( 'C:/Temp/foo.shp' , [ "*" ] ) as cursor:
    for row in cursor:
        print row[0]


My questions are...
1) Is this by design?  It kind of makes sense,  since one can go on and use the cursor some more even after the with block is exited.  Seems like it would be bad to edit the schema while you've got a cursor pointing to that file.

2) if this is not by design, is someone going to fix it soon?  If so, when?

3) If this is by design, why use the WITH statement in all the examples?  Sure, it gets rid of the read/write locks, but it doesn't remove all locks, so it messes up your workflow anyway.

4) If this is by design, shouldn't the documentation be modified?

Thanks for the help.
0 Kudos
11 Replies
BlakeTerhune
MVP Frequent Contributor

I did some quick testing here and I confirmed what Luke found: running the with statement in a function removes the locks while running it alone (outside a function) leaves the locks. However, if you run the with statement outside of a function, the locks only remain until you close the IDE (I was using PyScripter). Same thing if you just run the .py file with python.exe, the locks are removed when the script has finished running.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Lucas Danzinger‌ addressed this behavior in one of his earlier replies to this thread, i.e., the locks are removed by the Python interpreter when the cursor objects go out of scope by either having the function end or having the IDE/interpreter closed out.

0 Kudos