<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1551332#M73079</link>
    <description>&lt;P&gt;I completely agree with you, locks seem so random in arcpy. Throwing in a &lt;EM&gt;ClearWorkspaceCache()&lt;/EM&gt; here and there definitly feels (and&amp;nbsp; i am pretty sure it is) wrong, but at least it works...&lt;/P&gt;</description>
    <pubDate>Wed, 23 Oct 2024 14:58:31 GMT</pubDate>
    <dc:creator>SRK</dc:creator>
    <dc:date>2024-10-23T14:58:31Z</dc:date>
    <item>
      <title>arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/439261#M34479</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;FYI for the community.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;H1 id="toc-hId-1901393748"&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;The Problem:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/H1&gt;&lt;P&gt;So I recently ran into a problem where I wrote a nice little python script to export some feature classes to a FGDB and then zip the FGDB for delivery.&amp;nbsp; Every time I tried to zip the FGDB in the same python process as my 'feature class to feature class conversion' execution the zip routine would fail with a message that looked similar to: &lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P style="padding-left: 30px;"&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; File "&amp;lt;OBSCURE_PYTHON_FILE&amp;gt;.py", line &amp;lt;#&amp;gt;, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; File "C:\Python27\ArcGIS10.2\Lib\zipfile.py", line 1149, in write&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; with open(filename, "rb") as fp:&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;IOError: [Errno 13] Permission denied: u'&amp;lt;OBSCURE_PATH&amp;gt;\\&amp;lt;FGDB_NAME&amp;gt;.gdb\\_gdb.&amp;lt;HOSTNAME&amp;gt;.23104.8124.sr.lock'&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Indicating that something within my process had an 'sr' lock (schema lock??).&amp;nbsp; By placing some time.wait() statements... I tracked it back to the command: &lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;res=arcpy.FeatureClassToFeatureClass_conversion(fc,destination_fgdb,fc)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It appears that the arcpy.FeatureClassToFeatureClass_conversion function was placing a .sr.lock file in the FGDB and that it was not released until the code completed execution. This is a problem since I'm trying to zip up the FGDB after placing some data in it.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;H1 id="toc-hId--650763213"&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;The FIX: &lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/H1&gt;&lt;P&gt;Based on &lt;A _jive_internal="true" href="https://community.esri.com/thread/69480" target="_blank"&gt;this thread&lt;/A&gt;... &lt;A href="https://community.esri.com/migrated-users/25477" target="_blank"&gt;Lucas Danzinger&lt;/A&gt; came to the conclusion that:&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;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.&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so I took the arcpy.FeatureClassToFeatureClass_conversion() call and placed in a function called 'tmp()'.&amp;nbsp; As soon as I did that the .sr.lock files disappeared before my time.wait() executed and the zip process finished with success.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;An alternative (and my preferable method) is to delete the 'res' object (arcpy.Result) once completed.&amp;nbsp; That also effectively removed the sr.lock file.&amp;nbsp; &lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;res=arcpy.FeatureClassToFeatureClass_conversion(fc,destination_fgdb,fc)
if res.status !=4:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #do something
del res&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;H1 id="toc-hId-1092047122"&gt;&lt;SPAN style="color: #e23d39; text-decoration: underline;"&gt;&lt;STRONG&gt;So bottom line...&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/H1&gt;&lt;P&gt;Get in the habit of executing arcpy operations in a function (the .lock will be bound to the scope of the function* and upon completion of the function the .lock file should be cleanly removed) OR delete the result object returned by the function call.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*If the result is written to a global variable, then the scope would be larger than the function and would most likely still retain a .lock file.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this article helps others with these type of issues.&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:39:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/439261#M34479</guid>
      <dc:creator>PF1</dc:creator>
      <dc:date>2021-12-11T19:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/439262#M34480</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much for the post, saved lot of time and frustration. very well written post.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Nov 2016 04:16:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/439262#M34480</guid>
      <dc:creator>saraswathiemani</dc:creator>
      <dc:date>2016-11-22T04:16:04Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1068423#M61372</link>
      <description>&lt;P&gt;I just upgraded to 2.7.1. I think my previous version was 2.5, but not totally sure. I was not having any lock issues before, but now I get a lock file just running the CreateFileGDB function (I do other processing, but even just creating a new FGDB comes up locked). This work-around does not solve my problem, unfortunately.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have to exit Python altogether to get ArcPy to remove the lock file -- see code sample below running it from the command prompt. I may have to run my code in pieces as subprocess calls, which sounds like a Python programmer's nightmare. Thanks, Esri.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; from arcpy.management import CreateFileGDB
