设为首页 收藏本站
查看: 580|回复: 0

[经验分享] 定时执行Python脚本或模型

[复制链接]

尚未签到

发表于 2017-4-29 08:17:36 | 显示全部楼层 |阅读模式

At the Spatial Analysis and Geoprocessing island at this year’s user conference, several folks asked us about running a Python script or ModelBuilder model at a prescribed time — usually in the early morning when their computers are bored and just waiting for
something to do. We thought it would be good to make a post about this and to share some tips.


The first place to start is with the help topicScheduling
a Python script to run at prescribed times. This topic is kind of buried in the help system so it’s easy to miss. The meat of this topic shows you how to launch Window’sTask
Scheduleron various operating systems (Windows 7, XP, and so on). The screenshot below shows theTask Scheduleron Windows 7. There’s a lot
stuff you can do with the task scheduler and you’ll just have to play around with it and read the Windows help. TheActionspane, on the right,
has theCreate Basic Taskaction, and this is the place to start.


DSC0000.png


Clicking onCreate Basic Taskopens a wizard where you define the name of your task, thetrigger(when
it runs), and theaction(what program to run). The screenshot below shows theActiontab
where you specify the name of your Python script to run as well as any arguments to the script.


DSC0001.png


TheActiontab is where we have a few tips, as follows.



Run the Python executable with arguments



To ensure that your Python script will run regardless of the login account that the schedule task uses, and to avoid any confusion about which version of Python is used in mixed environments (64bit or 32bit), we recommend that you run the Python executable
with the name of your Python file as an argument to the executable.


Suppose the script you want to run isE:\My script.py. Instead
of running the script directly, instruct the task scheduler to runpython.exewith
the script as an argument. For example:


C:\Python27\ArcGIS10.2\python.exe "E:\My script.py"


The location ofpython.exedepends on your install. If you
don’t know where it is, you can discover its location; copy and paste the following code into a new Python script then execute the script. The script will print the location ofpython.exeas
well as other information about your Python environment.


import sys
import platform
import imp
print("Python EXE     : " + sys.executable)
print("Architecture   : " + platform.architecture()[0])
print("Path to arcpy  : " + imp.find_module("arcpy")[1])
raw_input("\n\nPress ENTER to quit")





After determining the location ofpython.exe, this is what
is entered in theActionpanel of the task scheduler:
DSC0002.png


If there are additional arguments (parameters) to your script, provide them after the path to your script. For example, the following command line executes a Python script that takes two arguments; the path to a feature class and an integer.


C:\Python27\ArcGIS10.2\python.exe "E:\My Scripts\DoConversion.py" c:\gisWork\gdb.mdb\counties 10We typically recommend writing scripts so that they take arguments like the above example rather the hard-coding dataset paths or values within the script. However, this is one case where hard-coding paths and values in your script works to
your advantage; rather than changing argument values in the task scheduler, it’s easier to just edit your script to use different paths or values.




If you’re familiar with.batfiles in Windows, you can have
the task scheduler run a.batfile that in turn runs your
script with arguments. The contents of the.batis the single
command line shown above.


Remember that if any of your arguments contain spaces, you need to enclose the argument in double quotes. In the above example,E:\My
Scripts\DoConversion.pycontains a space, so double quotes are required.



Running models at a prescribed time



DSC0003.png


A common misconception is that in order to run a model as a Python script, you must first export the model to a Python script. Geoprocessing is designed so that models are tools, just like all the tools delivered with the ArcGIS. That means you can run your
model within Python just like any other geoprocessing tool. All you need to do is import the toolbox containing your model using theImportToolboxfunction,
then run your model within Python.


For example, the model to the left imports a.gpxfile to
a geodatabase, adds a field, calculates it to the current date and time, then appends all those features into a “master” feature class.


To run this model on a schedule, all that’s needed is to execute the model within a script. Here’s the Python script to run the model:




importarcpy
importos
fromdatetime importdatetime
# Import the toolbox containing the model.  This toolbox
#  has an alias of "gpxtools"
#
arcpy.ImportToolbox(r"c:\importgpx\myGPXstuff.tbx")
# Run the model.  The model has two parameters, the input
#  .gpx file and the feature class to update
#
arcpy.ImportGPX_gpxtools(r"c:\ftp\inbox\datadrop.gpx",
r"c:\ftp\server.sde\gpx_tracks")
# Now that is has been processed, rename input gpx
#   to have the date and time.
#
os.rename(r"c:\ftp\inbox\datadrop.gpx",
r"c:\ftp\inbox\datadrop{}.gpx".format(datetime.strftime(datetime.now(),"%Y%m%d"))





Advanced options



TheCreate Basic Taskwizard allows you to set basic properties of the task. Once the task is created, you can examine its properties and refine its triggers and actions. For
example, you can have your task run every five minutes for three hours. You can set up a task with advanced options using theCreate Taskaction.



Start simple



When I started writing this blog, it had been a long time since I messed around with scheduled tasks, so I started with a simple script that wrote information to a log file. I scheduled it to run every minute for ten minutes so I could test changes to the script
soon after I made them. It was a great way to test and understand how scheduling worked before committing to running the “real” script. Here’s my simple script:




importsys
importplatform
importimp
print"Importing arcpy... this may take a moment\n"
importarcpy
# Open a log file to write to
#
f=open(r'E:\schedule.log','w')
# Write date/time
#
f.write(time.strftime('%x %X'))
f.write('\n')
# Test receiving of arguments/parameters via arcpy
#
f.write("Parameter 0 : " + arcpy.GetParameterAsText(0)+"\n")
# Python info
#
f.write("Python EXE : " + sys.executable +"\n")
f.write("Architecture : " + platform.architecture()[0]+"\n")
f.write("Path to arcpy : " + imp.find_module("arcpy")[1]+"\n")

Ghislain Prince contributed to this post

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370584-1-1.html 上篇帖子: python inspect模块解析python inspect模块解析 下篇帖子: Python Lover(6)Twisted and Basic
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表