Reputation: 35
I am working on a Youtube downloader script and met difficulty in multi->threading . I put the urls needs to be downloaded in a queue and get them one by >one with new thread spawning. Below is my code and the error are within the commant. >Please advise.
from queue import Queue
#put all the urls in 'q'
q = Queue()
with open(r'c:\ProgramData\youtube_download\122.txt', 'r') as f :
for url in f.readlines():
url = url.strip()
q.put(url) #totally 18 urls are stored in the queue
def My_download( url_d):
yt = YouTube(url_d)
video = yt.get('mp4', '720p')
video.download(r'C:/Users/abc/Desktop') # The default video directory
def main():
while not q.empty():
url_d = q.get()
q.task_done()
## print(str(q.qsize()) + url_d) >>until this step, everything works as expected
t = Thread(target = My_download, args = url_d) #>>TypeError: My_download() takes 1 positional argument but 92 were given
t.start()
q.join()
if __name__ == "__main__":
main()
Upvotes: 1
Views: 1662
Reputation: 2930
Change to
t = Thread(target = My_download, args = (url_d,))
From the docs
args is the argument tuple for the target invocation. Defaults to ().
When you send pass a string instead of tuple, it unpacks the string to a tuple of n elements(92 in your case, and so the error)
Upvotes: 1
Reputation: 78790
In
Thread(target = My_download, args = url_d)
args
is the argument sequence for the invocation of target
. The arguments are unpacked when calling My_download
. For example,
Thread(target = My_download, args = [1, 2, 3])
would try to call My_download(1, 2, 3)
eventually. This triggers the the TypeError
because your function takes exactly one argument. To prevent the unpacking, you can wrap args
in another sequence of length one, e.g.
Thread(target = My_download, args = [url_d])
or modify My_download
such that it accepts an arbitrary number of arguments by using the *args
syntax.
Upvotes: 1