@@ -536,6 +536,7 @@ def translate_document_from_filepath(
536
536
target_lang : str ,
537
537
formality : Union [str , Formality ] = Formality .DEFAULT ,
538
538
glossary : Union [str , GlossaryInfo , None ] = None ,
539
+ timeout_s : Optional [int ] = None ,
539
540
) -> DocumentStatus :
540
541
"""Upload document at given input path, translate it into the target
541
542
language, and download result to given output path.
@@ -551,6 +552,9 @@ def translate_document_from_filepath(
551
552
Formality enum, "less", "more", "prefer_less", or "prefer_more".
552
553
:param glossary: (Optional) glossary or glossary ID to use for
553
554
translation. Must match specified source_lang and target_lang.
555
+ :param timeout_s: (beta) (Optional) Maximum time to wait before
556
+ the call raises an error. Note that this is not accurate to the
557
+ second, but only polls every 5 seconds.
554
558
:return: DocumentStatus when document translation completed, this
555
559
allows the number of billed characters to be queried.
556
560
@@ -574,6 +578,7 @@ def translate_document_from_filepath(
574
578
formality = formality ,
575
579
glossary = glossary ,
576
580
output_format = output_format ,
581
+ timeout_s = timeout_s ,
577
582
)
578
583
except Exception as e :
579
584
out_file .close ()
@@ -591,6 +596,7 @@ def translate_document(
591
596
glossary : Union [str , GlossaryInfo , None ] = None ,
592
597
filename : Optional [str ] = None ,
593
598
output_format : Optional [str ] = None ,
599
+ timeout_s : Optional [int ] = None ,
594
600
) -> DocumentStatus :
595
601
"""Upload document, translate it into the target language, and download
596
602
result.
@@ -612,6 +618,9 @@ def translate_document(
612
618
if uploading string or bytes containing file content.
613
619
:param output_format: (Optional) Desired output file extension, if
614
620
it differs from the input file format.
621
+ :param timeout_s: (beta) (Optional) Maximum time to wait before
622
+ the call raises an error. Note that this is not accurate to the
623
+ second, but only polls every 5 seconds.
615
624
:return: DocumentStatus when document translation completed, this
616
625
allows the number of billed characters to be queried.
617
626
@@ -630,7 +639,7 @@ def translate_document(
630
639
)
631
640
632
641
try :
633
- status = self .translate_document_wait_until_done (handle )
642
+ status = self .translate_document_wait_until_done (handle , timeout_s )
634
643
if status .ok :
635
644
self .translate_document_download (handle , output_document )
636
645
except Exception as e :
@@ -752,27 +761,44 @@ def translate_document_get_status(
752
761
)
753
762
754
763
def translate_document_wait_until_done (
755
- self , handle : DocumentHandle
764
+ self ,
765
+ handle : DocumentHandle ,
766
+ timeout_s : Optional [int ] = None ,
756
767
) -> DocumentStatus :
757
768
"""
758
769
Continually polls the status of the document translation associated
759
770
with the given handle, sleeping in between requests, and returns the
760
771
final status when the translation completes (whether successful or
761
772
not).
762
-
763
773
:param handle: DocumentHandle to the document translation to wait on.
774
+ :param timeout_s: (beta) (Optional) Maximum time to wait before
775
+ the call raises an error. Note that this is not accurate to the
776
+ second, but only polls every 5 seconds.
764
777
:return: DocumentStatus containing the status when completed.
765
778
"""
766
779
status = self .translate_document_get_status (handle )
780
+ start_time_s = time .time ()
767
781
while status .ok and not status .done :
768
- secs = 5.0 # seconds_remaining is currently unreliable, so just
769
- # poll equidistantly
770
- util .log_info (
771
- f"Rechecking document translation status "
772
- f"after sleeping for { secs :.3f} seconds."
773
- )
774
- time .sleep (secs )
775
- status = self .translate_document_get_status (handle )
782
+ if (
783
+ timeout_s is not None
784
+ and time .time () - start_time_s > timeout_s
785
+ ):
786
+ raise DeepLException (
787
+ f"Manual timeout of { timeout_s } s exceeded for"
788
+ + " document translation" ,
789
+ should_retry = False ,
790
+ )
791
+ else :
792
+ secs = (
793
+ 5.0 # seconds_remaining is currently unreliable, so just
794
+ )
795
+ # poll equidistantly
796
+ util .log_info (
797
+ f"Rechecking document translation status "
798
+ f"after sleeping for { secs :.3f} seconds."
799
+ )
800
+ time .sleep (secs )
801
+ status = self .translate_document_get_status (handle )
776
802
return status
777
803
778
804
def translate_document_download (
0 commit comments