我一直在对此进行一些研究,但所有问题(在我的新手理解中)似乎都在解决如何判断是否正在运行另一个程序,而不是Python本身。
所以,我目前正在开发一个代码,它在shapefile中的一组行周围生成缓冲区。这是一个批处理过程,涉及20个shapefile,内部最多4000行。这个过程花了很长时间,我开始怀疑它是否真的有效。 我通常使用print语句来跟踪代码的进度,但在这种情况下我调用ArcGIS来执行该功能,因此我可以编写的唯一打印语句是在所有行已经处理之后,而不是在它仍在运行时
有没有办法找出进程是否仍在进行(除了检查任务管理器是否被冻结)?
疯狂的做法我想到了(不知道这样的事情是否可行):只要脚本没有完成运行,我就会想到每隔X分钟在txt文件中写一些东西。感谢!!!!
我的代码:
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
更新 部分解决方案,完整代码和新问题:
我应用了qwwqwwq的建议,这对我之前提到的功能非常有用。现在的事情是,当我运行我的其余脚本时(我之前展示的只是其中的一部分,所以你不必阅读那么多)其他一些功能不起作用。
这是完整的脚本:
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")
具体来说,错误出现在:
TheStepBuffer.AreaForest(TheBuffer,ForestPath,TableAreaFor,True)
产生此错误:
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
有谁知道这个错误的来源可能是什么? 谢谢!
答案 0 :(得分:2)
我会尝试使用多线程:
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!"
你的线程将执行函数TheStepBuffer.Buffer30m
,控制线程将监视该线程以确保它仍在执行你的目标函数,执行print
语句让你知道所有的都是好。