我在名为 urlopen_test() 的函数中使用 urllib.urlopen()。在这个函数中我调用了两次 urlopen,第一次调用很快,但第二次调用速度很快。有人打电话来帮我弄清楚为什么吗?
顺便说一句,我使用 line_profile 检查函数中的每一行,结果如下。
Line # Hits Time Per Hit % 时间线内容
5 @profile
6 def urlopen_test():
7 1 28690 28690.0 0.1 fh =urllib.urlopen('ftp://a.com/1.log')
8 1 58 58.0 0.0 print fh.read()
9
10 1 30027787 30027787.0 99.9 fh = urllib.urlopen('ftp://a.com/1.log')
11 1 40 40.0 0.0 print fh.read()
请您参考如下方法:
我找到了重点。当我在第一个 fh.read() 之后调用 fh.close() 时,urlopen_test() 函数运行得很快。但我仍然不明白为什么当我关闭第一个 urlopen 调用返回的文件时,第二个 urlopen() 运行得非常快。
