2011-02-01 76 views
3

我有一個列表。多線程python下載循環

symbols = ('GGP', 'JPM', 'AIG', 'AMZN','GGP', 'rx', 'jnj', 'osip') 

URL = "http://www.Xxxx_symbol=%s" 

def fetch(symbols): 
    try: 
     url = URL % '+'.join(symbols) 
     fp = urllib2.urlopen(url) 
     try: 
      data = fp.read() 

     finally: 
      fp.close() 
     return data 
    except Exception as e: 
     print "No Internet Access" 

我想多線程(與4線程)取數據進程,而不是多進程和不使用扭曲。 Url fetch的輸出文件是csv,帶有7行標題信息,我想擺脫它。我想循環每個符號在它自己的文件中。我以前使用過這個提取碼。我可以得到一個有一個元素的符號列表。

回答

4

這應該讓你開始:

from threading import Thread, Lock 

data = {} 
data_lock = Lock() 

class Fetcher(Thread): 
    def __init__(self, symbol): 
     super(Thread, self).__init__() 
     Thread.__init__(self) 
     self.symbol = symbol 

    def run(self): 
     # put the code from fetch() here 
     # replace 'data = fp.read()' with the following 
     tmp = fp.read() 
     data_lock.acquire() 
     data[self.symbol] = tmp 
     data_lock.release() 

# Start a new Fetcher thread like this: 
fetcher = Fetcher(symbol) 
fetcher.start() 
# To wait for the thread to finish, use Thread.join(): 
fetcher.join()