Hi all!
My Question:
Any advice on running multiple python scripts each evening to automate geoprocessing tools?
My Problem:
I am trying to improve my organizations python scripts. We have multiple SDEs and multiple scripts connecting to these SDEs all running nightly via a batch file and windows task scheduler. We have arrived at a point where we do not have any more time in the evening to add more scripts.
Each script establishes a lock with one or more SDEs, so only one script can run at a time. This seems really inefficient to me, but i am not sure how to do things otherwise. We would receive errors almost every night relating to 'lock' issues before we added the portion in the script that disconnects other users and denies connections to the databases (until the script ends).
We have arcPro, and i have heard that you can create models in there and have them run nightly as well, but i am uncertain if this would be more efficient or just a lateral move.
The complexity of each script varies but they all follow a general outline which is:
1. import modules
2. create log
3. disconnect users from sde to establish lock
4. run geoprocessing logic
5. allow connections again.
here is some sample code:
import arcpy
import logging
import datetime
import time
import sys
import os
# create log
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(message)s")
file_handler = logging.FileHandler("C:....<location of log file>logFile.log")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
arcpy.env.overwriteOutput = True
# Current Day
Day = time.strftime("%m-%d-%Y", time.localtime())
# Current Time
Time = time.strftime("%I:%M:%S %p", time.localtime())
# Local variables:
sde_1 = r"C:..<path to sde>...FIRST_SDE.sde"
sde_2 = r"C:..<path to sde>...SECOND_SDE.sde"
input_fc = r"C:..<path to sde>...FIRST_SDE.sde\FeatureClassInput"
output_fc = r"C:..<path to sde>...FIRST_SDE.sde\FeatureClassOutput"
logger.info("\n"*4)
try:
# Get a list of connected users to sde_1 and sde_2
userList_sde_1 = arcpy.ListUsers(sde_1)
userList_sde_2 = arcpy.ListUsers(sde_2)
# Pull names of current connections
userNames_sde_1 = [uL.Name for uL in userList_sde_1]
logger.info("These users are connected to sde_1.sde: " +
str(userNames_sde_1))
# Pull names of current connections
userNames_sde_2 = [uL.Name for uL in userList_sde_2]
logger.info("These users are connected to sde_2.sde: " +
str(userNames_sde_2))
# No longer accept connections to the databases
arcpy.AcceptConnections(sde_1, False)
logger.info("sde_1.sde is no longer accepting connections")
arcpy.AcceptConnections(sde_2, False)
logger.info("sde_2.sde is no longer accepting connections")
# Disconnect all users from the databases
arcpy.DisconnectUser(sde_1, "ALL")
print("disconnecting sde_1 users")
logger.info("Disconnecting sde_1 users" + str(userNames_sde_1))
arcpy.DisconnectUser(sde_2, "ALL")
print("disconnecting sde_2 users")
logger.info("Disconnecting sde_2 users" + str(userNames_sde_2))
except:
logger.exception(
"There is an error with disconnecting users from databases")
try:
# Start Time
logger.info('Process Started at ' + str(Day) + " " + str(Time))
# arcpy buffer tool
arcpy.Buffer_analysis(input_fc, output_fc, "50 METERS")
logger.info("Select Parcels \n")
#other geoprocessing tools go here
#....
#....
#....
#....
#....
#....
except:
logger.exception("There is an error with the arcpy portion of the script")
#Check to see if it was a lock issue....
userList_sde_1_afterDisconnect = arcpy.ListUsers(sde_1)
userList_sde_2_afterDisconnect = arcpy.ListUsers(sde_2)
# Check connections again for sde_1
userNames_sde_1 = [uL.Name for uL in userList_sde_1_afterDisconnect]
logger.info(
"These users are connected to sde_1 despite disconnect code: " + str(userNames_sde_1))
# Check connections again for sde_2
userNames_sde_2 = [uL.Name for uL in userList_sde_2_afterDisconnect]
logger.info(
"These users are connected to sde_2 despite disconnect code: " + str(userNames_sde_2))
try:
# Accept new connections to the database
arcpy.AcceptConnections(sde_1, True)
logger.info("sde_1 is accepting connections")
arcpy.AcceptConnections(sde_2, True)
logger.info("sde_2 is accepting connections")
except:
logger.exception(
"There is an error with allowing the databases to accept connections again")
logger.info("End of Script")
file_handler.close()