goroutine

I have to send out thousands of reminders, any way to avoid a ticker every minute?

落爺英雄遲暮 提交于 2020-02-02 14:43:27
问题 I have a struct like: type Notifications struct { Id int Start *time.Time } notifications := db.GetNotifications() So now I need to send out these notifications whenever the time matches the current time. 1 2018-11-07 09:05:00 2 2018-11-07 09:05:00 3 2018-11-07 09:15:00 .. The simplest way for me to do this is with a ticker: ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() for { <-ticker.C alerts := []Notification for _, n := range notifications { if n.Start == // same year,

Why Goroutine Can Get Scheduled After Busy Loop?

梦想的初衷 提交于 2020-01-30 04:07:35
问题 See these code below. I am not doing this in any production, just for studying purpose. I have heard from many posters that busy loop usually prevents from scheduling because they leave no chance for the go sheduler to scheduler. If this is true why deadloop() goroutine can gets scheduled?? I am using golang 1.12 and testing on windows os. func main() { go deadloop() // v1 -- keeps printing forever var i =1 for { i++ } } func deadloop() { i := 0 for { fmt.Printf("from deadloop\n") i++ } }

Why Goroutine Can Get Scheduled After Busy Loop?

醉酒当歌 提交于 2020-01-30 04:05:38
问题 See these code below. I am not doing this in any production, just for studying purpose. I have heard from many posters that busy loop usually prevents from scheduling because they leave no chance for the go sheduler to scheduler. If this is true why deadloop() goroutine can gets scheduled?? I am using golang 1.12 and testing on windows os. func main() { go deadloop() // v1 -- keeps printing forever var i =1 for { i++ } } func deadloop() { i := 0 for { fmt.Printf("from deadloop\n") i++ } }

How to handle errors and terminate Goroutine using WaitGroup

隐身守侯 提交于 2020-01-25 08:49:06
问题 I have been playing around with Goroutines, Channels and WaitGroup today and I am finally starting to understand the concept, after just been reading about it for a while. My problem is that I am unsure how I handle errors when working like this, mainly because of the WaitGroup I use. When using the WaitGroup, I start by adding the amount of goroutines that will be executed, but what if an error happens during one of these? package main import ( "errors" "sync" ) var waitGroup sync.WaitGroup

sync.WaitGroup and nested loops

你离开我真会死。 提交于 2020-01-25 06:49:13
问题 I'd like to add concurrency for iterating nested loops but having a trouble. What's wrong with this example usage of sync.WaitGroup? originCities := [3]string{"LED", "MOW", "PRS"} destinationCities := [2]string{"UKT", "AAC"} wg := &sync.WaitGroup{} wg.Add(len(originCities) * len(destinationCities)) for _, originIata := range originCities { for _, destinationIata := range destinationCities { go func () { fmt.Println(originIata) fmt.Println(destinationIata) wg.Done() }() } } wg.Wait() I'm

Best way to use HTTP client in a concurrent application

百般思念 提交于 2020-01-23 04:49:07
问题 First I'll describe my case. I have to do HTTPS requests to several APIs from my application and they should be ran concurrently. I want to know if I should use a separate HTTP client per goroutine or I can share one client across all goroutines. Of course I'd like to enjoy connection reusing/pooling offered by the HTTP client, but I am concerned about it being thread(aka goroutine)-safe and if the client will run requests concurrently or they'll in fact be sequenced? 回答1: Http clients are

Are goroutines garbage collected together with their channels?

巧了我就是萌 提交于 2020-01-14 08:59:10
问题 Imagine the following code: func waitForOneOfTwoProcesses() { c := make(chan bool) go func() { time.Sleep(1 * time.Second) c<-true }() go func() { time.Sleep(2 * time.Second) c<-true }() <-c } Does this leak a channel and a goroutine or does Go recognize that c is gone and the goroutine can exit? Would the answer be any different if the channel had a buffer size of 2? 回答1: If the channel is unbuffered, then one of the anonymous functions will not return. The program leaks a goroutine and

Goroutines 8kb and windows OS thread 1 mb

会有一股神秘感。 提交于 2020-01-11 10:24:27
问题 As windows user, I know that OS threads consume ~1 Mb of memory due to By default, Windows allocates 1 MB of memory for each thread’s user-mode stack. How does golang use ~8kb of memory for each goroutine , if OS thread is much more gluttonous. Are goroutine sort of virtual threads? 回答1: Goroutines are not threads, they are (from the spec): ...an independent concurrent thread of control, or goroutine , within the same address space. Effective Go defines them as: They're called goroutines

Why does this code cause data race?

左心房为你撑大大i 提交于 2020-01-11 10:20:11
问题 1 package main 2 3 import "time" 4 5 func main() { 6 m1 := make(map[string]int) 7 m1["hello"] = 1 8 m1["world"] = 2 9 go func() { 10 for i := 0; i < 100000000; i++ { 11 _ = m1["hello"] 12 } 13 }() 14 time.Sleep(100 * time.Millisecond) 15 m2 := make(map[string]int) 16 m2["hello"] = 3 17 m1 = m2 18 } I run command go run --race with this code and get: ================== WARNING: DATA RACE Read at 0x00c420080000 by goroutine 5: runtime.mapaccess1_faststr() /usr/local/go/src/runtime/hashmap_fast

goroutines order of execution

余生长醉 提交于 2020-01-10 04:15:08
问题 I'm trying to understand this piece of code, not sure why the 2nd go is executed before the 1st one. It'd be great if someone can really help me out with this! func sum(a []int, c chan int) { fmt.Println("summing: ", a) total := 0 for _, v := range a { total += v } //fmt.Println("send to c",total) c <- total // send total to c } func main() { //a := []int{7, 2, 8,134,23,23,1,23,1234,143, -9, 4, 0, 1234} c := make(chan int) go sum([]int{1,2,3}, c) go sum([]int{4,5,6}, c) x := <-c fmt.Println(x