what's the difference between time.After & time.Sleep?

4,627 views
Skip to first unread message

Daniel Mal

unread,
Jul 26, 2014, 12:02:49 AM7/26/14
to golan...@googlegroups.com
what's the difference between time.After & time.Sleep?

          <-time.After(sometime) 

looks the same as 

           time.Sleep(timesome)

what are the costs of these two funcs?

Jesse McNelis

unread,
Jul 26, 2014, 12:09:25 AM7/26/14
to Daniel Mal, golang-nuts
On Sat, Jul 26, 2014 at 2:02 PM, Daniel Mal <daniel...@gmail.com> wrote:
> what's the difference between time.After & time.Sleep?
>
> <-time.After(sometime)
>
> looks the same as
>
> time.Sleep(timesome)
>

time.After() gives you a channel that you can use in a select{}

> what are the costs of these two funcs?

time.After() is a bit more expensive because it has to allocate a channel.
You can reduce this cost is you're doing it in a loop by creating a
time.Timer yourself and calling .Reset() on it for each loop.



--
=====================
http://jessta.id.au

Daniel Mal

unread,
Jul 26, 2014, 12:16:45 AM7/26/14
to golan...@googlegroups.com, daniel...@gmail.com, jes...@jessta.id.au
hard to say sleep is cheaper

i ran time.Sleep in a loop for each of 10k goroutines, the system crashed soon, with message

 runtime/cgo: pthread_create failed: Resource temporarily unavailable

when i changed to time.After, it stopped the crash caused by 10k time.Sleep.




在 2014年7月26日星期六UTC+8下午12时09分25秒,Jesse McNelis写道:

Dmitry Vyukov

unread,
Jul 26, 2014, 5:46:01 AM7/26/14
to Daniel Mal, golang-nuts, Jesse McNelis
What version of Go are you using?
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Daniel Mal

unread,
Jul 26, 2014, 6:33:42 AM7/26/14
to golan...@googlegroups.com, daniel...@gmail.com, jes...@jessta.id.au
go 1.3 
macos/darwin64

在 2014年7月26日星期六UTC+8下午5时46分01秒,Dmitry Vyukov写道:

Dmitry Vyukov

unread,
Jul 26, 2014, 6:42:13 AM7/26/14
to Daniel Mal, golang-nuts, Jesse McNelis
then, please show the code
time.Sleep should not cause such crash
also you seem to be using cgo for some reason, how is it involved?

Daniel Mal

unread,
Jul 26, 2014, 7:48:40 AM7/26/14
to golan...@googlegroups.com, daniel...@gmail.com, jes...@jessta.id.au
i wrote a client which spawn 10k client to send packet to server.

this client will crash serveral seconds after spawned all 10k goroutines

i posted my code here


but without the server. 

在 2014年7月26日星期六UTC+8下午6时42分13秒,Dmitry Vyukov写道:

Dmitry Vyukov

unread,
Jul 26, 2014, 8:02:14 AM7/26/14
to Daniel Mal, golang-nuts, Jesse McNelis
I see. The problem is not with time.Sleep/time.After. Then problem is
that the program makes 10K blocking syscalls (probably dns
resolution). time.Sleep/time.After just produce somewhat different
scheduling of goroutines.
You need to understand what exactly syscall causes blocking, it must
be obvious from crash dump. And then bound number of goroutine
simultaneously doing that syscall.

Daniel Mal

unread,
Jul 26, 2014, 8:31:36 AM7/26/14
to golan...@googlegroups.com, daniel...@gmail.com, jes...@jessta.id.au
it seems that linux has no such limitation on blocking system call



在 2014年7月26日星期六UTC+8下午8时02分14秒,Dmitry Vyukov写道:

Dmitry Vyukov

unread,
Jul 26, 2014, 8:42:30 AM7/26/14
to Daniel Mal, golang-nuts, Jesse McNelis
I think that just DNS resolver behaves differently on darwin.
Reply all
Reply to author
Forward
0 new messages