Skip to content

Instantly share code, notes, and snippets.

@keith6014
Last active November 6, 2018 01:51
Show Gist options
  • Save keith6014/6a0055125894ac7629a545014f22f765 to your computer and use it in GitHub Desktop.
Save keith6014/6a0055125894ac7629a545014f22f765 to your computer and use it in GitHub Desktop.
goroutines
// http_goroutine project main.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
func get(url string, c chan Result) {
res, err := http.Get(url)
st := time.Now().Format(time.RFC850)
if err != nil {
fmt.Println(err)
}
val, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
fmt.Println(err)
}
c <- Result{Body: string(val), StartTime: st}
}
type Result struct {
Body string
StartTime string
}
func main() {
var endpoints []string = []string{"/sleep", "/sleep", "/sleep", "/sleep", "/sleep"}
var url string = "http://localhost:8080"
c := make(chan Result, 1)
for _, endpoint := range endpoints {
go get(url+endpoint, c)
}
var res []Result
for _, i := range endpoints {
x := <-c
res = append(res, x)
fmt.Println(i)
}
fmt.Println(res)
}
@keith6014
Copy link
Author

The python server looks like this

import cherrypy,time                                                                                                                                                                           
                                                                                                                                                                                               
class HelloWorld(object):                                                                                                                                                                      
                                                                                                                                                                                               
    @cherrypy.expose                                                                                                                                                                           
    def sleep(self):                                                                                                                                                                           
      cherrypy.log("got request at "+str(time.time()))                                                                                                                                         
      time.sleep(2)                                                                                                                                                                            
      cherrypy.log("slept")                                                                                                                                                                    
                                                                                                                                                                                               
cherrypy.server.socket_host = '0.0.0.0' # put it here                                                                                                                                          
cherrypy.quickstart(HelloWorld())          

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment