Crossin的编程教室

标题: 新人求助 python74课 多线程 [打印本页]

作者: yy123    时间: 2018-3-20 19:19
标题: 新人求助 python74课 多线程
我用的是python3
这是代码
import urllib.request,time,_thread
def get_content(i):
    id=1764796+i
    url = 'https://api.douban.com/v2/movie/subject/%d' % id
    d=urllib.request.urlopen(url).read()
    data.append(d)
    print(i,time.time()-time_start)
    print('data:',len(data))
   
time_start=time.time()
data=[]
for i in range(30):
    print('request movie:',i)
    _thread.start_new_thread(get_content,(i,))
input('press ENTER to exit...\n')







运行结果是这样的:
request movie: 0
request movie: 1
request movie: 2
request movie: 3
request movie: 4
request movie: 5
request movie: 6
request movie: 7
request movie: 8
request movie: 9
request movie: 10
request movie: 11
request movie: 12
request movie: 13
request movie: 14
request movie: 15
request movie: 16
request movie: 17
request movie: 18
request movie: 19
request movie: 20
request movie: 21
request movie: 22
request movie: 23
request movie: 24
request movie: 25
request movie: 26
request movie: 27
request movie: 28
request movie: 29
press ENTER to exit...
Unhandled exception in thread started by Unhandled exception in thread started by Unhandled exception in thread started by Unhandled exception in thread started by Unhandled exception in thread started by Unhandled exception in thread started by Unhandled exception in thread started by Unhandled exception in thread started by Unhandled exception in thread started by Unhandled exception in thread started by 63162021290Unhandled exception in thread started by 8Unhandled exception in thread started by Unhandled exception in thread started by 177Unhandled exception in thread started by 12224Unhandled exception in thread started by Unhandled exception in thread started by 2
>>> <function get_content at 0x062F8810><function get_content at 0x062F8810><function get_content at 0x062F8810><function get_content at 0x062F8810><function get_content at 0x062F8810><function get_content at 0x062F8810><function get_content at 0x062F8810><function get_content at 0x062F8810><function get_content at 0x062F8810><function get_content at 0x062F8810>       <function get_content at 0x062F8810> <function get_content at 0x062F8810><function get_content at 0x062F8810>  <function get_content at 0x062F8810>   <function get_content at 0x062F8810><function get_content at 0x062F8810>




0.90009140968322750.90009140968322750.92193412780761720.92193412780761720.98459267616271970.98459267616271970.9845926761627197
1.0543501377105713
1.05435013771057131.0543501377105713
1.06997585296630861.06997585296630861.1386773586273193
1.3554306030273438Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):



Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):

Traceback (most recent call last):

Traceback (most recent call last):
Traceback (most recent call last):
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
data:data:data:data:data:data:data:  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
data:  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
data:data:  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
data:data:data:  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
  File "D:\Program Files(X)\Python\个人文件\lesson74.py", line 6, in get_content
data:                                                                              1414141414141414d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()1414d=urllib.request.urlopen(url).read()141414d=urllib.request.urlopen(url).read()d=urllib.request.urlopen(url).read()14














  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
  File "D:\Program Files(X)\Python\lib\urllib\request.py", line 223, in urlopen
                    return opener.open(url, data, timeout)            return opener.open(url, data, timeout)            return opener.open(url, data, timeout)return opener.open(url, data, timeout)return opener.open(url, data, timeout)               
return opener.open(url, data, timeout)    return opener.open(url, data, timeout)return opener.open(url, data, timeout)
return opener.open(url, data, timeout)return opener.open(url, data, timeout)return opener.open(url, data, timeout)


作者: crossin先生    时间: 2018-3-21 14:29
请求失败了吧,豆瓣api现在接口频率降低了,它的官方文档里有说

你这个报错不全,没有输出必要的信息
作者: yy123    时间: 2018-3-22 16:49
嗯  报错太多了就没都复制下来
那我的代码应该没问题把
还有个问题就是func(**kargs)   输出的顺序是按照参数顺序输出的,并不是无序的,这个是因为python3的关系吗  
谢谢了

作者: crossin先生    时间: 2018-3-22 22:30
yy123 发表于 2018-3-22 16:49
嗯  报错太多了就没都复制下来
那我的代码应该没问题把
还有个问题就是func(**kargs)   输出的顺序是按照参 ...

py3的字典的确改过
作者: yy123    时间: 2018-3-23 11:24
嗯谢谢了





欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5