urllib2超时但未关闭套接字连接 [英] urllib2 times out but doesn't close socket connection

查看:163
本文介绍了urllib2超时但未关闭套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个python URL抓取程序。为了我的目的,我希望它真的很快超时,所以我正在做



urllib2.urlopen(http://。 ../,超时= 2)



当然它应该正确超时。但是,它并不打算关闭与服务器的连接,因此服务器认为客户端仍然连接。如何请求urllib2在超时后关闭连接?



运行gc.collect()不起作用我想不使用httplib如果我无能为力。



我能得到的最接近的是:第一次尝试将超时。服务器报告连接关闭只是,因为第二次尝试超时。然后,服务器报告连接关闭只是,因为第三次尝试超时。无限广告。



非常感谢。

解决方案

我怀疑套接字仍然在堆栈帧中打开。当Python引发异常时,它会存储堆栈帧,因此调试器和其他工具可以查看堆栈和内省值。



由于历史原因,现在为了向后兼容,堆栈信息在sys中存储(基于每个线程)(请参阅sys.exc_info(),sys.exc_type等)。这是Python 3.0中已删除的内容之一。



这对您来说意味着堆栈仍处于活动状态并被引用。堆栈包含具有打开套接字的某些功能的本地数据。这就是套接字尚未关闭的原因。只有在删除堆栈跟踪时才会显示所有内容。



要测试是否是这种情况,请插入类似


$的内容b $ b

 尝试:
1/0
除ZeroDivisionError:
传递


。这是用其他东西替换当前异常的快速方法。


I'm making a python URL grabber program. For my purposes, I want it to time out really really fast, so I'm doing

urllib2.urlopen("http://.../", timeout=2)

Of course it times out correctly as it should. However, it doesn't bother to close the connection to the server, so the server thinks the client is still connected. How can I ask urllib2 to just close the connection after it times out?

Running gc.collect() doesn't work and I'd like to not use httplib if I can't help it.

The closest I can get is: the first try will time out. The server reports that the connection closed just as the second try times out. Then, the server reports the connection closed just as the third try times out. Ad infinitum.

Many thanks.

解决方案

I have a suspicion that the socket is still open in the stack frames. When Python raises an exception it stores the stack frames so debuggers and other tools can view the stack and introspect values.

For historical reasons, and now for backwards compatibility, the stack information is stored (on a per-thread basis) in sys (see sys.exc_info(), sys.exc_type and others). This is one of the things which has been removed in Python 3.0.

What that means for you is the stack is still alive, and referenced. There stack contains the local data for some function which has the open socket. That's why the socket isn't yet closed. It's only when the stack trace is removed that everything will be gc'ed.

To test if that's the case, insert something like

try:
  1/0
except ZeroDivisionError:
  pass

in your except clause. That's a quick way to replace the current exception with something else.

这篇关于urllib2超时但未关闭套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