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

2024-05-15 12:27:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

正常下载:

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

Tags: inbrowsersrcforgetdownloaddefthread
2条回答

Python docs

join(timeout=None) Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.

因此,您正在创建一个线程,等待它完成下载,然后为后续下载创建另一个线程,该线程与原始解决方案基本相同

更新您可以使用^{},它提供了简单的界面:

import concurrent.futures as cf

def square(x):
    return x*x

with cf.ThreadPoolExecutor(10) as executor:
    futures = [executor.submit(square, i) for i in range(5)]
    cf.wait(futures)

    for f in futures:
        print(f.result())

输出:

0
1
4
9
16

对于您的代码,它看起来像这样:

NUM_THREADS = 10
with cf.ThreadPoolExecutor(NUM_THREADS) as executor:
    futures = []
    for imageSecond in imageSeconds:
        urlServer = imageSecond.get("src")
        pathLocal = formatPath(downloadLocationPath, ntpath.basename(urlServer))
        futures.append(executor.submit(downloadImage, browser, urlServer, pathLocal))

    cf.wait(futures)

使用如下代码

ts = []

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

for t in ts:
    t.join()

代替代码

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

相关问题 更多 >