【问题标题】:Python - RemoteDisconnected: Remote end closed connection without responsePython - RemoteDisconnected:远程结束关闭连接没有响应
【发布时间】:2020-12-30 23:30:17
【问题描述】:

我正在使用 Python 向smashgg API 请求内容(查询在 GraphQL 中),并且在运行以下代码时遇到了以前从未遇到过的错误:

headers = {"Authorization": "Bearer my token"}


def run_query(query, variables): # A simple function to use requests.post to make the API call. Note the json= section.
    request = requests.post('https://api.smash.gg/gql/alpha', json={'query': query, 'variables': variables}, headers=headers)
    if request.status_code == 200:
        return request.json()
    else:
        raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))

nbRequestsPerMinute = 75
dataFrameAllSets = pd.read_csv('dataFrameAllSets.csv') #sets of players I already fetched

countryByPlayer = pd.DataFrame(columns = [
    "playerId",
    "playerCountry"
])

uniqueValues1 = dataFrameAllSets["setWinnerId"].unique()
uniqueValues2 = dataFrameAllSets["setLoserId"].unique()
concate = np.concatenate((uniqueValues1, uniqueValues2))
countryByPlayer["playerId"] = concate
countryByPlayer.drop_duplicates(subset = "playerId", inplace = True)
#countryByPlayer has more than 200 000 rows

def queryCountry(playerId) :
    query = """
    query player ($playerId: ID!){
      player(id: $playerId){
        user{
          location {
            country
          }
        }
      }
    }
    """
    variables = {
      "playerId": playerId
    }
    return query, variables

for index, x in countryByPlayer.iterrows() :
    t0 = time.time()
    query, variables = queryCountry(int(x['playerId']))
    result = run_query(query, variables) # Execute the query
    if result["data"]["player"] != None :
        if result["data"]["player"]["user"] != None :
            if result["data"]["player"]["user"]["location"] != None :
                countryByPlayer.at[index, "playerCountry"] = result["data"]["player"]["user"]["location"]["country"]
            else :
                countryByPlayer.at[index, "playerCountry"] = "noneLocation"
    else :
        countryByPlayer.at[index, "playerCountry"] = "nonePlayer"
        
    t1 = time.time()
    if t1 - t0 < 60/nbRequestsPerMinute :
        time.sleep(60/nbRequestsPerMinute - t1 + t0)

错误信息如下:

RemoteDisconnected                        Traceback (most recent call last)
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    599                                                   body=body, headers=headers,
--> 600                                                   chunked=chunked)
    601 

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    383                     # otherwise it looks like a programming error was the cause.
--> 384                     six.raise_from(e, None)
    385         except (SocketTimeout, BaseSSLError, SocketError) as e:

~\Anaconda3\lib\site-packages\urllib3\packages\six.py in raise_from(value, from_value)

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    379                 try:
--> 380                     httplib_response = conn.getresponse()
    381                 except Exception as e:

~\Anaconda3\lib\http\client.py in getresponse(self)
   1320             try:
-> 1321                 response.begin()
   1322             except ConnectionError:

~\Anaconda3\lib\http\client.py in begin(self)
    295         while True:
--> 296             version, status, reason = self._read_status()
    297             if status != CONTINUE:

~\Anaconda3\lib\http\client.py in _read_status(self)
    264             # sending a valid response.
--> 265             raise RemoteDisconnected("Remote end closed connection without"
    266                                      " response")

RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

ProtocolError                             Traceback (most recent call last)
~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    448                     retries=self.max_retries,
--> 449                     timeout=timeout
    450                 )

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    637             retries = retries.increment(method, url, error=e, _pool=self,
--> 638                                         _stacktrace=sys.exc_info()[2])
    639             retries.sleep()

~\Anaconda3\lib\site-packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    366             if read is False or not self._is_method_retryable(method):
--> 367                 raise six.reraise(type(error), error, _stacktrace)
    368             elif read is not None:

