使用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个线程)来获取进程,而不是多进程并且不使用Twisted. Url fetch的输出文件是csv,其中包含我要删除的7行标题信息.我想循环每个符号在它自己的文件中.我以前使用过此提取代码.我可以获得包含一个元素的符号列表.

解决方法:

这应该使您开始:

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()
上一篇:python-在theano中扫描张量的不同维度


下一篇:c#-对于循环,应用程序挂起