Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to process parallel HTTP request in go programming language?
    primarykey
    data
    text
    <p>I was playing with the go HTTP package. I wanted to process request in parallel as I do in java. But I couldn't.</p> <p>I created a simple web server, put a sleep in the middle and realized that go process one request per time, so if I did a refresh on my browser, the process of the first request has to finish until the second request start processing, here is the code:</p> <pre><code>func main(){ //Process the http commands fmt.Printf("Starting http Server ... ") http.Handle("/", http.HandlerFunc(sayHello)) err := http.ListenAndServe("0.0.0.0:8080", nil) if err != nil { fmt.Printf("ListenAndServe Error",err) } } func sayHello(c http.ResponseWriter, req *http.Request) { fmt.Printf("New Request\n") processRequest(c, req) } func processRequest(w http.ResponseWriter, req *http.Request){ time.Sleep(time.Second*3) w.Write([]byte("Go Say’s Hello(Via http)")) fmt.Println("End") } </code></pre> <p>As I wanted to process both request in parallel I added the "go" command before "processRequest(c, req)" in "sayHello" function in order to process each request in a different gorutine. But... it doesn't work.... I don't know why. I know that both request are processed because I see the printed line at the console but the browser keep waiting for information..... and don't show my response.</p> <p>So... my questions, </p> <p>Does each request create a new http.ResponseWriter? or it's use the same? Do you know how to indicate the web server to process each request with different threads?</p> <p>Any help is welcomed....</p> <p>Fersca</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload