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)
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'>
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()"
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.
arcpy.DisconnectUser("Database Connections\blahblahblah.sde", "ALL")