Current File : //usr/local/apps/python3/lib/python3.11/site-packages/asgiref/__pycache__/server.cpython-311.pyc
�

�rRhu��l�ddlZddlZddlZddlZddlmZeje��ZGd�d��Z	dS)�N�)�guarantee_single_callablec�N�eZdZdZdZdd�Zd�Zd�Zd�Zd�Z	d	�Z
d
�Zd�Zd�Z
d
S)�StatelessServerax
    Base server class that handles basic concepts like application instance
    creation/pooling, exception handling, and similar, for stateless protocols
    (i.e. ones without actual incoming connections to the process)

    Your code should override the handle() method, doing whatever it needs to,
    and calling get_or_create_application_instance with a unique `scope_id`
    and `scope` for the scope it wants to get.

    If an application instance is found with the same `scope_id`, you are
    given its input queue, otherwise one is made for you with the scope provided
    and you are given that fresh new input queue. Either way, you should do
    something like:

    input_queue = self.get_or_create_application_instance(
        "user-123456",
        {"type": "testprotocol", "user_id": "123456", "username": "andrew"},
    )
    input_queue.put_nowait(message)

    If you try and create an application instance and there are already
    `max_application` instances, the oldest/least recently used one will be
    reclaimed and shut down to make space.

    Application coroutines that error will be found periodically (every 100ms
    by default) and have their exceptions printed to the console. Override
    application_exception() if you want to do more when this happens.

    If you override run(), make sure you handle things like launching the
    application checker.
    g�������?��c�0�||_||_i|_dS�N)�application�max_applications�application_instances)�selfr
rs   �F/usr/local/apps/python3/lib/python3.11/site-packages/asgiref/server.py�__init__zStatelessServer.__init__.s ��&��� 0���%'��"�"�"�c��tj��}tj|�����	|�|�����dS#t$rt�d��YdSwxYw)zD
        Runs the asyncio event loop with our handler loop.
        zExiting due to Ctrl-C/interruptN)	�asyncio�get_event_loop�
ensure_future�application_checker�run_until_complete�handle�KeyboardInterrupt�logger�info)r
�
event_loops  r�runzStatelessServer.run7s����+�-�-�
���d�6�6�8�8�9�9�9�	;��)�)�$�+�+�-�-�8�8�8�8�8�� �	;�	;�	;��K�K�9�:�:�:�:�:�:�	;���s�'A$�$$B�Bc��$K�td���)NzYou must implement handle()��NotImplementedError)r
s rrzStatelessServer.handleBs����!�"?�@�@�@rc��$K�td���)zM
        Receives outbound sends from applications and handles them.
        z%You must implement application_send()r)r
�scope�messages   r�application_sendz StatelessServer.application_sendEs����"�"I�J�J�Jrc����|�jvr4tj���j|d<�j|dSt�j���jkr1����t�j���jk�1tj��}t�j��}tj	|�|j
��fd������}||�tj��d��j|<|S)zH
        Creates an application instance and returns its queue.
        �	last_used�input_queuec�0�����|��Sr	)r#)r"r!r
s ��r�<lambda>zDStatelessServer.get_or_create_application_instance.<locals>.<lambda>_s���T�%:�%:�5�'�%J�%J�r)r!�receive�send)r&�futurer!r%)r�time�lenr�"delete_oldest_application_instancer�Queuerr
r�get)r
�scope_idr!r&�application_instancer+s` `   r�"get_or_create_application_instancez2StatelessServer.get_or_create_application_instanceMs�����t�1�1�1�@D�	���D�&�x�0��=��-�h�7�
�F�F��$�,�-�-��0E�E�E��3�3�5�5�5��$�,�-�-��0E�E�E��m�o�o��8��9I�J�J���&� � ��#��J�J�J�J�J�
�
�
�
�
��'�������	0
�0
��"�8�,��rc���td�|j���D����}|j���D])\}}|d|kr|�|��dS�*dS)zC
        Finds and deletes the oldest application instance
        c3�&K�|]}|dV��
dS)r%N�)�.0�detailss  r�	<genexpr>zEStatelessServer.delete_oldest_application_instance.<locals>.<genexpr>ns8����
�
�%,�G�K� �
�
�
�
�
�
rr%N)�minr�values�items�delete_application_instance)r
�oldest_timer1r8s    rr.z2StatelessServer.delete_oldest_application_instancejs����
�
�04�0J�0Q�0Q�0S�0S�
�
�
�
�
��"&�!;�!A�!A�!C�!C�	�	��H�g��{�#�{�2�2��0�0��:�:�:����	3�	�	rc��|j|}|j|=|d���s|d���dSdS)z�
        Removes an application instance (makes sure its task is stopped,
        then removes it from the current set)
        r+N)r�done�cancel)r
r1r8s   rr=z+StatelessServer.delete_application_instancexs\��
�,�X�6���&�x�0��x� �%�%�'�'�	'��H��$�$�&�&�&�&�&�	'�	'rc��xK�	tj|j���d{V��t|j�����D]q\}}|d���rR|d���}|r|�||���d{V��	|j|=�a#t$rY�mwxYw�r��)z�
        Goes through the set of current application instance Futures and cleans up
        any that are done/prints exceptions for any that errored.
        TNr+)
r�sleep�application_checker_interval�listrr<r@�	exception�application_exception�KeyError)r
r1r8rFs    rrz#StatelessServer.application_checker�s�����
	��-�� A�B�B�B�B�B�B�B�B�B�%)�$�*D�*J�*J�*L�*L�%M�%M�	
�	
�!��'��8�$�)�)�+�+�� '�� 1� ;� ;� =� =�I� �M�"�8�8��G�L�L�L�L�L�L�L�L�L�� �6�x�@�@��#����������
�	s�!B*�*
B7�6B7c	��K�tjd|d�tj|j����d|����dS)zL
        Called whenever an application coroutine has an exception.
        z%Exception inside application: %s
%s%s�z  N)�logging�error�join�	traceback�	format_tb�
__traceback__)r
rF�application_detailss   rrGz%StatelessServer.application_exception�sT����	�
�4���G�G�I�'�	�(?�@�@�A�A�����		
�	
�	
�	
�	
rN)r)�__name__�
__module__�__qualname__�__doc__rDrrrr#r3r.r=rrGr6rrrrs���������@$'� �(�(�(�(�	;�	;�	;�A�A�A�K�K�K����:���'�'�'����$	
�	
�	
�	
�	
rr)
rrKr,rN�
compatibilityr�	getLoggerrRrrr6rr�<module>rXs�������������������4�4�4�4�4�4�	��	�8�	$�	$��R
�R
�R
�R
�R
�R
�R
�R
�R
�R
r