connect.py 717 B

12345678910111213141516171819202122232425
  1. """Spawn multiple workers and collect their results.
  2. Demonstrates how to use the eventlet.green.socket module.
  3. """
  4. import eventlet
  5. from eventlet.green import socket
  6. def geturl(url):
  7. c = socket.socket()
  8. ip = socket.gethostbyname(url)
  9. c.connect((ip, 80))
  10. print '%s connected' % url
  11. c.sendall('GET /\r\n\r\n')
  12. return c.recv(1024)
  13. urls = ['www.google.com', 'www.yandex.ru', 'www.python.org']
  14. pile = eventlet.GreenPile()
  15. for x in urls:
  16. pile.spawn(geturl, x)
  17. # note that the pile acts as a collection of return values from the functions
  18. # if any exceptions are raised by the function they'll get raised here
  19. for url, result in zip(urls, pile):
  20. print '%s: %s' % (url, repr(result)[:50])