Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python的多线程下载循环_Python_Multithreading_Loops_Fetch - Fatal编程技术网

使用python的多线程下载循环

使用python的多线程下载循环,python,multithreading,loops,fetch,Python,Multithreading,Loops,Fetch,我有一张单子 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

我有一张单子

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()