简体   繁体   English

自动化Python脚本

[英]Automate Python Script

I'm running a python script manually that fetches data in JSON format.How do I automate this script to run automatically on an hourly basis? 我正在手动运行python脚本,该脚本以JSON格式获取数据。如何自动将此脚本每小时自动运行一次?

I'm working on Windows7.Can I use tools like Task scheduler?If I can use it,what do I need to put in the batch file? 我正在Windows7上工作。我可以使用Task Scheduler之类的工具吗?如果可以使用,我需要在批处理文件中放入什么?

Can I use tools like Task scheduler? 我可以使用任务计划程序之类的工具吗?

Yes. 是。 Any tool that can run arbitrary programs can run your Python script. 任何可以运行任意程序的工具都可以运行Python脚本。 Pick the one you like best. 选择最喜欢的一个。

If I can use it,what do I need to put in the batch file? 如果可以使用,我需要在批处理文件中放入什么?

What batch file? 什么批处理文件? Task Scheduler takes anything that can be run, with arguments—a C program, a .NET program, even a document with a default app associated with it. Task Scheduler可以带参数运行任何可以运行的东西-C程序,.NET程序,甚至是具有关联的默认应用程序的文档。 So, there's no reason you need a batch file. 因此,没有理由需要批处理文件。 Use C:\\Python33\\python.exe (or whatever the appropriate path is) as your executable, and your script's path (and its arguments, if any) as the arguments. 使用C:\\Python33\\python.exe (或任何适当的路径)作为可执行文件,并使用脚本的路径(及其参数(如果有))作为参数。 Just as you do when running the script from the command line. 就像从命令行运行脚本时一样。

See Using the Task Scheduler in MSDN for some simple examples, and Task Scheduler Schema Elements or Task Scheduler Scripting Objects for reference (depending on whether you want to create the schedule in XML, or via the scripting interface). 有关一些简单示例,请参见在MSDN中使用Task Scheduler ,以及Task Scheduler架构元素Task Scheduler脚本对象作为参考(取决于您是否要以XML创建时间表,还是通过脚本接口)。

You want to create an ExecAction with Path set to "C:\\Python33\\python.exe" and Arguments set to "C:\\MyStuff\\myscript.py" , and a RepetitionPattern with Interval set to "PT1H" . 您要创建一个ExecAction ,其Path设置为"C:\\Python33\\python.exe"Arguments设置为"C:\\MyStuff\\myscript.py" ,一个RepetitionPatternInterval设置为"PT1H" You should be able to figure out the rest from there. 您应该能够从那里找出其余的内容。

As sr2222 points out in the comments, often you end up scheduling tasks frequently, and needing to programmatically control their scheduling. 就像sr2222在评论中指出的那样,您经常会频繁安排计划任务,并且需要以编程方式控制其计划。 If you need this, you can control Task Scheduler's scripting interface from Python, or build something on top of Task Scheduler, or use a different tool that's a bit easier to get at from Python and has more helpful examples online, etc.—but when you get to that point, take a step back and look at whether you're over-using OS task scheduling. 如果需要,您可以从Python控制Task Scheduler的脚本接口,或在Task Scheduler上构建一些东西,或使用其他工具,该工具从Python那里可以轻松访问并且在线获得更多有用的示例,等等。您到了这一步,退后一步,看看是否过度使用了OS任务调度。 (If you start adding delays or tweaking times to make sure the daily foo1.py job never runs until 5 minutes after the most recent hourly foo0.py has finished its job, you're over-using OS task scheduling—but it's not always that obvious.) (如果您开始添加延迟或调整时间以确保每日foo1.py作业直到最近的每小时foo0.py完成其作业之后的5分钟才运行,那么您就过度使用了OS任务调度,但并非总是如此很明显。)

May I suggest WinAutomation or AutoMate . 我可以建议WinAutomation还是AutoMate These two do the exact same thing, except the UI is a little different. 两者的功能完全相同,只是UI有所不同。 I prefer WinAutomation, because the scripts are a little easier to build. 我更喜欢WinAutomation,因为脚本更易于构建。

Yes, you can use the Task Scheduler to run the script on an hourly bases. 是的,您可以使用任务计划程序按小时运行脚本。

To execute a python script via a Batch File, use the following code: 要通过批处理文件执行python脚本,请使用以下代码:

start path_to_python_exe path_to_python_file

Example: 例:

start C:\Users\harshgoyal\AppData\Local\Continuum\Anaconda3\python.exe %UserProfile%\Documents\test_script.py

If python is set as Window's Environment Window then you can reduce the syntax to: 如果将python设置为Window的Environment Window,则可以将语法简化为:

start python %UserProfile%\Documents\test_script.py

What I generally do is run the batch file once via Task Scheduler and within the python script I call a thread/timer every hour. 我通常要做的是通过Task Scheduler运行一次批处理文件,并在python脚本中每小时调用一个线程/计时器。

class threading.Timer(interval, function, args=None, kwargs=None) class threading.Timer(interval,function,args = None,kwargs = None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM