AnsweredAssumed Answered

exportToSDDraft leaves aprx file locked

Question asked by don.morrison on Apr 18, 2019
Latest reply on May 3, 2019 by don.morrison

I'm developing a python app that makes heavy use of the ArcGIS API for Python and running into a problem where it appears that when I run exportToSDDraft, a lock is left on the aprx file that I used to create the draft. This causes a failure downstream when I try to reopen the file (or delete it). Any suggestions on how to release this lock or is there an alternative to exportToSDDraft (Python 3). 

 

Here is the error message I get when I try to delete the file is os.remove. I confirmed that it is actually the same process that is holding the lock - not a different process as  the message states.

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'E:\\ROW_as_habitat\\projects\\test.aprx'

Here is the error when I do the delete with arcpy.DeleteManagement

ExecuteError: ERROR 000601: Cannot delete E:\ROW_as_habitat\projects\test.aprx. May be locked by another application.
Failed to execute (Delete).

I boiled down the script as far as I could to recreate the problem. When I remove the line with exportToSDDraft, there is no lock on the aprx file and the script completes successfully.

 

import os
import arcpy

blank_project_fn = os.path.abspath('config/templates/blank_project.aprx')
project_fn = os.path.normpath('E:/ROW_as_habitat/projects/test.aprx')
sd_draft_fn = os.path.normpath('E:/ROW_as_habitat/sd_drafts/test.sddraft')
connection_file = os.path.normpath('E:/ROW_as_habitat/conn3.ags')
layer_fn = os.path.normpath('E:/ROW_as_habitat/org_layer_files/Ameren_Sites.lyrx')

def create_project ():
# Make a copy of the blank temlate project
template_aprx = arcpy.mp.ArcGISProject(blank_project_fn)
template_aprx.saveACopy(project_fn)
del (template_aprx)

# Open the new copy and make changes
aprx_obj = arcpy.mp.ArcGISProject(project_fn)
m_map = aprx_obj.listMaps()[0]
m_map.addLayer(arcpy.mp.LayerFile(layer_fn), "BOTTOM")
aprx_obj.save()

del (m_map)
del (aprx_obj)

def create_sd_draft ():
aprx_obj = arcpy.mp.ArcGISProject(project_fn) # THIS IS THE FILE THE STAYS LOCKED
aprx_map = aprx_obj.listMaps()[0]
# Create MapServiceDraft and set service properties
service_draft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", 'test', aprx_map)
service_draft.targetServer = connection_file
service_draft.exportToSDDraft(sd_draft_fn) # THIS APPEARS TO LEAVE THE APRX FILE LOCKED
del (aprx_map)
del (aprx_obj)
del (service_draft)

def delete_project ():
print (str(os.getpid()))
if arcpy.Exists(project_fn):
  arcpy.Delete_management(project_fn)

 
if __name__ == '__main__':
delete_project ()
create_project ()
create_sd_draft ()
delete_project () # TEST FAILS HERE WHEN exportToSDDRaft is not commented out

Outcomes