Многопоточность запросов API к Google приводит к ошибке сокета - PullRequest
0 голосов
/ 10 апреля 2020

Сейчас я выполняю многопоточные запросы к изображениям OCR из облачного API Google. Я продвигаю довольно много изображений (в тысячах).

Я пробовал около 3000 изображений без проблем, но сейчас я пробую около 10-20 тысяч изображений и у меня возникает эта ошибка 'OSError: [WinError 10048] Только одно использование каждого адреса сокета (протокол / сетевой адрес / порт) обычно разрешается '.

Вот вызываемая функция:

    """Detects text in the file.

    Keyword arguments:
    path(string) -- location of file needed to downoad 

    Returns all of the detected text as a string.
    """
    from google.cloud import vision
    import io
    client = vision.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision.types.Image(content=content)

    response = client.text_detection(image=image)
    texts = response.text_annotations

    for text in texts:
        return('{}'.format(text.description))

    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages, check: '
            'https://cloud.google.com/apis/design/errors'.format(
                response.error.message))

Вот функция, которая запускает многопоточность

def iterated_master_CSV(year):
    """Saves csv file with all info for a given year. 

    Keyword arguments: 
    year(string)

    Returns time taken.
    """
    start= time.perf_counter()
    directory = r'Dropbox/British Wealth Inequality/Images/Testing/{}'.format(year)
    files=[]
    for filename in os.listdir(directory):
        files.append('{}/{}'.format(directory, filename))
    part_master_CSV=partial(master_CSV, year)
    if __name__== '__main__':
        with ThreadPoolExecutor() as executor: 
            executor.map(part_master_CSV, files)
    finish=time.perf_counter()
    finaltime=finish-start
    return finaltime

Где master_CSV - это функция, которая выполняет несколько простых офлайн-редактирований и вызывает функцию dete_text

...