
    TAi                       S r SSKJr  SSKrSSKrSSKrSSKrSSKJrJ	r	J
r
  SSKJr  SSKJrJr  SSKJrJrJrJr  SSKJrJr  SS	KJrJr  SS
KJrJr  SSKJr  SSK J!r!  SSK"J#r#  \RH                  " S\%5         " S S\5      r&SS jr'S r( " S S\RR                  RT                  5      r+    SS jr, " S S\5      r-\S 5       r.\S 5       r/\S 5       r0g)ad  Semaphore-free alternate executor.

There are two popular environments that do not fully support the standard Python
multiprocessing module: AWS Lambda, and Termux (a terminal emulator for Android).

This alternate executor divvies up work among worker processes before processing,
rather than having each worker consume work from a shared queue when they finish
their task. This means workers have no need to coordinate with each other. Each
worker communicates only with the main process.

This is not without drawbacks. If the tasks are not "even" in size, which cannot
be guaranteed, some workers may end up with too much work while others are idle.
It is less efficient than the standard implementation, so not the default.

This module is deprecated and will be removed in a future release. The standard
executor will fall back to threads in these environments.
    )annotationsN)CallableIterableIterator)suppress)Enumauto)islicerepeat	takewhilezip_longest)PipeProcess)
Connectionwait)ExecutorhookimplNullProgressBarInputFileError)remove_all_log_handlerszAsemfree.py is deprecated and will be removed in a future release.c                  B    \ rS rSrSr\" 5       r\" 5       r\" 5       rSr	g)MessageType-   zImplement basic IPC messaging. N)
__name__
__module____qualname____firstlineno____doc__r	   	exceptionresultcomplete__static_attributes__r       Z/var/www/html/land-ocr/venv/lib/python3.13/site-packages/ocrmypdf/extra_plugins/semfree.pyr   r   -   s    (IVFvHr&   r   c                d   ^ ^ [        U5      m[        [        UU 4S j[        S5       5       5      $ )zSplit iterable into groups of n.

>>> list(split_every(4, range(10)))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

https://stackoverflow.com/a/22919323
c              3  N   >#    U  H  n[        [        TT5      5      v   M     g 7f)N)listr
   ).0_iteratorns     r'   	<genexpr>split_every.<locals>.<genexpr>>   s      L|!D!!455|s   "%N)iterr   boolr   )r.   iterabler-   s   ` @r'   split_everyr4   5   s%     H~HTLvd|LMMr&   c                     [        S5      e)z)Handle SIGBUS signal at the worker level.z-A worker process lost access to an input filer   )argss    r'   process_sigbusr7   A   s    
H
IIr&   c                  6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )ConnectionLogHandlerF   zBHandler used by child processes to forward log messages to parent.c                0   > [         TU ]  S5        Xl        g)zInitialize the handler.N)super__init__conn)selfr>   	__class__s     r'   r=   ConnectionLogHandler.__init__I   s     		r&   c                >    U R                   R                  SU45        g)zEnqueue a log message.logN)r>   send)r?   records     r'   enqueueConnectionLogHandler.enqueueP   s    		v'r&   )r>   )r>   r   returnNone)	r   r   r   r    r!   r=   rF   r%   __classcell__)r@   s   @r'   r9   r9   F   s    L( (r&   r9   c                   [        [        5         [        R                  " [        R                  [        5        SSS5        [        U 5      n[        R                  " 5       n[        U5        UR                  U5        UR                  U5        U" 5         U H*  n U" U6 nU R                  [        R                  U45        M,     U R                  [        R                   S45        U R#                  5         g! , (       d  f       N= f! [         a-  n	U R                  [        R                  U	45         Sn	A	  Mu  Sn	A	ff = f)z!Initialize a process pool worker.N)r   AttributeErrorsignalSIGBUSr7   r9   logging	getLoggerr   setLevel
addHandlerrD   r   r#   	Exceptionr"   r$   close)
r>   	user_initlogleveltask	task_argshrootr6   r#   es
             r'   process_loopr\   U   s    
 
.	!fmm^4 
"
 	T"ADD!MM(OOAK	44[F
 II{))623  	II{##T*+JJL
1 
"	!   	II{,,a01	s#   *C8D	8
D	
E !D;;E c                  >    \ rS rSrSr              SS jrSrg)LambdaExecutoru   zEExecutor for AWS Lambda or similar environments that lack semaphores.c          
     <   U(       a8  US:X  a2  U R                   " S0 UD6 nU H  n	U" U	6 n
U" X5        M     S S S 5        g [        U5      n[        [        [        [        X&5      5      6 5      nU(       d  g / n/ nU H  n[	        5       u  nnU V	s/ s H	  oc  M  U	PM     nn	[        [        UU[        R                  " S5      R                  UU4S9nSUl
        UR                  U5        UR                  U5        M     U H  nUR                  5         M     U R                   " S0 UD6 nU(       a  [        U5       H  n
[        U
[        5      (       d  [!        S5      e U
R#                  5       u  nnU[(        R*                  :X  a  U" UU5        MV  US:X  a5  Un[        R                  " UR,                  5      nUR/                  U5        M  U[(        R0                  :X  a  UR'                  U
5        M  U[(        R2                  :X  d  M  U H  nUR5                  5         M     Ue   U(       a  M  S S S 5        U H  nUR7                  5         M     g ! , (       d  f       g = fs  sn	f ! [$         a    UR'                  U
5         GMK  f = f! , (       d  f       Na= f)N    )targetr6   TzWe only support Connection()rC   r   )
pbar_classr*   r   r4   r   r   r\   rO   rP   leveldaemonappendstartr   
isinstancer   NotImplementedErrorrecvEOFErrorremover   r#   namehandler$   r"   	terminatejoin)r?   use_threadsmax_workersprogress_kwargsworker_initializerrW   task_argumentstask_finishedpbarr6   r#   grouped_args	processesconnectionschunkparent_conn
child_connworker_argsprocessmsg_typemsgrE   loggers                          r'   _executeLambdaExecutor._executex   sP    ;!+3?3t*D!4[F!&/ + 4 n-k+FGH
 #%	(*!E&*f#K,1FED4EKF#&%%b)//	G "GNW%{+! "$ !GMMO ! __//4";/F%fj9912PQQ!(.#
  ;#5#55%c40!U*!$!(!2!26;;!?f-![%9%99#**62![%:%::'0G#--/ (1!	) 0 + 00 !GLLN !y 43  G2 $ !#**62 ! 0/sN   II%I%+6J"I*5BJ$J
I"*J
J	J

J
Jr   N)rr   r2   rs   intrt   dictru   r   rW   r   rv   r   rw   r   )r   r   r   r    r!   r   r%   r   r&   r'   r^   r^   u   sX    OI I 	I
 I %I I !I  Ir&   r^   c                    [        U S9$ )z!Return a LambdaExecutor instance.)rd   )r^   )progressbar_classs    r'   get_executorr      s     %677r&   c                 ,    [         R                  " 5       $ )z(Return a logging.StreamHandler instance.)rO   StreamHandlerr   r&   r'   get_logging_consoler      s       ""r&   c                     [         $ )zMReturn a NullProgressBar instance.

This executor cannot use a progress bar.
r   r   r&   r'   get_progressbar_classr      s
     r&   )r.   r   r3   r   rH   r   )r>   r   rU   zCallable[[], None])1r!   
__future__r   rO   logging.handlersrM   warningscollections.abcr   r   r   
contextlibr   enumr   r	   	itertoolsr
   r   r   r   multiprocessingr   r   multiprocessing.connectionr   r   ocrmypdfr   r   ocrmypdf._concurrentr   ocrmypdf.exceptionsr   ocrmypdf.helpersr   warnDeprecationWarningr   r4   r7   handlersQueueHandlerr9   r\   r^   r   r   r   r   r&   r'   <module>r      s   $ #     8 8   < < ) 7 ' 0 . 4 G$ 	NJ
(7++88 (
!3@LX L^ 
8 
8
 
# 
#
 
 
r&   