

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 스로틀된 통화 및 끊어진 연결 처리
<a name="handling-errors"></a>

초당 최대 트랜잭션 수 (TPS) 를 초과하여 서비스가 애플리케이션을 스로틀링하거나 연결이 끊어지면 Amazon Textract 작업이 실패할 수 있습니다. 예를 들어, 단기간에 Amazon Textract 작업에 너무 많은 전화를 걸면 호출이 제한되고`ProvisionedThroughputExceededException`작업 응답에 오류가 있습니다. Amazon Textract TPS 할당량에 대한 자세한 내용은 단원을 참조하십시오.[Amazon Textract 할당량](https://docs.aws.amazon.com/general/latest/gr/textract.html).

작업을 자동으로 다시 시도하여 제한 및 끊어진 연결을 관리할 수 있습니다. 를 포함하여 재시도 횟수를 지정할 수 있습니다.`Config`Amazon Textract 클라이언트를 생성할 때 매개 변수입니다. 재시도 횟수 5개를 사용하는 것이 좋습니다. 이AWSSDK는 실패하고 예외를 throw하기 전에 지정된 횟수만큼 작업을 재시도합니다. 자세한 내용은 [AWS의 오류 재시도 및 지수 백오프](https://docs.aws.amazon.com/general/latest/gr/api-retries.html) 단원을 참조하십시오.

**참고**  
자동 재시도는 동기식 및 비동기 작업 모두에서 작동합니다. 자동 재시도를 지정하기 전에 최신 버전의 AWS SDK를 사용해야 합니다. 자세한 정보는 [2단계: 설정AWS CLI과AWSSDK](setup-awscli-sdk.md)을 참조하십시오.

다음 예제는 여러 문서를 처리할 때 Amazon Textract 작업을 자동으로 재시도하는 방법을 보여 줍니다.

**사전 조건**
+ 아직 설정하지 않았다면 다음과 같이 하십시오.

  1. 을 사용하여 IAM 사용자를 생성하거나 업데이트합니다.`AmazonTextractFullAccess`과`AmazonS3ReadOnlyAccess`권한. 자세한 정보는 [1단계: AWS 계정 설정 및 IAM 사용자 만들기](setting-up.md#setting-up-iam)을 참조하십시오.

  1. AWS CLI와 AWS SDK를 설치하고 구성합니다. 자세한 정보는 [2단계: 설정AWS CLI과AWSSDK](setup-awscli-sdk.md)을 참조하십시오.

**자동으로 작업을 재시도하려면**

1. 동기식 예제를 실행하기 위해 S3 버킷에 여러 문서 이미지를 업로드합니다. S3 버킷에 여러 페이지 문서를 업로드하고`StartDocumentTextDetection`비동기 예제를 실행하십시오.

   지침은 단원을 참조하십시오.[Amazon S3 객체 업로드](https://docs.aws.amazon.com/AmazonS3/latest/user-guide/UploadingObjectsintoAmazonS3.html)의*Amazon Simple Storage Service*.

1. 다음 예제는`Config`매개 변수를 사용하여 작업을 자동으로 다시 시도합니다. 동기식 예제는`DetectDocumentText`작업, 비동기 예제는`GetDocumentTextDetection`작업.

------
#### [ Sync Example ]

   다음 예제를 사용하여`DetectDocumentText`Amazon S3 버킷의 문서에 대한 작업을 수행합니다. In`main`에서 값을 변경합니다.`bucket`S3 버킷에 업로드합니다. 의 값을 변경하려면`documents`2단계에서 업로드한 문서 이미지의 이름을 지정합니다.

   ```
   import boto3
   from botocore.client import Config
   # Documents
   
   def process_multiple_documents(bucket, documents):
       
       config = Config(retries = dict(max_attempts = 5))
    
       # Amazon Textract client
       textract = boto3.client('textract', config=config)
    
       for documentName in documents:
    
           print("\nProcessing: {}\n==========================================".format(documentName))
    
           # Call Amazon Textract
           response = textract.detect_document_text(
               Document={
                   'S3Object': {
                       'Bucket': bucket,
                       'Name': documentName
                   }
               })
    
           # Print detected text
           for item in response["Blocks"]:
               if item["BlockType"] == "LINE":
                   print ('\033[94m' +  item["Text"] + '\033[0m')
   
   
   def main():
       bucket = ""
       documents = ["document-image-1.png",
       "document-image-2.png", "document-image-3.png",
       "document-image-4.png", "document-image-5.png" ]
       process_multiple_documents(bucket, documents)
   
   
   
   if __name__ == "__main__":
       main()
   ```

------
#### [ Async Example ]

   다음 예제를 사용하여 `GetDocumentTextDetection` 작업을 호출합니다. 이미 전화를 걸었다고 가정합니다.`StartDocumentTextDetection`을 Amazon S3 버킷의 문서에 업로드하고`JobId`. In`main`에서 값을 변경합니다.`bucket`S3 버킷에 업로드하고`roleArn`Textract 역할에 할당된 Arn에. 의 값도 변경해야 합니다.`document`을 Amazon S3 버킷의 여러 페이지 문서 이름으로 바꿉니다. 마지막으로, 의 값을 대체합니다.`region_name`을 지역 이름으로 바꿉니다.`GetResults`의 이름을 가진 함수`jobId`.

   ```
   import boto3
   from botocore.client import Config
   
   class DocumentProcessor:
       jobId = ''
       region_name = ''
   
       roleArn = ''
       bucket = ''
       document = ''
   
       sqsQueueUrl = ''
       snsTopicArn = ''
       processType = ''
   
       def __init__(self, role, bucket, document, region):
           self.roleArn = role
           self.bucket = bucket
           self.document = document
           self.region_name = region
           self.config = Config(retries = dict(max_attempts = 5))
   
           self.textract = boto3.client('textract', region_name=self.region_name, config=self.config)
           self.sqs = boto3.client('sqs')
           self.sns = boto3.client('sns')
   
   # Display information about a block
       def DisplayBlockInfo(self, block):
   
           print("Block Id: " + block['Id'])
           print("Type: " + block['BlockType'])
           if 'EntityTypes' in block:
               print('EntityTypes: {}'.format(block['EntityTypes']))
   
           if 'Text' in block:
               print("Text: " + block['Text'])
   
           if block['BlockType'] != 'PAGE':
               print("Confidence: " + "{:.2f}".format(block['Confidence']) + "%")
   
           print('Page: {}'.format(block['Page']))
   
           if block['BlockType'] == 'CELL':
               print('Cell Information')
               print('\tColumn: {} '.format(block['ColumnIndex']))
               print('\tRow: {}'.format(block['RowIndex']))
               print('\tColumn span: {} '.format(block['ColumnSpan']))
               print('\tRow span: {}'.format(block['RowSpan']))
   
               if 'Relationships' in block:
                   print('\tRelationships: {}'.format(block['Relationships']))
   
           print('Geometry')
           print('\tBounding Box: {}'.format(block['Geometry']['BoundingBox']))
           print('\tPolygon: {}'.format(block['Geometry']['Polygon']))
   
           if block['BlockType'] == 'SELECTION_ELEMENT':
               print('    Selection element detected: ', end='')
               if block['SelectionStatus'] == 'SELECTED':
                   print('Selected')
               else:
                   print('Not selected')
   
       def GetResults(self, jobId):
           maxResults = 1000
           paginationToken = None
           finished = False
   
           while finished == False:
   
               response = None
   
               if paginationToken == None:
                   response = self.textract.get_document_text_detection(JobId=jobId,
                                                                            MaxResults=maxResults)
               else:
                   response = self.textract.get_document_text_detection(JobId=jobId,
                                                                            MaxResults=maxResults,
                                                                            NextToken=paginationToken)
   
               blocks = response['Blocks']
               print('Detected Document Text')
               print('Pages: {}'.format(response['DocumentMetadata']['Pages']))
   
               # Display block information
               for block in blocks:
                   self.DisplayBlockInfo(block)
                   print()
                   print()
   
               if 'NextToken' in response:
                   paginationToken = response['NextToken']
               else:
                   finished = True
   
   def main():
       roleArn = 'role-arn'
       bucket = 'bucket-name'
       document = 'document-name'
       region_name = 'region-name'
       analyzer = DocumentProcessor(roleArn, bucket, document, region_name)
       analyzer.GetResults("job-id")
   
   if __name__ == "__main__":
       main()
   ```

------