Goroutines

Note that when you run these, the numbers come in in different orderbetween runs.

In the Python example, it exits automatically when all requests havefinished.

Python

  1. import urllib2
  2. import multiprocessing
  3.  
  4.  
  5. def f(url):
  6. req = urllib2.urlopen(url)
  7. try:
  8. print len(req.read())
  9. finally:
  10. req.close()
  11.  
  12.  
  13. urls = (
  14. "http://www.peterbe.com",
  15. "http://peterbe.com",
  16. "http://htmltree.peterbe.com",
  17. "http://tflcameras.peterbe.com",
  18. )
  19.  
  20. if __name__ == '__main__':
  21. p = multiprocessing.Pool(3)
  22. p.map(f, urls)

Go

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. )
  8.  
  9. func f(url string) {
  10. response, err := http.Get(url)
  11. if err != nil {
  12. panic(err)
  13. }
  14. defer response.Body.Close()
  15. body, err := ioutil.ReadAll(response.Body)
  16. if err != nil {
  17. panic(err)
  18. }
  19.  
  20. fmt.Println(len(body))
  21. }
  22.  
  23. func main() {
  24. urls := []string{
  25. "http://www.peterbe.com",
  26. "http://peterbe.com",
  27. "http://htmltree.peterbe.com",
  28. "http://tflcameras.peterbe.com",
  29. }
  30. for _, url := range urls {
  31. go f(url)
  32. }
  33. // necessary so it doesn't close before
  34. // the goroutines have finished
  35. var input string
  36. fmt.Scanln(&input)
  37. }