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