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

发布于 2024-12-14 08:01:35 字数 3706 浏览 4 评论 0原文

函数 urllib2.urlopen 冻结。所以我的问题很简单:

这是冻结的部分(在 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()

后的堆栈跟踪

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

编辑

这是我的KeyboardInterrupt 让我的脚本运行一夜。我不知道到底花了多长时间(虽然超过五分钟)但脚本最终放弃并给了我一个堆栈跟踪:

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: ''

The function urllib2.urlopen freezes. So my question is simple:

  • Why does urlopen freeze my script for ever even though timeout is set?
  • 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?

This is the part where is freezes (In 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

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: ''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2024-12-21 08:01:35

这个脚本根本不会冻结,但它是一个很好的例子,说明了为什么你不应该在疲倦时编码。应该尝试连接到 API 3 次的循环将永远持续下去,因为我忘记在其中放置 break

这个问题太愚蠢了,我不会责怪你删除它。

固定代码:

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

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.

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()
匿名的好友 2024-12-21 08:01:35

您确定 urlopen() 调用挂起吗?因为如果调用成功,您的 while 循环不会终止。

Are you sure that the urlopen() call hangs? Because your while loop does not terminate if the call is successful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文