简体   繁体   English

即使设置了超时,Python urllib2.urlopen也会无限地冻结脚本

[英]Python urllib2.urlopen freezes script infinitely even though timeout is set

The function urllib2.urlopen freezes. urllib2.urlopen函数冻结了。 So my question is simple: 所以我的问题很简单:

  • Why does urlopen freeze my script for ever even though timeout is set? 为什么即使设置了超时, urlopen也会冻结我的脚本?
  • How can I access data at an URL (in this case: http://api.own3d.tv/live?channel=FnaticTV ) without the possibility of my Python process freezing up for all eternity? 我怎样才能访问URL上的数据(在这种情况下: http//api.own3d.tv/live?channel = .FnaticTV ),而我的Python进程可能永远不会冻结?

This is the part where is freezes (In own3d.py ): 这是冻结的部分(在own3d.py中 ):

# Try three times to make contact
while True:
    try:
        # Connect to API 

        # Right here! It freezes here
        connection = urllib2.urlopen(request, timeout=10)

        xmlstring = connection.read() 
    except URLError as e:
        tries += 1
        if tries >= 3:
            sys.stderr.write(
                      'own3dStreamsUpdater: Fatal error: Repeated timeouts')
            exit()

This is the stack trace after my KeyboardInterrupt 这是我的KeyboardInterrupt之后的堆栈跟踪

Traceback (most recent call last):
  File "", line 1, in 
  File "honsapp/own3dStreamsUpdater.py", line 53, in updateStreamInfo
    streamInfo = getStreamInfo(stream)
  File "honsapp/own3d.py", line 98, in getStreamInfo
    connection = urllib2.urlopen(request, timeout=10)
  File "/usr/local/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/local/lib/python2.7/urllib2.py", line 394, in open
    response = self._open(req, data)
  File "/usr/local/lib/python2.7/urllib2.py", line 412, in _open
    '_open', req)
  File "/usr/local/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python2.7/urllib2.py", line 1199, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/local/lib/python2.7/urllib2.py", line 1170, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/local/lib/python2.7/httplib.py", line 1027, in getresponse
    response.begin()
  File "/usr/local/lib/python2.7/httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python2.7/httplib.py", line 365, in _read_status
    line = self.fp.readline()
  File "/usr/local/lib/python2.7/socket.py", line 447, in readline
    data = self._sock.recv(self._rbufsize)
KeyboardInterrupt

Edit 编辑

I let my script run over night. 我让我的剧本过夜了。 I don't know exactly how long it took (Though more than five minutes) but the script finally gave up and gave me a stacktrace: 我不知道它花了多长时间(虽然超过五分钟),但剧本终于放弃并给了我一个堆栈跟踪:

Traceback (most recent call last):
  File "honsapp/own3dStreamsUpdater.py", line 260, in 
    newInfo()
  File "honsapp/own3dStreamsUpdater.py", line 172, in newInfo
    result = updateStreamInfo(stream)
  File "honsapp/own3dStreamsUpdater.py", line 53, in updateStreamInfo
    streamInfo = getStreamInfo(stream)
  File "/root/Dropbox/Projects/honstreams/honsapp/own3d.py", line 98, in getStreamInfo
    connection = urllib2.urlopen(request, timeout=10)
  File "/usr/local/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/local/lib/python2.7/urllib2.py", line 394, in open
    response = self._open(req, data)
  File "/usr/local/lib/python2.7/urllib2.py", line 412, in _open
    '_open', req)
  File "/usr/local/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python2.7/urllib2.py", line 1199, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/local/lib/python2.7/urllib2.py", line 1170, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/local/lib/python2.7/httplib.py", line 1027, in getresponse
    response.begin()
  File "/usr/local/lib/python2.7/httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python2.7/httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

This script doesn't freeze at all, but it's a brilliant example of why you shouldn't code while tired. 这个脚本根本不会冻结,但它是一个很好的例子,说明为什么你不应该在疲倦时编码。 The loop that's supposed to try to connect to the API three times will continue for ever, because I forgot to put a break in there. 应该尝试连接到API三次的循环将永远持续下去,因为我忘了在那里break一下。

This question was so stupid I wouldn't blame you for removing it. 这个问题是如此愚蠢,我不会责怪你删除它。

Fixed code: 固定代码:

# Try three times to make contact
while True:
    try:
        # Connect to API 
        connection = urllib2.urlopen(request, timeout=10)
        xmlstring = connection.read()
        break
    except URLError as e:
        tries += 1
        if tries >= 3:
            sys.stderr.write(
                      'own3dStreamsUpdater: Fatal error: Repeated timeouts')
            exit()

Are you sure that the urlopen() call hangs? 你确定urlopen()调用挂起了吗? Because your while loop does not terminate if the call is successful. 因为如果调用成功,你的while循环不会终止。

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

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