&amp;gt;&amp;gt;&amp;gt; def cf():
...     c = CreateFileGDB(r'D:\temp\2021-06', 'test.gdb')
...     del c
...
&amp;gt;&amp;gt;&amp;gt; cf()
&amp;gt;&amp;gt;&amp;gt; from pathlib import Path
&amp;gt;&amp;gt;&amp;gt; p = Path('D:/temp/2021-06/test.gdb')
&amp;gt;&amp;gt;&amp;gt; [str(x) for x in p.glob('*.lock')]
['D:\\temp\\2021-06\\test.gdb\\_gdb.ORG027.4232.16252.sr.lock']
&amp;gt;&amp;gt;&amp;gt; exit()

D:\temp\2021-06&amp;gt;cd test.gdb
D:\temp\2021-06\test.gdb&amp;gt;dir *.lock
Directory of D:\temp\2021-06\test.gdb
File Not Found&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 22:22:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1068423#M61372</guid>
      <dc:creator>davedoesgis</dc:creator>
      <dc:date>2021-06-15T22:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1499725#M70943</link>
      <description>&lt;P&gt;Same here. Just upgraded from ArcGIS Pro 3.0 to 3.3 and the locking nightmare got worse. We found a new function which leaves lock files behind in this version: &lt;EM&gt;arcpy.da.Walk&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;If you want to process a file geodatabase after an arcpy function (e.g. delete, ZIP, copy, etc.) you are lost. The only workaround is a wrapper around every arcpy function which is running in a separted subprocess. When does Esri get the lock problem under control? The large number of threads proves that this is a serious problem.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2024 11:16:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1499725#M70943</guid>
      <dc:creator>Adrian</dc:creator>
      <dc:date>2024-07-01T11:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1547909#M73013</link>
      <description>&lt;P&gt;Just use&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.management.ClearWorkspaceCache()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;when you are done with the arcpy stuff&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2024 08:56:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1547909#M73013</guid>
      <dc:creator>SRK</dc:creator>
      <dc:date>2024-10-11T08:56:34Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1547914#M73014</link>
      <description>&lt;P&gt;Thank you for your answer.&lt;/P&gt;&lt;P&gt;We lately found this workaround with &lt;EM&gt;ClearWorkspaceCache()&lt;/EM&gt; as well and we use it quite often. It seems to work better with releasing locks since ArcGIS Pro 3.3 and it's good to have a tool to release locks on demand.&lt;/P&gt;&lt;P&gt;However, as far as I know, releasing locks is not documented as functionaliy of this method. Additionally it is and remains a workaround for a dirty implementation in arcpy. It's usually not in the responsibility of the user of a library to keep the processed objects in a responsible state. It would be great, if this problem could be addressed in one of the upcoming releases.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2024 09:10:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1547914#M73014</guid>
      <dc:creator>Adrian</dc:creator>
      <dc:date>2024-10-11T09:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1550968#M73074</link>
      <description>&lt;P&gt;SRK, I can't thank you enough!&amp;nbsp; I've been battling lock files on Shape Files for days and nothing seemed to work. Your suggestion to use the ClearWorkspaceCache worked perfectly.&amp;nbsp; Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 16:25:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1550968#M73074</guid>
      <dc:creator>ewmahaffey</dc:creator>
      <dc:date>2024-10-22T16:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy &amp; GDB .lock files: Best Practice for executing arcpy tools while minimizing .lock file persistance</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1551332#M73079</link>
      <description>&lt;P&gt;I completely agree with you, locks seem so random in arcpy. Throwing in a &lt;EM&gt;ClearWorkspaceCache()&lt;/EM&gt; here and there definitly feels (and&amp;nbsp; i am pretty sure it is) wrong, but at least it works...&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2024 14:58:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-gdb-lock-files-best-practice-for-executing/m-p/1551332#M73079</guid>
      <dc:creator>SRK</dc:creator>
      <dc:date>2024-10-23T14:58:31Z</dc:date>
    </item>
  </channel>
</rss>

