10.1 data locks

592
1
08-28-2012 03:37 PM
StacyRendall1
Occasional Contributor III
If you are using Arcpy scripting you may have noticed that data lock issues are even more problematic with 10.1 than they were at 10. The Data Access module for cursors, that allows you to use Pythons with statement, is a step in the right direction but it doesn't help you when doing other operations (i.e. GDB creation, Feature Class creation or other editing).

The problem appears to be that locks are just left sitting there `for a while', but the code runs much faster than the speed at which the locks are removed. However, some of the inbuilt Arc tools seem to speed this process up, or force it to occur on demand for File GDBs, allowing you to work on any of the feature classes within the GDB. These tools are arcpy.Compact_management() and arcpy.Exists().

Here is a little function that I use within my code that has dramatically increased the reliability for a script that creates and edits multiple GDBs performs analysis and writes outputs. Sucess rate (the code running in its entirety) went from only succeeding one time in twenty runs, to running perfectly every time when the function is used.

def clearWSLocks(inputWS):
  if not all([arcpy.Exists(inputWS), arcpy.Compact_management(inputWS), arcpy.Exists(inputWS)]):
    print 'Error with Workspace (%s), killing script...' % inputWS
    exit()
  else:
    return True


It is used by simply passing the workspace (GDB) path to the function, and should be done after every operation on either the workspace (i.e. GDB creation) or Feature Classes within the workspace (i.e. Cursors, adding fields, calculations, etc.). For example (shown here as a standalone script incl. function):
import arcpy

def clearWSLocks(inputWS):
  if not all([arcpy.Exists(inputWS), arcpy.Compact_management(inputWS), arcpy.Exists(inputWS)]):
    print 'Error with Workspace (%s), killing script...' % inputWS
    exit()
  else:
    return True

GDBpath = 'C:/Temp/'
GDBname = 'Test.gdb'
tableName = 'SweetFC'

arcpy.CreateFileGDB_management(GDBpath, GDBname)
clearWSLocks(GDBpath+GDBname)

arcpy.CreateTable_management(GDBpath+GDBname, tableName)
clearWSLocks(GDBpath+GDBname)

# etc....


To use the function, copy and paste it between the imports and the actual program, as shown above. It is called in this case with clearWSLocks(GDBpath+GDBname), it appears to do nothing if the Workspace exists, but the checks and compacting effectively clear any residual data locks. If there are other problems with the dataset (i.e. it doesn't exist or couldn't be compacted) the code will exit the program.
Tags (2)
1 Reply
KevinBell
Occasional Contributor III

Very interesting.  I'll give that a try!

0 Kudos