Python线程导致Google API问题

时间:2019-03-01 15:22:32

标签: python-3.x python-multithreading

我正在浏览位置列表,并试图在我的路线上查找位置。这是我第一次尝试穿线,因此任何提示将不胜感激!当我运行它时,它在最初的几次迭代中都可以正常工作,但是随后我开始得到一个KeyError,并且API响应指出未找到路由(即使应该找到)。如果我沿着较短的路线搜索,一切都会很好。当我将路线延长超过几个小时的车程后,我开始遇到这些错误。我有可能正在超载它还是我的代码看上去不正确?

import pandas as pd
from threading import Thread
import threading
import requests


start_input = input("start: ")
end_input = input("end: ")
out_way = input("out of the way: ")
out_way_secs = int(out_way) * 60
thread_local = threading.local()


def get_session():
    if not getattr(thread_local, "session", None):
        thread_local.session = requests.Session()
    return thread_local.session


def get_routes(url, start, end, waypoint, idx):
    session = get_session()
    with session.get(url, params={'origins': f'{start}|{waypoint}', 'destinations': f'{start}|{end}',
                                  'key': '# key'}) as response:
        route = response.json()
        if route['rows'][1]['elements'][0]['status'] != 'OK':
            results[idx] = {'# info'}
        else:
            nonstop_route = route['rows'][0]['elements'][1]['duration']['value']
            leg1 = route['rows'][1]['elements'][0]['duration']['value']
            leg2 = route['rows'][1]['elements'][1]['duration']['value']

            time_added = (leg1 + leg2) - nonstop_route
            time_added_mins = str(datetime.timedelta(seconds=(leg1 + leg2) - nonstop_route))
            more_time = time_added_mins.split(':')
            added_time_str = str(f'{more_time[0]}:{more_time[1]}:{more_time[2]} away!')
            if time_added < allowable_time:
                    results[idx] = {# info to return}
                    return results[idx]


if __name__ == "__main__":
    start_time = time.time()
    output_df = pd.DataFrame(columns=['Location', 'Added Time', 'Notes'])

    threads = [None] * coords[0]
    results = [None] * coords[0]

    for i in range(len(threads)):
        threads[i] = Thread(target=get_routes, args=('https://maps.googleapis.com/maps/api/distancematrix/json',
                                                     start_input, end_input, stops[i], i))
        threads[i].start()

    for i in range(len(threads)):
        threads[i].join()

    for x in range(len(results)):
        output_df = output_df.append(results[x], ignore_index=True)

    output_df = output_df.sort_values(['Added Time'], ascending=True)
    output_df.to_csv('output.csv', index=False)

会有3个错误,第一个错误会自动弹出,最后两个错误会聚在一起。当我运行它时,代码是相同的,所以不确定为什么我会得到不同的错误。

这是最常见的错误(路由持续时间在单独运行时可以正常运行):

Exception in thread Thread-171:
Traceback (most recent call last):
  File "C:\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:program.py", line 46, in get_routes
    nonstop_route = route['rows'][0]['elements'][1]['duration']['value']
KeyError: 'duration'

下面的两个人聚在一起却不太常见:

Exception in thread Thread-436:
Traceback (most recent call last):
  File "C:\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:/program.py", line 40, in get_routes
    route = response.json()
  File "C:\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Python37-32\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Python37-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python37-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

第二次错误:

Exception in thread Thread-196:
Traceback (most recent call last):
  File "C:\site-packages\urllib3\response.py", line 360, in _error_catcher
    yield
  File "C:\urllib3\response.py", line 442, in read
    data = self._fp.read(amt)
  File "C:\Python37-32\lib\http\client.py", line 447, in read
    n = self.readinto(b)
  File "C:\Python37-32\lib\http\client.py", line 491, in readinto
    n = self.fp.readinto(b)
  File "C:\Python37-32\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\Python37-32\lib\ssl.py", line 1052, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Python37-32\lib\ssl.py", line 911, in read
    return self._sslobj.read(len, buffer)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\site-packages\requests\models.py", line 750, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\site-packages\urllib3\response.py", line 494, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "C:\site-packages\urllib3\response.py", line 459, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "C:\Python37-32\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\site-packages\urllib3\response.py", line 378, in _error_catcher
    raise ProtocolError('Connection broken: %r' % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None)", ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None))

0 个答案:

没有答案