Home > Mobile >  How to use threading to run the same proccess multiple times with specified data
How to use threading to run the same proccess multiple times with specified data

Time:01-09

I want to make a script to download files from links from a text file. It works with this code:

import urllib.request
import random
import threading

def request(line):
    urllib.request.urlretrieve(line, 'D:\\example_directory\\'   str(random.randint(1000000, 9999999))   "-69"   str(random.randint(100, 999))   "-"   str(random.randint(1000000, 9999999))   ".mp4")

with open('D:\\example_directory\\links.txt') as f:
    for line in f:
        print(line)
        threading.Thread(target=request(line)).start()

This code works, but it doesn't start to download the second link before the first one finished downloading. This is a problem for reasons that dont need to be specified.

CodePudding user response:

You have to replace the last line with threading.Thread(target=request, args=(line,)).start(). In your code the request is executed before the Thread object is even created.

  •  Tags:  
  • Related