a
    !f[                     @   s   d Z ddlZddlZddlZddlZddlmZmZmZ ddl	Z	ej
rRddlmZ G dd dejdZejjdd	d
ZG dd deZdS )zSchedulers provide means to *schedule* callbacks asynchronously.

These are used by the subscriber to call the user-provided callback to process
each message.
    N)CallableListOptional)	pubsub_v1c                   @   s\   e Zd ZdZeejejdddZeje	ddddZ
ejdeed
 dddZdS )	SchedulerzgAbstract base class for schedulers.

    Schedulers are used to schedule callbacks asynchronously.
    returnc                 C   s   t dS )zQueue: A concurrency-safe queue specific to the underlying
        concurrency implementation.

        This queue is used to send messages *back* to the scheduling actor.
        NNotImplementedErrorself r   l/var/www/html/python-backend/venv/lib/python3.9/site-packages/google/cloud/pubsub_v1/subscriber/scheduler.pyqueue&   s    zScheduler.queueNcallbackr   c                 O   s   t dS )a  Schedule the callback to be called asynchronously.

        Args:
            callback: The function to call.
            args: Positional arguments passed to the callback.
            kwargs: Key-word arguments passed to the callback.

        Returns:
            None
        Nr	   r   r   argskwargsr   r   r   schedule0   s    zScheduler.scheduleF$pubsub_v1.subscriber.message.Messageawait_msg_callbacksr   c                 C   s   t dS )a  Shuts down the scheduler and immediately end all pending callbacks.

        Args:
            await_msg_callbacks:
                If ``True``, the method will block until all currently executing
                callbacks are done processing. If ``False`` (default), the
                method will not wait for the currently running callbacks to complete.

        Returns:
            The messages submitted to the scheduler that were not yet dispatched
            to their callbacks.
            It is assumed that each message was submitted to the scheduler as the
            first positional argument to the provided callback.
        Nr	   )r   r   r   r   r   shutdown>   s    zScheduler.shutdown)F)__name__
__module____qualname____doc__propertyabcabstractmethodr   Queuer   r   boolr   r   r   r   r   r   r       s    r   )	metaclassr   c                   C   s   t jjdddS )N
   z"ThreadPoolExecutor-ThreadScheduler)max_workersthread_name_prefix)
concurrentfuturesThreadPoolExecutorr   r   r   r   "_make_default_thread_pool_executorS   s    r*   c                   @   sZ   e Zd ZdZdeejj dddZe	dd Z
eddd	d
Zdeed dddZdS )ThreadSchedulera$  A thread pool-based scheduler. It must not be shared across
       SubscriberClients.

    This scheduler is useful in typical I/O-bound message processing.

    Args:
        executor:
            An optional executor to use. If not specified, a default one
            will be created.
    N)executorc                 C   s&   t  | _|d u rt | _n|| _d S )N)r   r!   _queuer*   	_executor)r   r,   r   r   r   __init__e   s    

zThreadScheduler.__init__c                 C   s   | j S )zfQueue: A thread-safe queue used for communication between callbacks
        and the scheduling thread.)r-   r   r   r   r   r   n   s    zThreadScheduler.queuer   c                 O   sF   z| j j|g|R i | W n" ty@   tjdtdd Y n0 dS )a(  Schedule the callback to be called asynchronously in a thread pool.

        Args:
            callback: The function to call.
            args: Positional arguments passed to the callback.
            kwargs: Key-word arguments passed to the callback.

        Returns:
            None
        z.Scheduling a callback after executor shutdown.   )category
stacklevelN)r.   submitRuntimeErrorwarningswarnRuntimeWarningr   r   r   r   r   t   s    zThreadScheduler.scheduleFr   r   c                 C   s\   g }z0| j jjdd}|du r q||jd  qW n tjyH   Y n0 | j j|d |S )a  Shut down the scheduler and immediately end all pending callbacks.

        Args:
            await_msg_callbacks:
                If ``True``, the method will block until all currently executing
                executor threads are done processing. If ``False`` (default), the
                method will not wait for the currently running threads to complete.

        Returns:
            The messages submitted to the scheduler that were not yet dispatched
            to their callbacks.
            It is assumed that each message was submitted to the scheduler as the
            first positional argument to the provided callback.
        F)blockNr   )wait)r.   Z_work_queuegetappendr   r   Emptyr   )r   r   Zdropped_messagesZ	work_itemr   r   r   r      s    zThreadScheduler.shutdown)N)F)r   r   r   r   r   r'   r(   r)   r/   r   r   r   r   r"   r   r   r   r   r   r   r+   Y   s    
	
 r+   )r   r   concurrent.futuresr'   r   typingr   r   r   r5   TYPE_CHECKINGZgoogle.cloudr   ABCMetar   r(   r)   r*   r+   r   r   r   r   <module>   s   3