python:永久执行程序日期相关
我写了一个 script.py 从周一到周五从网上收集数据。该脚本通常是从主函数中的另一个脚本执行的。我希望它在周五关闭并在周一自动打开,并从周一运行到周五。
目前我必须每周一手动运行它。 我写了一些代码来在周五自动停止它。基本上看起来像这样
import sys
import time
if strftime("%a %H:%M", gmtime()) != "Fri 20:00":
...code...
else:
sys.exit()
如何永久运行主脚本并在需要时自动打开其他脚本?请帮助我改进这一点,谢谢。
编辑实际上我会重新表述这个问题:
除了执行以下操作之外,是否还有永久运行脚本的正确方法:
while 1!=0:
...code here
I wrote a script.py collecting data from the web from monday to friday. The script is usually executed from another script in the main function. I want it to close on friday and open monday automatically, and run from monday to friday.
At the moment I am obliged to run it manually every monday.
I wrote some code to stop it automatically on fridays. Basically it looks like this
import sys
import time
if strftime("%a %H:%M", gmtime()) != "Fri 20:00":
...code...
else:
sys.exit()
how to run the main script permanently and open the other script automatically when needed? hep me to improve this please thanks.
EDIT actually I will reformulate the question:
Is there a proper way to run prermanently a script, besides doing:
while 1!=0:
...code here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否可以选择使用 cron 或 Windows 任务计划程序自动安排程序在星期一启动?
或者,您可以编写一个永久运行的单独程序并控制 script.py 的启动和/或关闭。
Do you have any option to automatically schedule the program to start on mondays, with cron or windows task scheduler?
Alternatley, you could write a separate program that runs permanently and controlls the startup and/or shutdown of the script.py.
对于您重新提出的问题,使用
If 这确实是代码需要运行的方式没有任何问题。
如果您愿意,这是可以避免的,您可以通过重组代码来避免它,这样它就不需要在无限循环中运行。没有什么神奇的方法可以让脚本在不使用循环构造的情况下“永远在循环中运行”。
虽然我很想知道。你不喜欢
while 1!=0:
因为说 1!=0 看起来有点傻吗?
while True:
是一个完美的替代方案。
For your reformulated question, theres nothing wrong with using
If that is indeed how the code needs to run.
It is avoidable if you want to, you could avoid it by restructuring your code so that it does not need to run in an infinite loop. There is no magic way to have a script 'keep running in a loop forever' without using a loop construct.
Though I'm wondering. DO you not like
while 1!=0:
because it looks a bit silly to say 1!=0 ?
while True:
Is a perfectly neat alternative.