M_Sea :
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.
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?
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()
freakish :
In this line
threading.Thread(target=download_1(img_list[:int(num/2)]))
you call download_1(...)
and pass the result (null) to thread. That's why it runs serially. Instead you want to pass download_1
function itself (not the result of calling it) to the thread. 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.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=371962&siteId=1