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)
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)
Is the IN_MEMORY space inadequate for some reason (eg, the data is simply too large)?
Thanks James,
I have seen a few people talk about IN_MEMORY, but I have never seen any help files or code examples using it, so I have never tried it out! Sounds like something I should look into!
Cheers,
Stacy
def ClearINMEM(): ## clear out the IN_MEMORY workspace of any featureclasses try: gp.Workspace = "IN_MEMORY" ### clear in_memory of featureClasses fcs = gp.ListFeatureClasses() ### for each FeatClass in the list of fcs's, delete it. for f in fcs: gp.Delete_management(f) gp.AddMessage("deleted feature class: " + f) ### clear in_memory of tables tables = gp.ListTables() for tbl in tables: gp.Delete_management(tbl) gp.AddMessage("deleted table: " + tbl) gp.AddMessage("in_memory space cleared") except: gp.AddMessage("The following error(s) occured attempting to ClearINMEM " + gp.GetMessages()) return
Late to the party but....
Stacy, thanks for the post.
I've inherited some python code that makes use of your 10.0 ClearWSLocks so very helpful to find this "newer" version since we're still running servers on 10.1
Just to add a contribution, the IN_MEMORY is very useful, IF you have sufficient RAM on the processing device.
I wrote a script to pickup a file geodB of roads for a large county (almost 1200 Sq Miles) and dump part of it out into an Excel file. Using IN_MEMORY really sped it up.
Hi Stacy,
I am trying to update a feature class by running a python script after hours.
Unfortunately I am having trouble with file locks caused by other users.
I have tried your clearWSLocks.py, but it runs with this error:
So it is complaining about this section of code:
Do you have any idea what could be causing this?
I am using ArcGIS 10.2... not sure if that makes a difference or not.
Thanks for your help!
Scott, did you download psutil (or do you have it already?) Looks like you can get it
https://pypi.python.org/pypi/psutil/#downloads
If that isn't imported, then is will have an issue with line 26 in your code above.
Hi there,
Yes I have installed psutil... any other ideas?
Thanks.