Current File : //usr/local/apps/python3/lib/python3.11/site-packages/asgiref/__pycache__/server.cpython-311.pyc |
�
�rRhu � �l � d dl Z d dlZd dlZd dlZddlmZ ej e� � Z G d� d� � Z dS )� N� )�guarantee_single_callablec �N � e Zd ZdZdZdd�Zd� Zd� Zd� Zd� Z d � Z
d
� Zd� Zd� 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 | _ d S �N)�application�max_applications�application_instances)�selfr
r s �F/usr/local/apps/python3/lib/python3.11/site-packages/asgiref/server.py�__init__zStatelessServer.__init__. s � �&��� 0���%'��"�"�"� c � � t j � � }t j | � � � � � |� | � � � � � dS # t $ r t � d� � Y dS w xY w)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.run7 s� � � �+�-�-�
���d�6�6�8�8�9�9�9� ;��)�)�$�+�+�-�-�8�8�8�8�8�� � ;� ;� ;��K�K�9�:�:�:�:�:�:� ;���s �'A$ �$$B�Bc � �$ K � t d� � �)NzYou must implement handle()��NotImplementedError)r
s r r zStatelessServer.handleB s � � � �!�"?�@�@�@r c � �$ K � t d� � �)zM
Receives outbound sends from applications and handles them.
z%You must implement application_send()r )r
�scope�messages r �application_sendz StatelessServer.application_sendE s � � � � "�"I�J�J�Jr c � � �� |� j v r4t j � � � j | d<