即使使用多线程,为什么不快?

时间:2017-02-24 07:50:14

标签: python multithreading download

我使用下面的代码用于多线程下载图像,但我发现不是那么快,我不知道为什么,有没有人为我赎罪?

正常下载:

for imageSecond in imageSeconds:
    urlServer = imageSecond.get("src")
    pathLocal = formatPath(downloadLocationPath, ntpath.basename(urlServer))
    downloadImage(browser, urlServer, pathLocal)    

def downloadImage(browser, urlServer, pathLocal):

线程下载:

ts = []
for imageSecond in imageSeconds:
    urlServer = imageSecond.get("src")
    pathLocal = formatPath(downloadLocationPath, ntpath.basename(urlServer))
    ts.append(createNewDownloadThread(browser, urlServer, pathLocal))

for t in ts:
    t.join()

def createNewDownloadThread(browser, urlServer, pathLocal):
    download_thread = threading.Thread(target=downloadImage, args=(browser, urlServer, pathLocal))
    download_thread.start()
    return download_thread#.join()

def downloadImage(browser, urlServer, pathLocal):

1 个答案:

答案 0 :(得分:-1)

使用以下代码

ts = []

for:
    t = createThread()
    t.start()
    ts.append(t)

for t in ts:
    t.join()

代替代码

for:
    t = createThread()
    t.start()
    t.join()