吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2023|回复: 15
收起左侧

[求助] python多线程报错

[复制链接]
魔道书生 发表于 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()


抛出错误
[Python] 纯文本查看 复制代码
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
[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)
    t.start()
    n+=1
for t in threads:
    t.join()
 楼主| 魔道书生 发表于 2020-6-29 21:35
ReLoading 发表于 2020-6-29 20:33
[Python] 纯文本查看 复制代码
threads = []
n=1
path=a+str(page)+'\\'
[/quote]

[mw_shl_code=python,true]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-18  File "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:
        if  t.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 = []
...

[Python] 纯文本查看 复制代码
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 t  in,直接t.set t.start
报错意思是,不能设置一个已经开始运行的线程为守护线程,理解一下
在数组里面,第一个元素,已经start了之后,后面的循环里,你又再尝试 设置它为守护线程
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 15:24

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表