Golang for select loop consumes 100% of cpuHow to determine CPU and memory consumption from inside a process?How to get current CPU and RAM usage in Python?How to create a CPU spike with a bash commandHow to obtain the number of CPUs/cores in Linux from the command line?Profiled app with YourKit, still can't identify the CPU hogIs there a foreach loop in Go?High CPU on IIS sitesGolang code to increase cpu usagec# and ironpython CPU performance issuesYour CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Translate the beginning of the blessing "Asher Yatzar"
Why are prop blades not shaped like household fan blades?
What is the term for completing a route uncleanly?
How to innovate in OR
What is the range of a Drunken Monk's Redirect attack?
If I buy and download a game through second Nintendo account do I own it on my main account too?
What parameters are to be considered when choosing a MOSFET?
Why does macOS create file mounts for each app?
How does Asimov's second law deal with contradictory orders from different people?
Correct word for a little toy that always stands up?
Should students have access to past exams or an exam bank?
Password management for kids - what's a good way to start?
Why do we need a voltage divider when we get the same voltage at the output as the input?
Word for soundtrack music which is part of the action of the movie
Why are we moving in circles with a tandem kayak?
How to remove rebar passing through an inaccessible pipe
Word for giving preference to the oldest child
Was Donald Trump at ground zero helping out on 9-11?
How would a lunar colony attack Earth?
What to expect in a jazz audition
Should 2FA be enabled on service accounts?
Should I put my name first or last in the team members list?
Applying for mortgage when living together but only one will be on the mortgage
integration of absolute value
Golang for select loop consumes 100% of cpu
How to determine CPU and memory consumption from inside a process?How to get current CPU and RAM usage in Python?How to create a CPU spike with a bash commandHow to obtain the number of CPUs/cores in Linux from the command line?Profiled app with YourKit, still can't identify the CPU hogIs there a foreach loop in Go?High CPU on IIS sitesGolang code to increase cpu usagec# and ironpython CPU performance issuesYour CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a resource that needs to be loaded before any access is allowed to it. It also needs to be updated every minute.
The channels are of length 1 struct, so the loop will be blocked if the resource has not been loaded.
This code below started to use 100% of my cpu, I tried adding
time.Sleep(10 * time.Millisecond)
Which made the application cpu consumption drop to 1%
I am thinking a ticker would be a better option for the timed collection.
Any ideas why it would consume 100% cpu or any better ideas for implemention?
func (al *AsyncLoop) Run()
go func()
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
else if al.lastUpdated.Add(1*time.Minute).Unix() < time.Now().Unix() && al.fetching == false
go al.fetchData()
al.chanFetchResponse <- struct
continue
default:
continue
()
go cpu
add a comment |
I have a resource that needs to be loaded before any access is allowed to it. It also needs to be updated every minute.
The channels are of length 1 struct, so the loop will be blocked if the resource has not been loaded.
This code below started to use 100% of my cpu, I tried adding
time.Sleep(10 * time.Millisecond)
Which made the application cpu consumption drop to 1%
I am thinking a ticker would be a better option for the timed collection.
Any ideas why it would consume 100% cpu or any better ideas for implemention?
func (al *AsyncLoop) Run()
go func()
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
else if al.lastUpdated.Add(1*time.Minute).Unix() < time.Now().Unix() && al.fetching == false
go al.fetchData()
al.chanFetchResponse <- struct
continue
default:
continue
()
go cpu
5
You have a default case in your select, meaning that the for loop will spin if there is nothing to read fromal.al.chanFetchRequest
.
– Tim Cooper
Mar 26 at 22:46
1
Also looking at howal.lastUpdated
,scp.fetching
andgo al.fetchData()
there is also a chance you have data race in this code as well.
– zerkms
Mar 26 at 22:59
Yes thanks for spotting that, I will add some get and set methods with a mutex
– Charles Bryant
Mar 26 at 23:25
add a comment |
I have a resource that needs to be loaded before any access is allowed to it. It also needs to be updated every minute.
The channels are of length 1 struct, so the loop will be blocked if the resource has not been loaded.
This code below started to use 100% of my cpu, I tried adding
time.Sleep(10 * time.Millisecond)
Which made the application cpu consumption drop to 1%
I am thinking a ticker would be a better option for the timed collection.
Any ideas why it would consume 100% cpu or any better ideas for implemention?
func (al *AsyncLoop) Run()
go func()
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
else if al.lastUpdated.Add(1*time.Minute).Unix() < time.Now().Unix() && al.fetching == false
go al.fetchData()
al.chanFetchResponse <- struct
continue
default:
continue
()
go cpu
I have a resource that needs to be loaded before any access is allowed to it. It also needs to be updated every minute.
The channels are of length 1 struct, so the loop will be blocked if the resource has not been loaded.
This code below started to use 100% of my cpu, I tried adding
time.Sleep(10 * time.Millisecond)
Which made the application cpu consumption drop to 1%
I am thinking a ticker would be a better option for the timed collection.
Any ideas why it would consume 100% cpu or any better ideas for implemention?
func (al *AsyncLoop) Run()
go func()
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
else if al.lastUpdated.Add(1*time.Minute).Unix() < time.Now().Unix() && al.fetching == false
go al.fetchData()
al.chanFetchResponse <- struct
continue
default:
continue
()
go cpu
go cpu
edited Mar 26 at 23:09
Charles Bryant
asked Mar 26 at 22:43
Charles BryantCharles Bryant
4331 gold badge7 silver badges18 bronze badges
4331 gold badge7 silver badges18 bronze badges
5
You have a default case in your select, meaning that the for loop will spin if there is nothing to read fromal.al.chanFetchRequest
.
– Tim Cooper
Mar 26 at 22:46
1
Also looking at howal.lastUpdated
,scp.fetching
andgo al.fetchData()
there is also a chance you have data race in this code as well.
– zerkms
Mar 26 at 22:59
Yes thanks for spotting that, I will add some get and set methods with a mutex
– Charles Bryant
Mar 26 at 23:25
add a comment |
5
You have a default case in your select, meaning that the for loop will spin if there is nothing to read fromal.al.chanFetchRequest
.
– Tim Cooper
Mar 26 at 22:46
1
Also looking at howal.lastUpdated
,scp.fetching
andgo al.fetchData()
there is also a chance you have data race in this code as well.
– zerkms
Mar 26 at 22:59
Yes thanks for spotting that, I will add some get and set methods with a mutex
– Charles Bryant
Mar 26 at 23:25
5
5
You have a default case in your select, meaning that the for loop will spin if there is nothing to read from
al.al.chanFetchRequest
.– Tim Cooper
Mar 26 at 22:46
You have a default case in your select, meaning that the for loop will spin if there is nothing to read from
al.al.chanFetchRequest
.– Tim Cooper
Mar 26 at 22:46
1
1
Also looking at how
al.lastUpdated
, scp.fetching
and go al.fetchData()
there is also a chance you have data race in this code as well.– zerkms
Mar 26 at 22:59
Also looking at how
al.lastUpdated
, scp.fetching
and go al.fetchData()
there is also a chance you have data race in this code as well.– zerkms
Mar 26 at 22:59
Yes thanks for spotting that, I will add some get and set methods with a mutex
– Charles Bryant
Mar 26 at 23:25
Yes thanks for spotting that, I will add some get and set methods with a mutex
– Charles Bryant
Mar 26 at 23:25
add a comment |
1 Answer
1
active
oldest
votes
I think you just post to al.chanFetchRequest when there is new data so I think you have to keep reading from this channel all the time. Adding a ticker to the select might cause you to fetch the data even if it has not changed or (worse) before it has even loaded. Why not, in the normal case, take the time whenever you fetch the data then the next time make sure you have waited enough time before fetching again. Something like this:
var nextFetch time.Time
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
else if time.Now().After(nextFetch)
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
al.chanFetchResponse <- struct
A select with a single case has no purpose. Just read from the channel.
– Adrian
Mar 27 at 14:06
@Adrian thanks - you are right which is exactly what I said in my 2nd answer (below). However, it could be useful if you think you will add other cases later.
– AJR
Mar 29 at 7:06
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%2f55367231%2fgolang-for-select-loop-consumes-100-of-cpu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you just post to al.chanFetchRequest when there is new data so I think you have to keep reading from this channel all the time. Adding a ticker to the select might cause you to fetch the data even if it has not changed or (worse) before it has even loaded. Why not, in the normal case, take the time whenever you fetch the data then the next time make sure you have waited enough time before fetching again. Something like this:
var nextFetch time.Time
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
else if time.Now().After(nextFetch)
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
al.chanFetchResponse <- struct
A select with a single case has no purpose. Just read from the channel.
– Adrian
Mar 27 at 14:06
@Adrian thanks - you are right which is exactly what I said in my 2nd answer (below). However, it could be useful if you think you will add other cases later.
– AJR
Mar 29 at 7:06
add a comment |
I think you just post to al.chanFetchRequest when there is new data so I think you have to keep reading from this channel all the time. Adding a ticker to the select might cause you to fetch the data even if it has not changed or (worse) before it has even loaded. Why not, in the normal case, take the time whenever you fetch the data then the next time make sure you have waited enough time before fetching again. Something like this:
var nextFetch time.Time
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
else if time.Now().After(nextFetch)
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
al.chanFetchResponse <- struct
A select with a single case has no purpose. Just read from the channel.
– Adrian
Mar 27 at 14:06
@Adrian thanks - you are right which is exactly what I said in my 2nd answer (below). However, it could be useful if you think you will add other cases later.
– AJR
Mar 29 at 7:06
add a comment |
I think you just post to al.chanFetchRequest when there is new data so I think you have to keep reading from this channel all the time. Adding a ticker to the select might cause you to fetch the data even if it has not changed or (worse) before it has even loaded. Why not, in the normal case, take the time whenever you fetch the data then the next time make sure you have waited enough time before fetching again. Something like this:
var nextFetch time.Time
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
else if time.Now().After(nextFetch)
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
al.chanFetchResponse <- struct
I think you just post to al.chanFetchRequest when there is new data so I think you have to keep reading from this channel all the time. Adding a ticker to the select might cause you to fetch the data even if it has not changed or (worse) before it has even loaded. Why not, in the normal case, take the time whenever you fetch the data then the next time make sure you have waited enough time before fetching again. Something like this:
var nextFetch time.Time
for
select
case <-al.chanFetchRequest:
if al.fetched == false
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
else if time.Now().After(nextFetch)
al.fetchData()
nextFetch = time.Now().Add(1 * time.Minute)
al.chanFetchResponse <- struct
edited Mar 26 at 23:14
answered Mar 26 at 23:07
AJRAJR
13810 bronze badges
13810 bronze badges
A select with a single case has no purpose. Just read from the channel.
– Adrian
Mar 27 at 14:06
@Adrian thanks - you are right which is exactly what I said in my 2nd answer (below). However, it could be useful if you think you will add other cases later.
– AJR
Mar 29 at 7:06
add a comment |
A select with a single case has no purpose. Just read from the channel.
– Adrian
Mar 27 at 14:06
@Adrian thanks - you are right which is exactly what I said in my 2nd answer (below). However, it could be useful if you think you will add other cases later.
– AJR
Mar 29 at 7:06
A select with a single case has no purpose. Just read from the channel.
– Adrian
Mar 27 at 14:06
A select with a single case has no purpose. Just read from the channel.
– Adrian
Mar 27 at 14:06
@Adrian thanks - you are right which is exactly what I said in my 2nd answer (below). However, it could be useful if you think you will add other cases later.
– AJR
Mar 29 at 7:06
@Adrian thanks - you are right which is exactly what I said in my 2nd answer (below). However, it could be useful if you think you will add other cases later.
– AJR
Mar 29 at 7:06
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55367231%2fgolang-for-select-loop-consumes-100-of-cpu%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
5
You have a default case in your select, meaning that the for loop will spin if there is nothing to read from
al.al.chanFetchRequest
.– Tim Cooper
Mar 26 at 22:46
1
Also looking at how
al.lastUpdated
,scp.fetching
andgo al.fetchData()
there is also a chance you have data race in this code as well.– zerkms
Mar 26 at 22:59
Yes thanks for spotting that, I will add some get and set methods with a mutex
– Charles Bryant
Mar 26 at 23:25