2015-11-15 02:46
浏览 505


I'm need to run a function at specific times of the day (e.g. 0010, 0610, 1210, 1810). My current approach uses a ticker for _ = range time.Tick(21600 * time.Second) and I manually launch the program at one of these intervals (e.g 1210). This is obviously sub-optimal.

What's the best solution to this? I thought of running the ticker every 60 seconds and then checking to see if the time matched one of the intervals, but that doesn't seem very elegant.

图片转代码服务由CSDN问答提供 功能建议

我需要在一天的特定时间(例如0010、0610、1210、1810)运行一个函数 。 我目前的方法是使用代码表示_ =范围time.Tick(21600 * time.Second),然后以这些间隔之一(例如1210)手动启动程序。 这显然是次优的。

对此的最佳解决方案是什么? 我想到了每60秒运行一次股票行情,然后检查时间是否与时间间隔之一匹配,但这似乎并不十分优雅。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douna1892 2015-11-15 03:08

    What you're really after here is a scheduler, so your options are:

    • Limit your program to just what you want done at each interval, but use an existing scheduler to do the scheduling - a simple example would be to use a cron job (or for Windows, Task Scheduler) to launch your program according to the required schedule

    • Turn your program into a scheduler in its own right, which invokes the required function correctly on schedule. Your program would then ideally need to run as a daemon (service, on Windows), or be launched once and left running all the time somehow

    打赏 评论

相关推荐 更多相似问题