goroutine

Parallel processing in golang

≡放荡痞女 提交于 2019-12-04 02:42:16
Given the following code: package main import ( "fmt" "math/rand" "time" ) func main() { for i := 0; i < 3; i++ { go f(i) } // prevent main from exiting immediately var input string fmt.Scanln(&input) } func f(n int) { for i := 0; i < 10; i++ { dowork(n, i) amt := time.Duration(rand.Intn(250)) time.Sleep(time.Millisecond * amt) } } func dowork(goroutine, loopindex int) { // simulate work time.Sleep(time.Second * time.Duration(5)) fmt.Printf("gr[%d]: i=%d\n", goroutine, loopindex) } Can i assume that the 'dowork' function will be executed in parallel? Is this a correct way of achieving

Does Go have something like ThreadLocal from Java?

自作多情 提交于 2019-12-04 00:05:32
I use Go and Gin to setup my website and want to know the database access time. I use goroutine so if don't use something like thread-local, I must change almost every function to do it. Does Go have a good way to do it? The Go runtime and standard libraries do not provide goroutine local storage or a goroutine identifier that can be used to implement goroutine local storage. The third party gls package implements goroutine local storage in an interesting way. Some find this package horrifying and others think it's clever. The Go team recommends passing context explicitly as function argument

Time response for HTTP GET request when using goroutines

不问归期 提交于 2019-12-03 22:01:56
I have a simple code that prints GET response time for each URL listed in a text file (url_list.txt). When the requests are fired sequentially the returned times correspond to the expected response times of individual URLs. However, when the same code is executed concurrently the returned response times are typically higher than expected. It seems that the time_start I capture before the http.Get(url) is called is not the time of when the request is actually sent. I guess the execution of http.Get(url) is queued to some extend. Is there a better way to capture URL response time when using

All goroutines are asleep - deadlock! ----— Error

人走茶凉 提交于 2019-12-03 17:37:27
I want to write three concurrent go routines that sends integers to each other. Now, my code is compiled properly, however after first execution it gives error "all goroutines are asleep - deadlock!". I tried to find the error but I could not able to find any error in code logic.Can anybody help me to find the mistake with my code. My code is given below. Thanks in advance. package main import "rand" func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) { for i := 0; i < 10; i++ { y := rand.Intn(10) if y%2 == 0 { command12 <- y } if y%2 != 0 {

On windows, is it possible to run a single goroutine as a different user?

做~自己de王妃 提交于 2019-12-03 17:26:59
How do you delegate the running of a goroutine to another non administrator account on windows? I see you can do this on Linux using syscall.Setuid() . I can't see how to do this on Windows using the windows syscall package. I'd like to be able to set the account the goroutine runs under while the program is running. Is this possible? Bit of background :- I want to switch the user that runs the goroutine so I can change the OS User passed to Oracle during the database connection when I use go-oci8 (See my other question ). I need to connect to the database and it uses the logged in user (OS

Golang context.WithValue: how to add several key-value pairs

帅比萌擦擦* 提交于 2019-12-03 08:26:01
问题 With Go's context package it is possible to pass request-specific data to the stack of request handling functions using func WithValue(parent Context, key, val interface{}) Context This creates a new Context which is a copy of parent and contains the value val which can be accessed with key. How do I proceed if I want to store several key-value pairs in a Context ? Shall I call WithValue() several times, each time passing the Context received from my last call to WithValue() ? This appears

Context timeout implementation on every request using golang

此生再无相见时 提交于 2019-12-03 07:40:02
问题 I am trying to handle context timeout for every request. We have following server structures: Flow overview: Go Server : Basically, acts as a [Reverse-proxy].2 Auth Server : Check for requests Authentication. Application Server : Core request processing logic. Now if Authorization server can't able to process a request in stipulated time, then I want to close the goroutine from memory. Here is what I tried: ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel()

How to lock/synchronize access to a variable in Go during concurrent goroutines?

吃可爱长大的小学妹 提交于 2019-12-03 07:20:24
问题 In his answer to this question: Golang for Windows erratic behavior? user @distributed recommended to lock/synchronize access to a shared variable on concurrent goroutines. How can I do that? More on the issue: I get this code (the returned function with a closure on views ) running on several goroutines at the same time: func makeHomeHandler() func(c *http.Conn, r *http.Request) { views := 1 return func(c *http.Conn, r *http.Request) { fmt.Fprintf(c, "Counting %s, %d so far.", r.URL.Path[1:]

Wait for the termination of n goroutines

半城伤御伤魂 提交于 2019-12-03 07:19:31
I need to start a huge amount of goroutines and wait for their termination. The intuitive way seems to use a channel to wait till all of them are finished : package main type Object struct { //data } func (obj *Object) Update(channel chan int) { //update data channel <- 1 return } func main() { channel := make(chan int, n) list := make([]Object, n, m) for { for _, object := range list { go object.Update(channel) } for i := 0; i < n; i++ { <-channel } //now everything has been updated. start again } } But the problem is that the amount of objects and therefore the amount of goroutines could

How can we use channels in Go in place of mutex?

假装没事ソ 提交于 2019-12-03 05:41:18
Channels combine communication—the exchange of a value—with synchronization—guaranteeing that two calculations (goroutines) are in a known state. How is it possible to use the channels in Google Go to perform the functionality of mutex? package main import "sync" var global int = 0 var m sync.Mutex func thread1(){ m.Lock() global = 1 m.Unlock() } func thread2(){ m.Lock() global = 2 m.Unlock() } func main(){ go thread1() go thread2() } An example of using a Channel as a Mutex: package main var global int = 0 var c = make(chan int, 1) func thread1(){ <-c // Grab the ticket global = 1 c <- 1 //