Which one of the two is idiomatic way? time.Sleep() or ticker?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have to execute some statements say every minute. I am not sure which one of the following I should follow. It would be great if someone can explain the pros and cons in terms of memory and CPU.
time.Sleep()
func main()
go func()
for
time.Sleep(time.Minute)
fmt.Println("Hi")
()
time.Sleep(10 * time.Minute) //just to keep main thread running
Or Ticker
func main()
go func()
for _ = range time.Tick(time.Minute)
fmt.Println("Hi")
()
time.Sleep(10 * time.Minute) //just to keep main thread running
go
add a comment |
I have to execute some statements say every minute. I am not sure which one of the following I should follow. It would be great if someone can explain the pros and cons in terms of memory and CPU.
time.Sleep()
func main()
go func()
for
time.Sleep(time.Minute)
fmt.Println("Hi")
()
time.Sleep(10 * time.Minute) //just to keep main thread running
Or Ticker
func main()
go func()
for _ = range time.Tick(time.Minute)
fmt.Println("Hi")
()
time.Sleep(10 * time.Minute) //just to keep main thread running
go
add a comment |
I have to execute some statements say every minute. I am not sure which one of the following I should follow. It would be great if someone can explain the pros and cons in terms of memory and CPU.
time.Sleep()
func main()
go func()
for
time.Sleep(time.Minute)
fmt.Println("Hi")
()
time.Sleep(10 * time.Minute) //just to keep main thread running
Or Ticker
func main()
go func()
for _ = range time.Tick(time.Minute)
fmt.Println("Hi")
()
time.Sleep(10 * time.Minute) //just to keep main thread running
go
I have to execute some statements say every minute. I am not sure which one of the following I should follow. It would be great if someone can explain the pros and cons in terms of memory and CPU.
time.Sleep()
func main()
go func()
for
time.Sleep(time.Minute)
fmt.Println("Hi")
()
time.Sleep(10 * time.Minute) //just to keep main thread running
Or Ticker
func main()
go func()
for _ = range time.Tick(time.Minute)
fmt.Println("Hi")
()
time.Sleep(10 * time.Minute) //just to keep main thread running
go
go
edited Nov 15 '18 at 9:09
Sagar Zala
2,41151438
2,41151438
asked Nov 15 '18 at 9:01
drjoenhdrjoenh
112
112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
From the docs:
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.
time.Sleep
just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.
The ticker takes the execution time of the provided block into account and skips an interval, if necessary.
Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.
In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.
In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.
To me it's also significant that theTicker
code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). TheSleep
code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.
– Adrian
Nov 15 '18 at 14:12
add a comment |
In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315716%2fwhich-one-of-the-two-is-idiomatic-way-time-sleep-or-ticker%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the docs:
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.
time.Sleep
just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.
The ticker takes the execution time of the provided block into account and skips an interval, if necessary.
Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.
In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.
In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.
To me it's also significant that theTicker
code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). TheSleep
code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.
– Adrian
Nov 15 '18 at 14:12
add a comment |
From the docs:
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.
time.Sleep
just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.
The ticker takes the execution time of the provided block into account and skips an interval, if necessary.
Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.
In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.
In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.
To me it's also significant that theTicker
code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). TheSleep
code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.
– Adrian
Nov 15 '18 at 14:12
add a comment |
From the docs:
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.
time.Sleep
just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.
The ticker takes the execution time of the provided block into account and skips an interval, if necessary.
Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.
In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.
In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.
From the docs:
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.
time.Sleep
just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.
The ticker takes the execution time of the provided block into account and skips an interval, if necessary.
Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.
In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.
In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.
edited Nov 15 '18 at 14:10
answered Nov 15 '18 at 9:05
mbuechmannmbuechmann
3,07121429
3,07121429
To me it's also significant that theTicker
code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). TheSleep
code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.
– Adrian
Nov 15 '18 at 14:12
add a comment |
To me it's also significant that theTicker
code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). TheSleep
code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.
– Adrian
Nov 15 '18 at 14:12
To me it's also significant that the
Ticker
code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep
code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.– Adrian
Nov 15 '18 at 14:12
To me it's also significant that the
Ticker
code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep
code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.– Adrian
Nov 15 '18 at 14:12
add a comment |
In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.
add a comment |
In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.
add a comment |
In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.
In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.
answered Nov 15 '18 at 9:09
MagiqMagiq
213
213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315716%2fwhich-one-of-the-two-is-idiomatic-way-time-sleep-or-ticker%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown