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;








-1















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


()










share|improve this question





















  • 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 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

















-1















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


()










share|improve this question





















  • 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 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













-1












-1








-1








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


()










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 from al.al.chanFetchRequest.

    – Tim Cooper
    Mar 26 at 22:46






  • 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











  • Yes thanks for spotting that, I will add some get and set methods with a mutex

    – Charles Bryant
    Mar 26 at 23:25












  • 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 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







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












1 Answer
1






active

oldest

votes


















0














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







share|improve this answer



























  • 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










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
);



);













draft saved

draft discarded


















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









0














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







share|improve this answer



























  • 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















0














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







share|improve this answer



























  • 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













0












0








0







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







share|improve this answer















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








share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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








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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript