멀티 다운로드에 스레딩을 사용하는 파이썬 방법

M_Sea :

내가 사용 해요 threading지금은 URL_LIST이 병렬 다운로드를 할 img_list내가 너무 데프이 다운로드 2 스레드에서 다운로드 할를.

난에 반 넣어 download1의 절반 download2이 완료까지 속도 있도록,하지만 난 아직 일련의 스크립트 내 다운로드를 실행하면 마지막으로, 내가, 내가 어떻게 내 스크립트를 수정할 수 있습니다 이유를 몰라?

여기에 코드입니다 :

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()
괴물 :

이 라인에서

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

당신은 전화 download_1(...)및 스레드에 결과 (널)를 전달합니다. 이 순차적으로 실행되는 이유입니다. 대신 당신이 전달하려는 download_1스레드에 함수 자체 (하지를 호출 한 결과를). 이 같이 :

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

두 곳 모두에서 작업을 수행합니다.

사이드 노트 : 당신이해야 t.join()끝에 두 스레드.

추천

출처http://43.154.161.224:23101/article/api/json?id=373084&siteId=1