Noelia
Noelia

Reputation: 3

How to know if a process is still running in Python?

I have been doing some research on this, but all the questions (in my newbie understanding) seem to address how to figure out if ANOTHER program is running, not Python itself.

So, I am currently working on a code that generates buffers around a set of lines in a shapefile. It is a batch process that involves20 shapefiles and up to 4000 lines inside them. The process is taking a really long time, and I am starting to wonder if it is actually working. I usually use print statements to keep track of the progress of my code, but in this case I am calling ArcGIS to do the function so the only print statement I can write is one AFTER all the lines have been processed, not while it's still running.

Is there any way to find out if the process is still going on (other than checking the Task Manager to see if it's frozen)?

crazy way of doing it I thought of (no idea if something like this is possible): I was thinking of something like writing something in a txt file every X number of minutes as long as the script hasn't finished running.

Thanks!!!!

My code:

def Buffer30m(self,InputFile,OutputBuffer, size):    
   arcpy.Buffer_analysis(InputFile,OutputBuffer,size,"FULL","ROUND","NONE","#")


TheList=os.listdir(SSFlinespath)    #read the files in the folder
os.mkdir(SSFlines+"SSFbuffers"      #create folder for the output
SSbuff=SSFlinespath+"SSFbuffers/"   #select the folder as destination 
try:
    size=30
    for TheFile in TheList:                                          #Identify each file
        TheFileName, TheFileExtension = os.path.splitext(TheFile)    #Break up the file name
        if (TheFileExtension==".shp"):                               #Identify the shapefiles
            TheLines=SSFlines+TheFile
            ##Generate a 30m buffer around the lines
            TheBuffer=SSFbuff+TheFileName+"_buff30.shp"
            TheStepBuffer.Buffer30m(TheLines,TheBuffer,size)
            print "SSF buffer done"  
 except Exception as TheError:
    print "Error with the SSF forest area calculation"
    print TheError

UPDATE Partial solution, complete code and new issue:

I applied qwwqwwq's suggestion, which works great for the function I mentioned before. The thing now is that when I run the rest of my script (what I showed before was just part of it so you wouldn't have to read so much) some of the other functions don't work.

Here is the complete script:

class StepBuffer:
 def GetPairInfo (self, MainFile, SourceFile, WantFields, SSF):
    fields= WantFields   #Fields I will keep
    ##Extract the info and add to the main table
    if SSF==False:
        GRHE_proj.GetFieldInfo(MainFile,SourceFile,"Pair", "Pair", fields)
    elif SSF==True:
        GRHE_proj.GetFieldInfo(MainFile,SourceFile,"SAMPLEID", "SAMPLEID", fields)

 def Buffer30m(self,InputFile,OutputBuffer, size):
    arcpy.Buffer_analysis(InputFile,OutputBuffer,size,"FULL","ROUND","NONE","#")

 def AreaBuff (self,InputFile, OutputTable):      
    arcpy.CalculateAreas_stats(InputFile,OutputTable)

 def AreaForest (self,BufferFile,ForestFile, OutputTable, SSF):
    if SSF==False:
        arcpy.TabulateIntersection_analysis(BufferFile,"Pair",ForestFile,OutputTable,"type","#","#","HECTARES")
    elif SSF==True:
        arcpy.TabulateIntersection_analysis(BufferFile,"SAMPLEID",ForestFile,OutputTable,"type","#","#","HECTARES")



TheList=os.listdir(SSFlinespath)    #read the files in the folder
os.mkdir(SSFlines+"SSFbuffers"      #create folder for the output
SSbuff=SSFlinespath+"SSFbuffers/"   #select the folder as destination

try:
    size=30
    for TheFile in TheList:                                          #Identify each file
    TheFileName, TheFileExtension = os.path.splitext(TheFile)    #Break up the file name
    if (TheFileExtension==".shp"):                               #Identify the shapefiles
        TheLines=SSFlines+TheFile
        ##Generate a 30m buffer around the lines
                t = threading.Thread(target=TheStepBuffer.Buffer30m, args=(TheLines,TheBuffer,size))
                t.start() 
                while (t.is_alive()):
                    time.sleep(2)  ## sleep so that we don't execute the print statement too often
                    print "I'm alive!"                  
                print "SSF buffer done"


                ##Calculate area of buffer
                TableAreaBuff=OutputTables+TheFileName+"_Area_Buffers.dbf"         
                TheStepBuffer.AreaBuff(TheBuffer,TableAreaBuff)
                print "SSF area buffer done"

                ##Calculate the area of the forest inside the buffer
                TableAreaFor=OutputTables+TheFileName+"_Area_forest.dbf"
                TheStepBuffer.AreaForest(TheBuffer,ForestPath,TableAreaFor,True)
                print "SSF area forest done"

                ##Add info of the area of the buffer to the buffer layer
                TheStepBuffer.GetPairInfo(TheBuffer,TableAreaBuff, "F_AREA",True)

                ##Add info of area of forest within the buffers to the buffer layer
                TheStepBuffer.GetPairInfo(TheBuffer,TableAreaFor,["Area","Percentage"],True)
                print TheFileName+"Done"
    except Exception as TheError:
        print "Error with the SSF forest area calculation"
        print TheError
        TheErrorFile.writelines(format(TheError)+"\n")

Specifically, the error appears when it gets to :

TheStepBuffer.AreaForest(TheBuffer,ForestPath,TableAreaFor,True)

producing this error:

arcgisscripting.ExecuteError:
Traceback (most recent call last):
File "c:\program files (x86)\arcgis\desktop10.1\
ArcToolbox\Scripts\CalculateAreas.py", line 76, in <module>
setupCalcAreas()
File "c:\program files (x86)\arcgis\desktop10.1\ArcToolbox\Scripts\CalculateAreas.py", line 31, in setupCalcAreas
calculateAreas(inputFC, outputFC)
File "c:\program files (x86)\arcgis\desktop10.1\ArcToolbox\Scripts\CalculateAreas.py", line 55, in calculateAreas
cnt = UTILS.getCount(inputFC)
File "c:\program files (x86)\arcgis\desktop10.1\ArcToolbox\Scripts\SSUtilities.py", line 502, in getCount
return int(countObject.getOutput(0))
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 986, in getOutput ... (truncated)
return convertArcObjectToPythonObject(self._arc_object.GetOutput(*gp_fixargs(args))) ... (truncated)
File "C:\Users\Noelia Volpe\Dropbox\Project\analisis\Python_Codes\Scripts\MovementPref\volpe_project_v2.py", line 464, in <module>
  TheStepBuffer.AreaBuff(TheBuffer,TableAreaBuff)
File "C:\Users\Noelia Volpe\Dropbox\Project\analisis\Python_Codes\ReUsable\Movement_preferences.py", line 58, in AreaBuff
  arcpy.CalculateAreas_stats(InputFile,OutputTable)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\stats.py", line 1564, in CalculateAreas
  raise e

Does anyone know what might be the source of this error? Thanks!

Upvotes: 0

Views: 11134

Answers (1)

qwwqwwq
qwwqwwq

Reputation: 7329

I would try using multithreading:

import threading
import time
t = threading.Thread(target=TheStepBuffer.Buffer30m, args=(TheLines,TheBuffer,size))
t.start()

while (t.is_alive()):
    time.sleep(2)  ## sleep so that we don't execute the print statement too often
    print "I'm alive!"

Your thread will execute the function TheStepBuffer.Buffer30m, and the control thread will monitor that thread to make sure that it is still executing your target function, executing a print statement to let you know that all is well.

Upvotes: 2

Related Questions