简体   繁体   English

Python如何在多下载中使用线程

[英]Python how to use Threading in multi download

I am use threading to do parallel download now i have a url_list img_list i want to download it in 2 thread so def two download.我正在使用threading进行并行下载,现在我有一个 url_list img_list我想在 2 个线程中下载它,所以需要两个下载。

i put half into download1 and half in download2 so it will speed up to complete, but finally when i run the scripts my download still in serial,i don't know why, how can i modify my script?我把一半放在download1 ,一半放在download2所以它会加快完成,但最后当我运行脚本时,我的下载仍然是串行的,我不知道为什么,我该如何修改我的脚本?

here's the code:这是代码:

import requests,threading
img_list=[...]
num=len(img_list)
def download_1(img_list):
    n=0
    for i in img_list:
        n+=1
        with open('./img/'+str(n)+'.jpg','wb')as f:
            f.write(requests.get(i).content)
            print(str(n)+"download1 complete")
def download_2(img_list):
    n=len(img_list)
    for i in img_list:
        n+=1
        with open('./img/'+str(n)+'.jpg','wb')as f:
            f.write(requests.get(i).content)
            print(str(n)+"download2 complete")
thread_1 = threading.Thread(target=download_1(img_list[:int(num/2)]))
thread_2 = threading.Thread(target=download_2(img_list[int(num/2):]))
thread_1.start()
thread_2.start()

In this line在这一行

threading.Thread(target=download_1(img_list[:int(num/2)]))

you call download_1(...) and pass the result (null) to thread.您调用download_1(...)并将结果 (null) 传递给线程。 That's why it runs serially.这就是它串行运行的原因。 Instead you want to pass download_1 function itself (not the result of calling it) to the thread.相反,您希望将download_1函数本身(不是调用它的结果)传递给线程。 Like this:像这样:

threading.Thread(target=download_1, args=(img_list[:int(num/2)],))

Do it in both places.在两个地方都做。

Side note: you should t.join() both threads at the end.旁注:你应该在最后t.join()两个线程。

You are calling both functions at the time of creating threads.您在创建线程时调用了这两个函数。 So, threads are passed null and, therefore, do nothing.因此,线程被传递为空,因此什么都不做。 You should change the code like this:您应该像这样更改代码:

thread_1 = threading.Thread(target=download_1, args=(img_list[:int(num/2)]))
thread_2 = threading.Thread(target=download_2, args=(img_list[int(num/2):]))

thread_1.start()
thread_2.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM