gpt4 book ai didi

python - urlopen 在第二次调用中非常慢

转载 作者:太空宇宙 更新时间:2023-11-04 05:31:29 27 4
gpt4 key购买 nike

我在名为 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() 运行得非常快。

关于python - urlopen 在第二次调用中非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com