
    (phZ                     r    S r SSKrSSKrSSKr " S S5      r " S S\R
                  R                  5      rg)zm
The code is derived from
https://github.com/althonos/pronto/commit/3384010dfb4fc7c66a219f59276adef3288a886b
    Nc                   <    \ rS rSrSrSS jrS	S jr\S 5       rSr	g)
SharedCounter   aK  A synchronized shared counter.

The locking done by multiprocessing.Value ensures that only a single
process or thread may read or write the in-memory ctypes object. However,
in order to do n += 1, Python performs a read followed by a write, so a
second process may read the old value before the new one is written by
the first process. The solution is to use a multiprocessing.Lock to
guarantee the atomicity of the modifications to Value.

This class comes almost entirely from Eli Bendersky's blog:
http://eli.thegreenplace.net/2012/01/04/shared-counter-with-pythons-multiprocessing/
c                 <    [         R                  " SU5      U l        g )Ni)multiprocessingValuecountselfns     B/var/www/html/venv/lib/python3.13/site-packages/django_q/queues.py__init__SharedCounter.__init__   s    $**32
    c                     U R                   R                  5          U R                   =R                  U-  sl        SSS5        g! , (       d  f       g= f)z(Increment the counter by n (default = 1)N)r
   get_lockvaluer   s     r   	incrementSharedCounter.increment   s3    ZZ  "JJ! #""s    A
Ac                 .    U R                   R                  $ )zReturn the value of the counter)r
   r   r   s    r   r   SharedCounter.value!   s     zzr   )r
   N)r   )   )
__name__
__module____qualname____firstlineno____doc__r   r   propertyr   __static_attributes__ r   r   r   r      s%    3"
    r   r   c                   x   ^  \ rS rSrSrU 4S jrU 4S jrU 4S jrU 4S jrU 4S jr	S\
4S	 jrS\4S
 jrSrU =r$ )Queue'   aF  A portable implementation of multiprocessing.Queue.

Because of multithreading / multiprocessing semantics, Queue.qsize() may
raise the NotImplementedError exception on Unix platforms like Mac OS X
where sem_getvalue() is not implemented. This subclass addresses this
problem by using a synchronized shared counter (initialized to zero) and
increasing / decreasing its value every time the put() and get() methods
are called, respectively. This not only prevents NotImplementedError from
being raised, but also allows us to implement a reliable version of both
qsize() and empty().
c                    > [         R                  S:  a  [        [        U ]  " U0 UD6  O)[        [        U ]  " US[
        R                  " 5       0UD6  [        S5      U l        g )N)   r   ctxr   )	sysversion_infosuperr$   r   r   get_contextr   sizer   argskwargs	__class__s      r   r   Queue.__init__4   s^    f$%'88%'*668<B "!$	r   c                 D   > [         [        U ]  5       U R                  4-   $ )N)r+   r$   __getstate__r-   )r   r1   s    r   r4   Queue.__getstate__=   s    UD.0DII<??r   c                 F   > [         [        U ]  US S 5        US   U l        g N)r+   r$   __setstate__r-   )r   stater1   s     r   r9   Queue.__setstate__@   s#    eT'cr
3"I	r   c                 d   > [         [        U ]
  " U0 UD6  U R                  R	                  S5        g )Nr   )r+   r$   putr-   r   r.   s      r   r=   	Queue.putD   s)    eT//		Ar   c                 f   > [         [        U ]
  " U0 UD6nU R                  R	                  S5        U$ r7   )r+   r$   getr-   r   )r   r/   r0   xr1   s       r   r@   	Queue.getH   s0    %"D3F3		Br   returnc                 .    U R                   R                  $ )z8Reliable implementation of multiprocessing.Queue.qsize())r-   r   r   s    r   qsizeQueue.qsizeM   s    yyr   c                 *    U R                  5       S:  + $ )z8Reliable implementation of multiprocessing.Queue.empty()r   )rE   r   s    r   emptyQueue.emptyQ   s    ::<!##r   )r-   )r   r   r   r   r   r   r4   r9   r=   r@   intrE   boolrH   r!   __classcell__)r1   s   @r   r$   r$   '   s?    
%@
s $t $ $r   r$   )r   r   multiprocessing.queuesr)   r   queuesr$   r"   r   r   <module>rO      s7   
   
   8,$O""(( ,$r   