首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优雅地捕获Iterator中的python异常

优雅地捕获Iterator中的python异常
EN

Stack Overflow用户
提问于 2019-01-06 23:45:17
回答 1查看 1.2K关注 0票数 2

我正在使用PyGithub来抓取一些存储库,尽管我在迭代搜索页面时遇到了一些错误。

代码语言:javascript
复制
def scrape_interval(self, interval):
    for repo_number, repo in self.search(interval):
        code...

def search(self, interval):
    try:
        iterator = enumerate(self.github.search_repositories(query="Laravel created:" + interval))
    except:
        print.warning("Going to sleep for 1 hour. The search API hit the limit")
        time.sleep(3600)
        iterator = self.search(interval)
    return iterator

如您所见,在def search中创建迭代器时,我尝试捕捉错误。但是这个错误是在for repo_number, repo in self.search(interval):上抛出的,所以在迭代器得到下一个项目的某个时候?

我有什么选择来使这些错误容易被发现?我最好避免将整个for循环包装在try子句中,而不是在迭代过程中管理它。

有关错误本身的参考:

代码语言:javascript
复制
File "/Users/olofjondelius/Documents/Code/laravel-ai/src/examples/migration-analysis/../../GithubScraper.py", line 47, in scrape_interval
    for repo_number, repo in self.search(interval):
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/PaginatedList.py", line 58, in _iter_
    newElements = self._grow()
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/PaginatedList.py", line 70, in _grow
    newElements = self._fetchNextPage()
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/PaginatedList.py", line 172, in _fetchNextPage
    headers=self.__headers
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/Requester.py", line 185, in requestJsonAndCheck
    return self.__check(*self.requestJson(verb, url, parameters, headers, input, cnx))
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/Requester.py", line 231, in requestJson
    return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/Requester.py", line 284, in __requestEncode
    status, responseHeaders, output = self.__requestRaw(cnx, verb, url, requestHeaders, encoded_input)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/Requester.py", line 309, in __requestRaw
    requestHeaders
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1384, in connect
    super().connect()
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 928, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/anaconda3/envs/laravel-ai/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-09 00:08:01

听起来,当您在迭代器上迭代时,而不是在创建迭代器时,会引发异常。当前的tryexcept块只捕获在调用self.github.search_repositories时立即引发的异常,而不是在使用结果时出现的异常。

要解决这个问题,您可以让search函数成为一个生成器。这将使您在有值的情况下产生值,但仍然会捕获异常,并根据需要经常重试。

试着做这样的事情:

代码语言:javascript
复制
def search(self, interval):
    while True:
        try:
            it = enumerate(self.github.search_repositories(query="Laravel created:" + interval))
            yield from it
            return   # if we completed the yield from without an exception, we're done!

        except:  # you should probably limit this to catching a specific exception types
            print.warning("Going to sleep for 1 hour. The search API hit the limit")
            time.sleep(3600)

正如我在一条注释中指出的,您可能应该将裸except语句更改为except socket.gaierror或类似的东西,这样就不会抑制所有异常,而是只会阻止所期望的异常,并且延迟将为您修复。仍然应该允许一些真正出乎意料的东西停止程序(因为它可能反映在代码的其他地方)。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54066957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档