1

I am working on a script tool to be run in ArcGIS Pro (version 2.4.0 with Python 3.6.8) and wish to give the user a warning message and also time to close a feature class so as to avoid the script breaking with a schema lock run time error. I tried applying the code from Pause ArcPy script until feature class lock goes away, however I find that the open feature class is caught when the code is run from an IDE (I'm using Spyder), but it is not caught when run from ArcGIS Pro's Interactive Python Interpreter window or when included in a script tool.

I've tried to make a minimum reproducible example (below). After making the test dataset, Example_Polygons, I open it in Pro and run the test schema lock code from both an IDE and the Pro Python window. lockTest is False in the IDE and True in Pro.

import arcpy, time

# Create test polygon for minimal reproducible example

arcpy.env.overwriteOutput = True
if not arcpy.Exists(r"C:\Temp\test.gdb"):
    arcpy.management.CreateFileGDB(r"C:\Temp","test.gdb")
arcpy.env.workspace = r"C:\Temp\Test.gdb"
srWGS84 = arcpy.SpatialReference("WGS 1984")
arcpy.management.CreateFishnet("Example_Polygons",
                               "0 0", "0 1", 1, 1, 4, 6, None,
                               "NO_LABELS", "DEFAULT", "POLYGON")
arcpy.management.DefineProjection("Example_Polygons", srWGS84)

# Code to compare between when run from IDE and from python interactive window (or potentially script tool) 

# Test schema lock
lockTest = arcpy.TestSchemaLock('Example_Polygons')
print(lockTest)

# Give user message and time to close feature class
while lockTest != True:
    time.sleep(10)
    lockTest = arcpy.TestSchemaLock('Example_Polygons')
    arcpy.AddMessage('Please close Example_Polygons')

if lockTest == True:
    arcpy.AddField_management(in_table = 'Example_Polygons', field_name = 'wave_count', field_type = 'SHORT')

I came across https://support.esri.com/en/technical-article/000011739, which states that "The 64-bit background processing cannot gather the schema lock information across applications running as 32-bit." However, I don't think that is relevant in my case because I'm not running anything as 32-bit (to my knowledge).

1 Answer 1

3

ENIAC-6, this maybe a bit long as there's quite a bit of background.

Some background

  • arcpy.TestSchemaLock returns True if a lock can be applied, else False.

  • with regards to 64 & 32 bit background processing are not part of the ArcGIS Pro application. They are an ArcGIS Desktop/ArcMap feature.

  • ArcGIS Pro is a multi-threaded application, gp tools (including script tools) run in a dedicated thread (so as to leave the main thread free to allow user interactions such as map interaction, button clicks).

  • gp tools have explicit input and output parameters (the data that will be acted on)

  • locks are applied to data when an applications is reading it (layer, or processing)

So the short answer to some of your questions

Q1: Why does arcpy.TestSchemaLock return False when run in a script tool?

A1: Because we free locks on all input and output tool parameters when we send them to the dedicated thread for processing (so the data is not locked by drawing threads). That's why if your input is a layers in the map, it'll show lock icon and do not draw while the gp tool is processing it.

Q2: Why does arcpy.TestSchemaLock return True when run from an external IDE like spider?

A2: If ArcGIS Pro has a layer open on your dataset, then it's placed a lock on it, spider cannot (and should not) break that lock.

So that's a bit of a description of why your're seeing what you're seeing. All this strikes me as expected. Hopefully it helps you get to the behavior you're shooting for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.