【问题标题】:urlopen is very slow in the second callurlopen 在第二次调用中很慢
【发布时间】:2016-05-04 09:48:36
【问题描述】:

我在一个名为urlopen_test() 的函数中使用urllib.urlopen()。在这个函数中,我调用了 urlopen 两次,第一次调用很快,第二次就不行了。有人打电话帮我弄清楚原因吗?

顺便说一句,我使用line_profile检查函数中的每一行,结果如下。

Line # Hits Time Per Hit % Time Line Contents

 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()

【问题讨论】:

    标签: python urlopen


    【解决方案1】:

    我找到了重点。当我在第一个 fh.read() 之后调用 fh.close() 时,urlopen_test() 函数运行得很快。但是我仍然不明白为什么当我关闭第一个urlopen 返回的文件时,第二个urlopen() 运行得非常快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-28
      • 2017-05-23
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多