How to run a function periodically every minute, on the minute.

blov · · 780 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Is there a way to run a function every minute, on the minute?</p> <p>The following code will run every minute, however maybe halfway through the minute if that&#39;s when I executed it.</p> <pre><code>ticker := time.NewTicker(5 * time.Second) quit := make(chan struct{}) go func() { for { select { case &lt;- ticker.C: // do stuff case &lt;- quit: ticker.Stop() return } } }() </code></pre> <p>However I want this code to run at 7:00:00, 7:01:00, 7:02:00, and so on. Is there any trick to this? Thanks.</p> <hr/>**评论:**<br/><br/>captncraig: <pre><p>The time package has a <code>Truncate</code> function for this.</p> <p>Something like:</p> <pre><code>nextTime := time.Now().Truncate(time.Minute) for{ nextTime = nextTime.Add(time.Minute) // wait till then or quit } </code></pre></pre>silviucm: <pre><p>Perhaps this tried and true package can give you some further leads: <a href="https://github.com/robfig/cron" rel="nofollow">https://github.com/robfig/cron</a></p> <p>In particular: <a href="https://github.com/robfig/cron/blob/master/constantdelay.go" rel="nofollow">https://github.com/robfig/cron/blob/master/constantdelay.go</a></p></pre>catkins88: <pre><p>Wow, that package has immaculate godocs, but nothing in the README</p></pre>EnragedMikey: <pre><p>cron/crontab?</p></pre>curiousGambler: <pre><p>Yeah if nothing else needs to happen in the meantime, this is the answer </p></pre>LimEJET: <pre><p>Wait for a specific point in time, then start ticking.</p></pre>mcandre: <pre><p><a href="https://github.com/mcandre/toys/blob/master/go/hello-timers/main.go" rel="nofollow">https://github.com/mcandre/toys/blob/master/go/hello-timers/main.go</a></p> <p>Adjust timer period as needed.</p></pre>zeiko_is_back: <pre><p><a href="https://play.golang.org/p/fFahno03qr" rel="nofollow">https://play.golang.org/p/fFahno03qr</a></p> <pre><code>package main import ( &#34;fmt&#34; &#34;time&#34; ) func main() { for i := 0; i &lt; 10; i++ { runOn(func() { fmt.Println(time.Now()) }, 1 * time.Second) } } func runOn(f func(), d time.Duration) { now := time.Now() d = now.Add(d).Truncate(d).Sub(now) time.Sleep(d) f() } </code></pre> <p>````</p></pre>shark1337: <pre><p>Using Sleep in your code is generally a bad idea..</p></pre>squiiid: <pre><p>Could you expand more on this? I&#39;ve ran into the same issue where I needed to perform an <a href="https://gist.github.com/nhatbui/61629eb54ca09afe3c88abd4aa524530" rel="nofollow">action every <code>X</code> seconds</a>. Timers with channels was overkill: the process was already done in it&#39;s own goroutine. I went with <code>Sleep</code>.</p></pre>ijustwantaredditacct: <pre><p>It&#39;s uninterruptable -- I tend to write a function like: </p> <pre><code>func sleep(ctx context.Context, timer &lt;-chan time.Time) bool { select { case &lt;-timer: return true case &lt;-ctx.Done(): return false } } </code></pre> <p>this lets the sleep be interrupted if the context is cancelled, and is handy in loops:</p> <pre><code>for sleep(ctx, timer.C) { // timer triggered, do stuff } </code></pre></pre>shark1337: <pre><p>No need for Sleep when working with channels. There&#39;s sync.Waitgroup{} that gets the job done..</p></pre>alan0098: <pre><p>Ticking per second, do stuff only on 00 second. Otherwise, skip.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

780 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传