How can I run more than two Thread to parse multiple documents with DocumentProcessorServiceAsyncClient – python

#As such the code works but only with two Threads, if I add another one, the process stops and then takes a time out, I don’t know if DocumentProcessorServiceAsyncClient will have a limit of two threads at a time although most likely I am doing something wrong.

async def get_text_ocr(file_path):
try:
tipo = str(file_path).split(‘.’)[-1]
if tipo.upper() ==’PDF’:
mime_type = ‘application/’+str(file_path).split(‘.’)[-1]
else:
mime_type = ‘image/’+str(file_path).split(‘.’)[-1]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> print(mime_type)
#client = documentai.DocumentProcessorServiceClient(
client = documentai.DocumentProcessorServiceAsyncClient(
client_options=ClientOptions(api_endpoint=f'{location}-{endpoint}')
)
name = client.processor_version_path(project_id,location,processor_id,processor_version)
with open(file_path, 'rb') as pag:
image_content = pag.read()
raw_document = documentai.RawDocument(
content = image_content, mime_type=mime_type
)
request = documentai.ProcessRequest(name=name, raw_document=raw_document)
response = await client.process_document(request=request)
document = response.document
return document.text
except Exception as exp:
print(exp)
return None
</code>
<code> print(mime_type) #client = documentai.DocumentProcessorServiceClient( client = documentai.DocumentProcessorServiceAsyncClient( client_options=ClientOptions(api_endpoint=f'{location}-{endpoint}') ) name = client.processor_version_path(project_id,location,processor_id,processor_version) with open(file_path, 'rb') as pag: image_content = pag.read() raw_document = documentai.RawDocument( content = image_content, mime_type=mime_type ) request = documentai.ProcessRequest(name=name, raw_document=raw_document) response = await client.process_document(request=request) document = response.document return document.text except Exception as exp: print(exp) return None </code>
    print(mime_type)
    #client = documentai.DocumentProcessorServiceClient(
    client = documentai.DocumentProcessorServiceAsyncClient(
        client_options=ClientOptions(api_endpoint=f'{location}-{endpoint}')
    )
    

    name = client.processor_version_path(project_id,location,processor_id,processor_version)
    with open(file_path, 'rb') as pag:
        image_content = pag.read()
    
    raw_document = documentai.RawDocument(
        content = image_content, mime_type=mime_type
    )
    request =  documentai.ProcessRequest(name=name, raw_document=raw_document)
    response = await client.process_document(request=request)
    document = response.document
    return document.text
except Exception as exp:
    print(exp)
    return None

def procesar_documentos(archivos_leer,Carpeta_destino, hilo_cc):
carpeta_igual=str(archivos_leer[0]).split(‘/’)[5]
for archivo in archivos_leer:
carpeta_actual = (str(archivo).split(‘/’)[5])
nueva_ruta = str(archivo).split(‘/’)[:-1]
nueva_ruta.append(“Identificados”)
nueva_ruta = ‘/’.join(nueva_ruta)
if carpeta_actual != carpeta_igual:
hola = str(“/”).join(str(archivo).split(‘/’)[:5])+”/”
system(f’move “{hola+carpeta_igual}” “{Carpeta_destino}”‘)
carpeta_igual = carpeta_actual
txt = mija(archivo)
system(f’move “{hola+carpeta_igual}” “{Carpeta_destino}”‘)
print(‘Finalizado’)

def mija(file_path):
return asyncio.run(get_text_ocr(file_path))

hilo1 = threading.Thread(target=procesar_documentos, args=(hilos[0],Carpeta_destino,”hilo_1″), name=”Hilo 1″)
hilo2 = threading.Thread(target=procesar_documentos, args=(hilos[1],Carpeta_destino,”hilo_2″), name=”Hilo 2″)

deseo poder utilizar como mínimo 4 hilos para procesar documentos 4 DocumentProcessorServiceAsyncClient al tiempo

New contributor

Jeison Jose Bolano Pabon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật