0

如果我问这个问题时看起来像个菜鸟,那是因为这是我第一次尝试多线程。对不起。

看到多线程是为了使过程更快,我试图找出我在哪里出错了。我注意到的第一件事是“主线程”线程比它应该的要慢得多(它是唯一不使用代理的线程)。

在进程是多线程之前,它会在单个进程/线程中在一分钟内下载大约 10-15 个图像。但是现在,“多线程”(它是唯一不通过代理发送 HTTP 请求的)真的从未显示(如您所见: http: //puu.sh/3KfwY.png)。它应该是最快的线程!

我希望我清楚地解释了我的问题。:S

PS:程序启动时,15秒内下载约20张图片;但是,在初始启动后,程序会急剧变慢。关于为什么会这样的任何想法?

#!/usr/bin/env python
import sys
import string
import os.path
import urllib.request
import threading
from time import gmtime, strftime, sleep
from random import choice

#list of our proxies
proxies = []

downloads = 1

#the number of files we want to download
target = int(sys.argv[1])

#argument 2 - proxies
try:
    sys.argv[2]
except:
    print('')
else:
    param = sys.argv[2]

    if param.find('.txt') != -1:
        print('Loading specified proxy list ('+ param +').')

        f = open(param, 'r+')
        print('Opening '+ f.name)
        proxylist = f.read()
        f.close()

        #split retrieved list by new line
        proxies = proxylist.split('\n')
    else:
        print('Single proxy specified.')
        proxies.append(param)


class thread(threading.Thread):
    def __init__(self, ID, name, proxy):
        threading.Thread.__init__(self)
        self.id = ID
        self.name = name
        self.downloads = 0
        self.proxy = proxy
        self.running = True
        self.fails = 0
    def run(self):
        global downloads

        if self.proxy != False:
            #id is always above one, so make the ID -1
            self.proxy = proxies[(self.id-1)]

            print(self.name +' initiating with proxy: '+self.proxy)

            #make our requests go through proxy
            self.p_handler = urllib.request.ProxyHandler({'http' : self.proxy})
            self.opener = urllib.request.build_opener(self.p_handler)
            urllib.request.install_opener(self.opener)
        else:
            print(self.name +' initiating without a proxy.')

        while downloads <= target and self.running:
            rstr = ''.join(choice(string.ascii_letters + string.digits) for x in range(5))

            url = 'http://puu.sh/'+rstr
            filename = 'downloaded/'+ strftime('%Y %m %d %H-%M-%S', gmtime()) +'.png';

            try:
                urllib.request.urlretrieve(url, filename)
            except urllib.request.HTTPError:
                pass
            except IOError:
                if self.fails > 10:
                    print(self.name +': Proxy is not working. Stopping thread.')
                    self.running = False

                self.fails += 1
            except:
                pass
            else:
                print(self.name +': downloading '+ filename+'...' + str(downloads))

                #if self.downloads % 10 == 0:
                #    print(self.name +': Sleeping a bit.')
                #    sleep(60)

                downloads += 1
                self.downloads += 1
                sleep(5)

#lets create the "downloaded" folder if it does not exist
if not os.path.isdir('downloaded'):
    try:
        os.mkdir('downloaded')
    except:
        pass

#thread count
thread_count = 1

#create threads, and initiate them
try:
    thread(0, 'main-thread', False).start()

    for x in proxies:
        thread(thread_count, 'Thread-'+str(thread_count), proxies[(thread_count-1)]).start()
        thread_count += 1
except:
    print('Couldn\'t start threads.')
4

1 回答 1

0

我不知道实际问题是什么,但我注意到的一件事是您使用urllib.request.install_opener. 这设置了一个默认的全局开启器,因此每个线程都会看到更改。很可能在第一次通过时,大多数线程都使用分配给它们的代理,但在那之后,所有请求都转到同一个代理,并且您会受到代理或其他东西的限制。

sleep(5)样子怪怪的。这很容易成为你的问题。

您正在使用没有锁定的共享全局变量。大多数时候它可能不会咬你,但你最终可能会遇到奇怪的失败,除非你正确地同步所有来自不同线程的对可变状态的同时访问。

于 2013-07-24T03:09:08.537 回答