魔道书生 发表于 2020-6-29 19:56

python多线程报错

我的代码
    threads = []
    n=1
    path=a+str(page)+'\\'
    for url in urls:
      t=threading.Thread(target=urllib.request.urlretrieve,args=(url,path+str(n)+'.jpg',))
      threads.append(t)
      n+=1
      for t in threads:
            t.setDaemon(True)
            t.start()
      t.join()

抛出错误
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 640, in http_response
    response = self.parent.error(
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
Traceback (most recent call last):
File "C:\Users\Zhao\Desktop\picx\new.py", line 44, in <module>
    t.setDaemon(True)
RuntimeError: cannot set daemon status of active thread

刚刚入坑PYTHON求大佬看看

kof21411 发表于 2020-6-29 20:03

打开的url不对

lxp7765 发表于 2020-6-29 20:17

403 request请求加上请求头
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}

smxgn1 发表于 2020-6-29 20:19

学习了 真心辛苦了

魔道书生 发表于 2020-6-29 20:20

lxp7765 发表于 2020-6-29 20:17
403 request请求加上请求头
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gec ...

加了还是老样子

ReLoading 发表于 2020-6-29 20:33

threads = []
n=1
path=a+str(page)+'\\'
for url in urls:
    t=threading.Thread(target=urllib.request.urlretrieve,args=(url,path+str(n)+'.jpg',))
    threads.append(t)
    t.start()
    n+=1
for t in threads:
    t.join()

魔道书生 发表于 2020-6-29 21:35

ReLoading 发表于 2020-6-29 20:33
threads = []
n=1
path=a+str(page)+'\\'


Exception in thread Exception in thread Thread-4Exception in thread Thread-1Exception in thread :
Thread-6Exception in thread Exception in thread Exception in thread Exception in thread :
Thread-5Exception in thread Exception in thread Exception in thread Exception in thread Traceback (most recent call last):
:


老是报那个
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner

和 403 错误
Thread-10Thread-8Exception in thread Exception in thread Thread-11Exception in thread Exception in thread Thread-9Traceback (most recent call last):
:
Thread-15Thread-17Thread-16Thread-18File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
Traceback (most recent call last):
:
:
Thread-23Thread-21:
Thread-24Thread-26:
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
Traceback (most recent call last):
:
:
:

lxp7765 发表于 2020-6-29 21:36

t.setDaemon(True)start的线程执行到这里会报错 前面可以加个判断 已经设置过的线程跳过
tthreads = []
n=1
path=a+str(page)+'\\'
for url in urls:
    t=threading.Thread(target=urllib.request.urlretrieve,args=(url,path+str(n)+'.jpg',))
    threads.append(t)
    n+=1
    for t in threads:
      ift.daemon:
            continue
      t.setDaemon(True)
      t.start()
    t.join()

魔道书生 发表于 2020-6-29 21:42

lxp7765 发表于 2020-6-29 21:36
t.setDaemon(True)start的线程执行到这里会报错 前面可以加个判断 已经设置过的线程跳过
tthreads = []
...

Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 640, in http_response
    response = self.parent.error(
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 640, in http_response
    response = self.parent.error(
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
Exception in thread Thread-3:
Traceback (most recent call last):
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 640, in http_response
    response = self.parent.error(
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
File "C:\Users\Zhao\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
403应该是协议头的问题 上面的报错就不知道了

井右寺 发表于 2020-6-30 10:51

循环有问题
双层循环了,看你的意思是单层循环才对
要么把for t in treads 放到外层for外面去。要么直接取消for tin,直接t.set t.start
报错意思是,不能设置一个已经开始运行的线程为守护线程,理解一下
在数组里面,第一个元素,已经start了之后,后面的循环里,你又再尝试 设置它为守护线程
页: [1] 2
查看完整版本: python多线程报错