Clearing Arc/Arcpy workspace locks

26421
23
11-19-2012 02:00 PM
StacyRendall1
Occasional Contributor III
Particularly with the release of ArcGIS 10.1 I have had a great deal of trouble with workspaces being locked within complex codes that are creating, reading from and writing to numerous feature classes and workspaces. These locks cause the code to fail intermittently at different places, although occasionally making it through without a problem.

I have previously posted on this with a solution that sometimes helps reduce the problem, but is unable to eliminate it completely. That is using arcpy.Exists() and arcpy.Compact() in sequence upon the workspace. This combination of tools, when used in ArcGIS 10.0, seemed adequate, however it is no longer sufficient in 10.1 to prevent issues 100% of the time.

In response to this I have developed a new method for clearing tricky locks which, for my purposes, works 100% of the time. It is a small function that can be added to the start of any arcpy script, and it requires the psutil Python library, which can be acquired here: http://code.google.com/p/psutil/. When using multiprocessing the code seems to work fine with the Parallel Python library, but will hang indefinitely if you are using the Multiprocessing library due to (I guess) the method in which child processes are spawned. However, the Parallel Python library has other issues, for example, I have had trouble with it when getting it to run checked out extensions such as Network Analyst.

The following codeblock shows the necessary imports, the function block and an example of the usage.
import arcpy

import os
import psutil


def clearWSLocks(inputWS):
 '''Attempts to clear ArcGIS/Arcpy locks on a workspace.

 Two methods:
  1: if ANOTHER process (i.e. ArcCatalog) has the workspace open, that process is terminated
  2: if THIS process has the workspace open, it attempts to clear locks using arcpy.Exists, arcpy.Compact and arcpy.Exists in sequence

 Notes:
  1: does not work well with Python Multiprocessing
  2: this will kill ArcMap or ArcCatalog if they are accessing the worspace, so SAVE YOUR WORK

 Required imports: os, psutil
 '''

 # get process ID for this process (treated differently)
 thisPID = os.getpid()

 # normalise path
 _inputWS = os.path.normpath(inputWS)

 # get list of currently running Arc/Python processes
 p_List = []
 ps = psutil.process_iter()
 for p in ps:
  if ('Arc' in p.name) or ('python' in p.name):
   p_List.append(p.pid)

 # iterate through processes
 for pid in p_List:
  p = psutil.Process(pid)

  # if any have the workspace open
  if any(_inputWS in pth for pth in [fl.path for fl in p.get_open_files()]):
   print '      !!! Workspace open: %s' % _inputWS

   # terminate if it is another process
   if pid != thisPID:
    print '      !!! Terminating process: %s' % p.name
    p.terminate()
   else:
    print '      !!! This process has workspace open...'

 # if this process has workspace open, keep trying while it is open...
 while any(_inputWS in pth for pth in [fl.path for fl in psutil.Process(thisPID).get_open_files()]):
  print '    !!! Trying Exists, Compact, Exists to clear locks: %s' % all([arcpy.Exists(_inputWS), arcpy.Compact_management(_inputWS), arcpy.Exists(_inputWS)])

 return True


#######################################
## clearWSLocks usage example #########
#######################################

# define paths and names
workspace = 'C:\\Temp\\test.gdb'
tableName = 'testTable'

# create table
arcpy.CreateTable_management(workspace, tableName)

# usage of clearWSLocks
clearWSLocks(workspace)



I have also attached the code as a separate module (clearWSLocks.py), which can be used in the following fashion:
import arcpy
import clearWSLocks

# define paths and names
workspace = 'C:\\Temp\\test.gdb'
tableName = 'testTable'

# create table
arcpy.CreateTable_management(workspace, tableName)

# usage of clearWSLocks
clearWSLocks.clear(workspace)
Tags (2)
23 Replies
DanPatterson_Retired
MVP Emeritus

p.name would have to be a  string or list or some other iterable

>>> p_name="pythonista"
>>> "python" in p_name
True

or

>>> p_name = None
>>> "python" in p_name
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: argument of type 'NoneType' is not iterable

so whatever p.name is, it isn't an iterable.  So to find out what it is try adding a print statement may be with a type statement in it

>>> p_name = None
>>> type(p_name)
<type 'NoneType'>
0 Kudos
JeffreySadler
New Contributor

I had this same problem Luke Catania. The syntax has changed some in 4 years it looks like. Check out Kim Ollivier's reply above : "p.name is now a function p.name() and p.get_open_files() is now p.open_files()"

LukeCatania
New Contributor III

I had manged to figure it out since, but that did not fix my issue.  I still run into this problem and have not figured it out.  At one point unchecking Enable Background Processing in the GP options fixed it, but I have been getting the error again even with that unchecked.

0 Kudos
StephenSanford
New Contributor

arcpy.DisconnectUser("Database Connections\blahblahblah.sde", "ALL")

ArcGIS Help (10.2, 10.2.1, and 10.2.2) 

0 Kudos