Geoprocessing service gives error, but runs fine when ran as standalone script or python tool

3090
3
09-09-2015 03:07 PM
MichaelSouffront
New Contributor III

Has anyone have this problem? I created a python script, and it runs fine in a python interface or as a python tool in Arcmap, but when I convert it to a geoprocessing service it won't run. Furthermore, the error it gives it's unclear:

RuntimeError: Objects in this class cannot be updated outside an edit session

This error doesn't make sense because 1. the process is done in an editing session, and 2. it works as a standalone script or as a python tool.

Below is the code in case you want to take a look, but again it runs fine so the problem seems to be in the geoprocessing service step.

import arcpy

arcpy.env.overwriteOutput = True

# If needed create a connection file to the sde geodatabase
if not arcpy.Exists('connect.sde'):
  arcpy.CreateDatabaseConnection_management('************')

arcpy.env.workspace = r'connect.sde'

# Input files
districts = '*****'
sites = '*****'


arcpy.MakeFeatureLayer_management(sites, 'sites')


# Start an editing session
with arcpy.da.Editor(arcpy.env.workspace) as edit:
  
  fields = ['Name', 'totalsites', 'completedsites', 'SHAPE@']
  
  with arcpy.da.UpdateCursor(districts, fields) as cursor:
    for row in cursor:
      name = row[0]
      shape = row[3]
  
      arcpy.SelectLayerByLocation_management('sites', 'WITHIN', shape)
      arcpy.CopyFeatures_management('sites', 'in_memory/selected')
  
      # count total and completed
      total = 0
      completed = 0
      siteFields = ['SITE_STATUS']
      with arcpy.da.SearchCursor('in_memory/selected', siteFields) as cursor2:
        for row2 in cursor2:
          status = row2[0]
          total += 1
          if status == '****':
            completed += 1
  
      row[1] = total
      row[2] = completed
  
      cursor.updateRow(row)
  
print 'Finished'     

Thanks a in advance,

Michael

3 Replies
XanderBakker
Esri Esteemed Contributor

You may want to consider the following sequence:

After defining the edit object, use:

edit.startEditing(False, True) # check what configuration applied to your specific situation
edit.startOperation()

# your edit operation

edit.stopOperation()
edit.stopEditing(True)

See: http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/editor.htm

MichaelSouffront
New Contributor III

Sorry for the late reply.

We had tried something similar with the .startediting method parameters set both to True. I tried your suggestion and it didn't work either.

We ended up using a windows task as an alternative to want we needed. I would still like to find out why it didn't work, though.

Thanks,

Michael

JennaWalz2
New Contributor II

Hello,

Has anyone figured this out? I am having the same problem and I have added this.

edit.startEditing(FalseTrue# check what configuration applied to your specific situation  

edit.startOperation()  

  

# your edit operation  

  

edit.stopOperation()  

edit.stopEditing(True

Thanks,

Jenna Walz

0 Kudos