简体   繁体   English

在python 3中正确使用httplib2? (超时问题)

[英]Using httplib2 in python 3 properly? (Timeout problems)

Hey, first time post, I'm really stuck on httplib2. 嘿,第一次发帖,我真的被困在httplib2上了。 I've been reading up on it from diveintopython3.org, but it mentions nothing about a timeout function. 我一直在从diveintopython3.org上阅读它,但它没有提到超时功能。 I look up the documentation, but the only thing I see is an ability to put a timeout int but there are no units specified (seconds? milliseconds? What's the default if None?) This is what I have (I also have code to check what the response is and try again, but it's never tried more than once) 我查看文档,但我唯一看到的是能够设置超时int但没有指定单位(秒?毫秒?如果没有什么是默认值?)这就是我所拥有的(我也有代码检查)响应是什么,然后再试一次,但它从未尝试过多次)

h = httplib2.Http('.cache', timeout=None)
for url in list:
    response, content = h.request(url)
    more stuff...

So the Http object stays around until some arbitrary time, but I'm downloading a ton of pages from the same server, and after a while, it hangs on getting a page. 因此,Http对象会保持一段时间,直到某个任意时间,但是我从同一台服务器上下载了大量的页面,过了一段时间,它会挂起来获取页面。 No errors are thrown, the thing just hangs at a page. 没有错误被抛出,只是挂在页面上。 So then I try: 那么我试试:

h = httplib2.Http('.cache', timeout=None)
for url in list:
    try:
        response, content = h.request(url)
    except:
        h = httplib2.Http('.cache', timeout=None)
    more stuff...

But then it recreates another Http object every time (goes down the 'except' path)...I dont understand how to keep getting with the same object, until it expires and I make another. 但是它每次都会重新创建另一个Http对象(沿着'except'路径向下)...我不明白如何继续使用相同的对象,直到它到期并且我再创建另一个对象。 Also, is there a way to set a timeout on an individual request? 另外,有没有办法在单个请求上设置超时?

Thanks for the help! 谢谢您的帮助!

由于错误 httplib2测量超时以秒为单位乘以2 直到版本0.7.5 (2012-08-28)。

Set the timeout to 1, and you'll pretty quickly know if it means one millisecond or one second. 将超时设置为1,您很快就会知道它是否意味着一毫秒或一秒。

I don't know what your try/except should solve, if it hangs on h.request(url) in one case it should hang in the other. 我不知道你的try / except应该解决什么,如果它挂在h.request(url)上,在一种情况下它应该挂在另一种情况下。

If you run out of memory in that code, then httplib2 doesn't get garbage collected properly. 如果该代码中的内存不足,则httplib2不会正确收集垃圾。 It may be that you have circular references (although it doesn't look like it above) or it may be a bug in httlib2. 可能是您有循环引用(虽然它看起来不像上面)或者它可能是httlib2中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM