Python WITH statement does not release da cursor schema locks

8329
11
03-04-2013 12:51 PM
LT
by
Occasional 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
LucasDanzinger
Esri Frequent Contributor
I tested this with a few shapefiles and it releases all of the locks for me. I am at 10.1 Service Pack 1, Windows 7 64 bit. What is your set up? Perhaps the issue is specific to a different configuration. Otherwise, could you try this on a co-worker's machine and have another user log into your machine to see if the issue persists?
0 Kudos
LT
by
Occasional Contributor
Thanks for the reply.

I have tested this on several machines.  The behavior is the same.  The schema lock is not released until the cursor is deleted.  You can see it when you look in the directory in Windows Explorer.

I should add that I'm running this code in PythonWin in a stand-alone script, not in the Python window within ArcMap.  Is that how you tried it too?
0 Kudos
LT
by
Occasional Contributor
I should add that I have Windows 7 64-bit , 10.1 with service pack 1.
0 Kudos
LT
by
Occasional Contributor
Hmmm I'm wondering if I wasn't quite specific enough. 

Have you tried running the code and then viewing the shapefile components in Windows Explorer?

The following pic was taking while a cursor was active.  After the with block was existed, the rd.lock file disappears. But the sr.lock file remains.
[ATTACH=CONFIG]22361[/ATTACH]

Thanks for your input.
0 Kudos
LucasDanzinger
Esri Frequent Contributor
Yes, I was viewing the locks through Windows Explorer as well... I am at 10.1 SP1 with Windows 7 64 bit as well. I ran it through PyScipter, so I will test it in PythonWin and let you know.
0 Kudos
LucasDanzinger
Esri Frequent Contributor
Interesting. So here is what I found. The IDE does not make a difference. When I run it and it gets rid of the locks, I use the following syntax:

import arcpy

def main():
    fc = r"C:\TestData\500ft_Grid.shp"
    fields = ["*"]
    with arcpy.da.SearchCursor(fc, fields) as cursor:
        for row in cursor:
            print(row)

if __name__ == '__main__':
    main()


When I take it out of a function and just do the following, the sr lock remains:

fc = r"C:\TestData\500ft_Grid.shp"
fields = ["*"]
with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        print(row)


I'll have to get back to you with more information.

Luke
0 Kudos
LucasDanzinger
Esri Frequent Contributor
The reason it worked when I had it in a function is because the variables were automatically deleted once the code in the function completed successfully.

Since I was able to reproduce the issue, I logged the following bug for this: NIM089529: The data access arcpy cursors do not release locks when using with statement.

-Luke
0 Kudos
LT
by
Occasional Contributor
Thank you, Luke!
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Bug BUG-000083762: In each cursor documentation, specify the type of lock being closed and released, as a shared lock is still present in the geodatabase after the 'with' statement executes.

0 Kudos