0

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
User1986
  • 175
  • 10

2 Answers2

1

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.

Emil105
  • 160
  • 6
1

You have a typo in threading.Thread(target=request(line)).start() You can also replace it with Thread(target=lambda line=line: request(line))