即使设置了超时,Python urllib2.urlopen 也会无限地冻结脚本 [英] Python urllib2.urlopen freezes script infinitely even though timeout is set

查看:26
本文介绍了即使设置了超时,Python urllib2.urlopen 也会无限地冻结脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  • 为什么即使设置了超时,urlopen 仍会永远冻结我的脚本?
  • 如何访问 URL 上的数据(在本例中:http://api.own3d.tv/live?channel=FnaticTV) 而我的 Python 进程不可能永远冻结?​​

这是冻结的部分(在 own3d.py 中):

#尝试3次联系为真:尝试:# 连接API# 就在这儿!这里结冰connection = urllib2.urlopen(request, timeout=10)xmlstring = connection.read()除了 URLError 为 e:尝试 += 1如果尝试> = 3:sys.stderr.write('own3dStreamsUpdater:致命错误:重复超时')出口()

这是我的 KeyboardInterrupt 之后的堆栈跟踪

<前>回溯(最近一次调用最后一次):文件",第 1 行,在文件honsapp/own3dStreamsUpdater.py",第 53 行,在 updateStreamInfo 中流信息 = 获取流信息(流)getStreamInfo 中的文件honsapp/own3d.py",第 98 行connection = urllib2.urlopen(request, timeout=10)文件/usr/local/lib/python2.7/urllib2.py",第 126 行,在 urlopenreturn _opener.open(url, data, timeout)文件/usr/local/lib/python2.7/urllib2.py",第394行,打开response = self._open(req, data)文件/usr/local/lib/python2.7/urllib2.py",第 412 行,在 _open'_open',请求)_call_chain 中的文件/usr/local/lib/python2.7/urllib2.py",第 372 行结果 = func(*args)文件/usr/local/lib/python2.7/urllib2.py",第 1199 行,在 http_open返回 self.do_open(httplib.HTTPConnection, req)文件/usr/local/lib/python2.7/urllib2.py",第 1170 行,在 do_openr = h.getresponse(缓冲=真)文件/usr/local/lib/python2.7/httplib.py",第 1027 行,在 getresponse 中response.begin()文件/usr/local/lib/python2.7/httplib.py",第 407 行,在开始版本、状态、原因 = self._read_status()文件/usr/local/lib/python2.7/httplib.py",第 365 行,在 _read_statusline = self.fp.readline()文件/usr/local/lib/python2.7/socket.py",第 447 行,在 readline数据 = self._sock.recv(self._rbufsize)键盘中断


编辑

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

<前>回溯(最近一次调用最后一次):文件honsapp/own3dStreamsUpdater.py",第 260 行,在新信息()文件honsapp/own3dStreamsUpdater.py",第 172 行,在 newInfo 中结果 = updateStreamInfo(stream)文件honsapp/own3dStreamsUpdater.py",第 53 行,在 updateStreamInfo 中流信息 = 获取流信息(流)getStreamInfo 中的文件/root/Dropbox/Projects/honstreams/honsapp/own3d.py",第 98 行connection = urllib2.urlopen(request, timeout=10)文件/usr/local/lib/python2.7/urllib2.py",第 126 行,在 urlopenreturn _opener.open(url, data, timeout)文件/usr/local/lib/python2.7/urllib2.py",第394行,打开response = self._open(req, data)文件/usr/local/lib/python2.7/urllib2.py",第 412 行,在 _open'_open',请求)_call_chain 中的文件/usr/local/lib/python2.7/urllib2.py",第 372 行结果 = func(*args)文件/usr/local/lib/python2.7/urllib2.py",第 1199 行,在 http_open返回 self.do_open(httplib.HTTPConnection, req)文件/usr/local/lib/python2.7/urllib2.py",第 1170 行,在 do_openr = h.getresponse(缓冲=真)文件/usr/local/lib/python2.7/httplib.py",第 1027 行,在 getresponse 中response.begin()文件/usr/local/lib/python2.7/httplib.py",第 407 行,在开始版本、状态、原因 = self._read_status()文件/usr/local/lib/python2.7/httplib.py",第 371 行,在 _read_status提高 BadStatusLine(line)httplib.BadStatusLine: ''

解决方案

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

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

固定代码:

#尝试3次联系为真:尝试:# 连接APIconnection = urllib2.urlopen(request, timeout=10)xmlstring = connection.read()休息除了 URLError 为 e:尝试 += 1如果尝试> = 3:sys.stderr.write('own3dStreamsUpdater:致命错误:重复超时')出口()

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

解决方案

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

这篇关于即使设置了超时,Python urllib2.urlopen 也会无限地冻结脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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