Python Runtime Appears to Timeout

678
6
01-13-2021 10:17 AM
PatrickMcKinney1
Occasional Contributor III

I am trying to convert a Model Builder script into a Windows Scheduled Task.  I have broken it into multiple scripts, and then call each script successively from a BAT file.

Each script runs the arcpy.TiledLabelsToAnnotation_cartography tool for a specific scale.  It basically creates annotation feature classes.

The first and last map scales run, but the other scales do not run.  There are also no error messages in my log files (that should write within the except clause).  I initially tried running this as a single script, but it would stop during the second scale (without any messages).

It almost seems like Python is crashing for some reason.  From my log file, it takes about 2 hours for the first scale, and about an hour for the last scale.

Does anybody have any ideas as to what may be happening?  I can share the scripts, if needed.

0 Kudos
6 Replies
JoeBorgione
MVP Esteemed Contributor

perhaps you could post your code up.  It would be nice to capture the error if there is one in your try/except block.  (See https://community.esri.com/t5/community-help-documents/how-to-write-a-good-question-that-will-genera... on how to post your python code)

That should just about do it....
0 Kudos
JeffK
by MVP Regular Contributor
MVP Regular Contributor

Might be that the gdb/files are locked and by the last run it is finally removed from the first script getting done.

You can also start the scripts directly from Task scheduler without going through the bat file to start them.  In the actions tab you can set it to your script and it will execute the script using the default application to you have set for opening .py files (mine is set to python.exe).  Each script can have its own task in scheduler and you can offset the script start times by a minute or so, giving time for the gdb locks to be removed from the previous script running. You can also see the history of the task in the history tab of task scheduler.  I think tasks scheduler's error messages get logged to the stdout so they might be in the event viewer> windows logs, but I agree with Joe- seeing some code and using the try/catch would be easier/helpful to see what is happening.

PatrickMcKinney1
Occasional Contributor III

Here are the scripts.  Nothing in the Except statements run.  I event ran these from command prompt with print statements and left it open.  No error messages.

0 Kudos
JeffK
by MVP Regular Contributor
MVP Regular Contributor

Might be that you are first declaring your log_message in the try block and its not able to get it in the exception blocks. Try moving it above the try, along with any other variable that you want to use within exceptions.

# Import arcpy module
import arcpy, time, datetime, sys

# variable to store messages for log file. Messages written in finally statement at end of script
log_message = ''

try:
    # Time stamp variables
    current_time = datetime.datetime.now()

 

0 Kudos
PatrickMcKinney1
Occasional Contributor III

When I first discovered the issue the first time I ran the entire script as a scheduled task, I found the following information.  I've found Windows Task Scheduler error messages to be cryptic.

In brief, when the detached job crashes, Windows tells the scheduler that the detached process completed with error code 3221225477. The scheduler simply writes "finish" message to the log indicating that the job didn't end with standard exit code 0.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Are you running the scheduled task on a pc or some sort of server?  I've never been able to get Task Scheduler to work on a PC; for me the code was bullet proof but the failure is in task schedular.

Looking at one of your scripts, everybody has their own style of doing things.  I would run it as one script and make each of the individual scripts a def() that is called by main().  I don't see anything wrong with your code per se.  Below is what we use as a basic template for our python scripts.  Each geoprocessing def() has only one except to one try.  We print to the log file no matter what happens, success or failure.

#TEMPLATE to send error emails automatically
#TEMPLATE to insure correct error trapping automatically
#SEE END OF SCRIPT FOR STARTING POINT CODE
# <<<<<<<<<   CONFIG SETTINGS TO BE ENTERED BY TEMPLATE USER

# sample imports
import arcpy
import sys
import os
import smtplib

# sample script level variables/paths   <<<<<<<<<<<HERE          
samplePath = r"\\somePath\arcgisresources\common\geocode\backupLoc"
samplePath2 = r"\\somePath\arcgisresources\common\geocode"

# sample init   HERE
arcpy.env.overwriteOutput = True

def sendEmail(errMessage):   # if errors send email
    from_addr = "errors@slco.org"
    to_addr = ["IS-GISDL@Something.org", "customerEmail@SOmething.org"]  # <<<<< ENTER CUSTOMER EMAIL HERE
    subject = "Errors:   ERROR DESCRIPTION HERE"   # <<<<<<<   ERROR DESC HERE
    msg_text = errMessage
    msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (from_addr, to_addr, subject, msg_text)
    server = "SomeEmailServer.org"
    server = smtplib.SMTP(server)
    server.sendmail(from_addr, to_addr, msg)
    server.quit()


# FUNCTIONS HERE
def functionName(parameter1, parameter2, etc):
    try:
        #<<<<<<<<LOGIC HERE
        print "Success: DESCRIPTION HERE will display in log file" 
    except Exception as err:
        print err
        sendEmail(err)

# MAIN FUNCTION HERE
def main():

    # start log file HERE
    logFile = r"E:\scripts\locators_recreate_publish\logs\locators recreate publish.log"   #<<<<<<<<<< INSERT LOG FILE PATH HERE
    logoutput = open(logFile,'w')
    beginTime = time.strftime('%Y-%m-%d %H:%M:%S')
    logoutput.write("Began at # " + beginTime + "\n")
    sys.stdout = logoutput   #<<<< POINTS ERROR MESSAGES TO BE RECORDED IN LOG FILE, NOT TO THE PYTHON DEBUG SCREEN

    # paths variables <<<<<<<<<<<<HERE
    samplePathFOR_DEF_MAIN = r"\\somePath\arcgisresources\common\geocode\backupLoc"
    samplePath2FOR_DEF_MAIN = r"\\somePath\arcgisresources\common\geocode"



    # SCRIPT LOGIC <<<<<<<<<<<HERE
    functionName(parameter1, parameter2, etc)
    functionName(parameter1, parameter2, etc)
           


    # close log file HERE
    logoutput.write("Ended at # " + time.strftime('%Y-%m-%d %H:%M:%S') + "\n")
    logoutput.close()

    ###end def main
    

# script start HERE, points python.exe etc to use the def main as a starting point
if __name__ == '__main__':  #if def main exists, call it
    main()   #calls def main

 

That should just about do it....
0 Kudos