o
    0׾g,                     @   sB  d Z ddlmZ ddlmZ ddlmZ ddlmZ zddlZW n e	y/   ddl
ZdZY nw ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dgZdgZee Zeee eddZ eeB Z!ej"Z"ej#Z#e"e#B Z$ej%Z%e&edej'Z(G dd de(Z)e)Z*dd Z+dS )an  
This module provides :class:`GeventSelector`, a high-level IO
multiplexing mechanism. This is aliased to :class:`DefaultSelector`.

This module provides the same API as the selectors defined in :mod:`selectors`.

On Python 2, this module is only available if the `selectors2
<https://pypi.org/project/selectors2/>`_ backport is installed.

.. versionadded:: 20.6.0
    )absolute_import)division)print_function)defaultdictN
selectors2)_get_hub_noargs)sleep)	iteritems)
itervalues)copy_globals)Lazy)Event)_EV_READ)	_EV_WRITEDefaultSelectorGeventSelector)__all__)names_to_ignoredunder_names_to_keep_BaseSelectorImplc                       sZ   e Zd ZdZd fdd	Zdd Zedd Zdd	d
Zdd Z	dddZ
dd Z  ZS )r   a  
    A selector implementation using gevent primitives.

    This is a type of :class:`selectors.BaseSelector`, so the documentation
    for that class applies here.

    .. caution::
       As the base class indicates, it is critically important to
       unregister file objects before closing them. (Or close the selector
       they are registered with before closing them.) Failure to do so
       may crash the process or have other unintended results.
    Nc                    s>   |d ur|| _ i | _i | _tt| _t | _tt	| 
  d S N)hub_active_watchers_inactive_watchersr   int_accumulated_eventsr   _readysuperr   __init__)selfr   	__class__ Z/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/selectors.pyr   n   s   
zGeventSelector.__init__c                 C   sJ   |dkr| j | }|t@ r|tO }|t@ r|tO }|| j |< | j  d S )Nr   )r   r   
EVENT_READr   EVENT_WRITEr   set)r   eventsfdcur_event_for_fdr"   r"   r#   
__callbacky   s   

zGeventSelector.__callbackc                 C   s   t  S r   )get_hub)r   r"   r"   r#   r      s   zGeventSelector.hubc           
      C   sf   t | |||}|tkrt}n	|tkrt}nt}| jj}|j	}|j
}||j| | j|j< }	||	_|S r   )r   register_ALL_EVENTS	_POLL_ALLr$   r   r   r   loopioMAXPRIr(   r   priority)
r   fileobjr'   datakeyflagsr/   r0   r1   watcherr"   r"   r#   r,      s   zGeventSelector.registerc                 C   sZ   t | |}|j| jv r| j|j}n| j|j}|  |  | j|jd  |S r   )	r   
unregisterr(   r   popr   stopcloser   )r   r3   r5   r7   r"   r"   r#   r8      s   zGeventSelector.unregisterc                 C   s   |dur
|dk r
d}t | jD ]\}}|j| j|dd q| j| j | j  | j r1t	  | j
| | j  g }t | jD ])\}}| |}| j|}|  |rh||||j@ f || j|< qC|  qC| j  |S )z
        Poll for I/O.

        Note that, like the built-in selectors, this will block
        indefinitely if no timeout is given and no files have been
        registered.
        Nr   T)pass_events)r	   r   start_GeventSelector__callbackr   updateclearr   is_setr   waitr   _key_from_fdr9   r:   appendr'   r;   )r   timeoutr(   r7   resulteventr5   r"   r"   r#   select   s*   





zGeventSelector.selectc                 C   s`   | j | jfD ]}|d u rqt|D ]
}|  |  qqd  | _ | _d | _d | _t|  d S r   )r   r   r
   r:   r;   r   r   r   )r   dr7   r"   r"   r#   r;      s   
zGeventSelector.closer   )__name__
__module____qualname____doc__r   r>   r   r   r,   r8   rH   r;   __classcell__r"   r"   r    r#   r   E   s    (


>c           	         s   | j d }| j}|   dd l}d|jvr|jt |jd< | dd}|j|us*J t}|j	}t
|drO|j|j|fv rOddlm   fdd}||j	_d	|_|r\| |d
dd t|_t
|drst
|jdruddlm} ||j_d S d S d S )N
aggressiver   	selectorsrH   _selectrH   c                    s    |i |S r   r"   )r   argskwargsrR   r"   r#   rQ     s   z(_gevent_do_monkey_patch.<locals>._selectTEpollSelectorKqueueSelectorDevpollSelectorPollSelector_selector_cls)poll)patch_kwargstarget_moduledefault_patch_itemssysmodulesrJ   get_originalrH   __selectors__SelectSelectorhasattrrQ   gevent.select_gevent_monkeyremove_itemr   rX   rZ   rY   )	patch_requestrO   
target_modr^   orig_select_selectrP   rb   rQ   rZ   r"   rR   r#   _gevent_do_monkey_patch   s:   

rj   ),rM   
__future__r   r   r   collectionsr   rP   ra   ImportErrorr   
__target__
gevent.hubr   r+   geventr   gevent._compatr	   r
   gevent._utilr   r   gevent.eventr   rd   r   r   __implements__	__extra__r   globals__imports__r.   r$   r%   r-   SelectorKeygetattrBaseSelectorr   r   r   rj   r"   r"   r"   r#   <module>   sX    2