Example dns_mass_resolve.pyΒΆ

 1#!/usr/bin/python
 2# gevent-test-requires-resource: network
 3"""Resolve hostnames concurrently, exit after 2 seconds.
 4
 5Under the hood, this might use an asynchronous resolver based on
 6c-ares (the default) or thread-pool-based resolver.
 7
 8You can choose between resolvers using GEVENT_RESOLVER environment
 9variable. To enable threading resolver:
10
11    GEVENT_RESOLVER=thread python dns_mass_resolve.py
12"""
13from __future__ import print_function
14import gevent
15from gevent import socket
16from gevent.pool import Pool
17
18N = 1000
19# limit ourselves to max 10 simultaneous outstanding requests
20pool = Pool(10)
21finished = 0
22
23
24def job(url):
25    global finished
26    try:
27        try:
28            ip = socket.gethostbyname(url)
29            print('%s = %s' % (url, ip))
30        except socket.gaierror as ex:
31            print('%s failed with %s' % (url, ex))
32    finally:
33        finished += 1
34
35with gevent.Timeout(2, False):
36    for x in range(10, 10 + N):
37        pool.spawn(job, '%s.com' % x)
38    pool.join()
39
40print('finished within 2 seconds: %s/%s' % (finished, N))

Current source

Next page: Example echoserver.py