Debian Codegraph @

examples/connect.py

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