【发布时间】:2019-07-09 13:25:44
【问题描述】:
我正在制作一个从网页下载图像的脚本,并且我正在尝试使其成为多线程的,因此速度要快得多。
在下载功能中,我必须设置两个参数,因为当我设置一个(队列)时,我得到了这个错误:
TypeError: downloading() takes 1 positional arguments but 21* were given
** queue has 21 links
代码:
count = 0
queue = {"some urls", , , }
done = set()
path = 'foldername'
def downloading(queue, name):
for imgs in queue:
if imgs not in done:
done.add(imgs)
urllib.request.urlretrieve(imgs, path + '/' + imgs.split('/')[-1])
global count
count += 1
print(str(count) + ' ' + name)
print('Done: ' + imgs.split('/')[-1])
def threads(queue):
print('Start Downloading ...')
th1 = Thread(target=downloading, args=(queue, "Thread 1"))
th1.start()
th2 = Thread(target=downloading, args=(queue, "Thread 2"))
th2.start()
th3 = Thread(target=downloading, args=(queue, "Thread 3"))
th3.start()
th4 = Thread(target=downloading, args=(queue, "Thread 4"))
th4.start()
th5 = Thread(target=downloading, args=(queue, "Thread 5"))
th5.start()
th6 = Thread(target=downloading, args=(queue, "Thread 6"))
th6.start()
th7 = Thread(target=downloading, args=(queue, "Thread 7"))
th7.start()
th8 = Thread(target=downloading, args=(queue, "Thread 8"))
th8.start()
th9 = Thread(target=downloading, args=(queue, "Thread 9"))
th9.start()
th10 = Thread(target=downloading, args=(queue, "Thread 10"))
th10.start()
【问题讨论】:
-
您可以使用
for-loop创建所有线程并将它们保留在列表中th[1].start() -
你确定你没有使用
args=queue吗?或args=queue, "Thread 1"没有()? -
@furas 以及如何制作这个
for-loop?我使用了args=queue和args=(queue)两者都是一样的,我认为集合是这里的问题,就像它把每个元素作为一个参数而不是一个集合一样。Traceback (most recent call last): File "C:\Users\nulla\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner self.run() File "C:\Users\nulla\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run self._target(*self._args, **self._kwargs) TypeError: downloading() takes 1 positional argument but 21 were given -
@furas 没关系,我只是想通了,我不知道我应该把
,放在争论之后。让我们回到for-loop -
您运行相同的代码 10 次 - 您可以创建列表
all_threads = [ ]并使用for x in range(10):运行t = Thread(...)t.start()、all_threas.append(t)。这样您的代码更少,您可以更改range(10)以运行更多线程。但是更多的线程并不一定意味着更快。 Python 使用 GIL 阻止线程并且它们不会同时运行。最好使用可以同时下载的ThreadPool或者grequests。
标签: python python-3.x multithreading loops for-loop