rictic (owner)

Revisions

gist: 33001 Download_button fork
public
Public Clone URL: git://gist.github.com/33001.git
Embed All Files: show embed
multi_get.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from threading import Thread, enumerate
from urllib import urlopen
from time import time
 
class URLThread(Thread):
    def __init__(self,url):
        super(URLThread, self).__init__()
        self.url = url
        self.response = None
        self.setDaemon(True)
 
    def run(self):
        self.request = urlopen(self.url)
        self.response = self.request.read()
 
def multi_join(threads, timeout=None):
    for thread in threads:
        start = time()
        thread.join(timeout)
        if timeout is not None:
            timeout -= time() - start
    return threads
    
def multi_get(uris,timeout=2.0):
    def alive_count(lst):
        count = 0
        for x in lst:
            if x.isAlive():
                count += 1
        return count
    threads = [ URLThread(uri) for uri in uris ]
    for thread in threads:
        thread.start()
    return [ (x.url, x.response) for x in multi_join(threads, timeout)]