~\Anaconda3\lib\site-packages\urllib3\packages\six.py in reraise(tp, value, tb)
    684         if value.__traceback__ is not tb:
--> 685             raise value.with_traceback(tb)
    686         raise value

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    599                                                   body=body, headers=headers,
--> 600                                                   chunked=chunked)
    601 

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    383                     # otherwise it looks like a programming error was the cause.
--> 384                     six.raise_from(e, None)
    385         except (SocketTimeout, BaseSSLError, SocketError) as e:

~\Anaconda3\lib\site-packages\urllib3\packages\six.py in raise_from(value, from_value)

~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    379                 try:
--> 380                     httplib_response = conn.getresponse()
    381                 except Exception as e:

~\Anaconda3\lib\http\client.py in getresponse(self)
   1320             try:
-> 1321                 response.begin()
   1322             except ConnectionError:

~\Anaconda3\lib\http\client.py in begin(self)
    295         while True:
--> 296             version, status, reason = self._read_status()
    297             if status != CONTINUE:

~\Anaconda3\lib\http\client.py in _read_status(self)
    264             # sending a valid response.
--> 265             raise RemoteDisconnected("Remote end closed connection without"
    266                                      " response")

ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
<ipython-input-5-f3b194f946df> in <module>
      4     compteurPlayers += 1
      5     query, variables = queryCountry(int(x['playerId']))
----> 6     result = run_query(query, variables) # Execute the query
      7     if result["data"]["player"] != None :
      8         if result["data"]["player"]["user"] != None :

<ipython-input-2-2a5c0920ef76> in run_query(query, variables)
      3 
      4 def run_query(query, variables): # A simple function to use requests.post to make the API call. Note the json= section.
----> 5     request = requests.post('https://api.smash.gg/gql/alpha', json={'query': query, 'variables': variables}, headers=headers)
      6     if request.status_code == 200:
      7         return request.json()

~\Anaconda3\lib\site-packages\requests\api.py in post(url, data, json, **kwargs)
    114     """
    115 
--> 116     return request('post', url, data=data, json=json, **kwargs)
    117 
    118 

~\Anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
     58     # cases, and look like a memory leak in others.
     59     with sessions.Session() as session:
---> 60         return session.request(method=method, url=url, **kwargs)
     61 
     62 

~\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    531         }
    532         send_kwargs.update(settings)
--> 533         resp = self.send(prep, **send_kwargs)
    534 
    535         return resp

~\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
    644 
    645         # Send the request
--> 646         r = adapter.send(request, **kwargs)
    647 
    648         # Total elapsed time of the request (approximately)

~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    496 
    497         except (ProtocolError, socket.error) as err:
--> 498             raise ConnectionError(err, request=request)
    499 
    500         except MaxRetryError as e:

ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

我最初认为这与 API 的速率限制有关,该限制设置为每分钟 80 个请求,但是,我将循环延迟到始终低于此速率限制。 而且,我多次重新测试我的代码时,错误是在循环过程中随机出现的。

所以我来找你们是因为我真的需要你们的帮助。

提前致谢。

【问题讨论】:

    标签: python api graphql


    【解决方案1】:

    问题

    远程服务不可靠。

    解决方案

    通过处理代码中的预期错误来进行防御性编程。考虑实施具有最大重试次数的指数退避。此外,添加日志记录以跟踪请求成功、重试或完全失败。如有必要,您可能希望实施应用程序监控或分页系统,以便在满足某个条件(连续 100 个错误)时提醒您,如果这被认为是对您的应用程序至关重要的过程。

    此外,该服务可能有一个您可以使用的批量 API,因此您可以提交 n / bulk_limit(其中 bulk_limit 是他们的批量 ID 的最大数量)而不是提交 n 个请求(其中 n 是玩家 ID 的数量) api 接受在单个请求中处理)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2018-12-27
      • 2021-10-17
      • 2017-03-12
      • 1970-01-01
      • 2018-06-14
      • 2021-08-20
      相关资源
      最近更新 更多