o
    wi                     @   s`   d dl Z d dlZd dlZd dlT ddlmZ ddlmZmZm	Z	 G dd dZ
G dd	 d	ZdS )
    N)*   )InferResult)get_cancelled_errorget_error_grpcraise_errorc                   @   sJ   e Zd ZdZdd Zdd ZdddZd	d
 Zdd Zdd Z	dd Z
dS )_InferStreama  Supports sending inference requests and receiving corresponding
    requests on a gRPC bi-directional stream.

    Parameters
    ----------
    callback : function
        Python function that is invoked upon receiving response from
        the underlying stream. The function must reserve the last two
        arguments (result, error) to hold :py:class:`InferResult` and
        :py:class:`InferenceServerException` objects respectively which will be
        provided to the function when executing the callback. The
        ownership of these objects will be given to the user. The
        'error' would be None for a successful inference.
    verbose : bool
        Enables verbose mode if set True.
    c                 C   s2   || _ || _t | _d | _d| _d| _d | _d S )NFT)		_callback_verbosequeueQueue_request_queue_handler
_cancelled_active_response_iterator)selfcallbackverbose r   \/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/tritonclient/grpc/_infer_stream.py__init__9   s   

z_InferStream.__init__c                 C   s   | j dd d S )NT)cancel_requests)closer   r   r   r   __del__B   s   z_InferStream.__del__Fc                 C   sf   |r| j r| j   d| _| jdur1| js| jd | j r,| j  | jr,t	d d| _dS dS )a  Gracefully close underlying gRPC streams.
        If cancel_requests is set True, then client cancels all the
        pending requests and closes the stream. If set False, the
        call blocks till all the pending requests on the stream are
        processed.
        TNzstream stopped...)
r   cancelr   r   r   putis_alivejoinr
   print)r   r   r   r   r   r   E   s   





z_InferStream.closec                 C   sH   || _ | jdurtd tj| jd| _| j  | jr"td dS dS )zInitializes the handler to process the response from
        stream and execute the callbacks.

        Parameters
        ----------
        response_iterator : iterator
            The iterator over the gRPC response stream.

        Nz7Attempted to initialize already initialized InferStream)targetzstream started...)	r   r   r   	threadingThread_process_responsestartr
   r    )r   response_iteratorr   r   r   _init_handlerY   s   


z_InferStream._init_handlerc                 C   s"   | j r| j| dS td dS )zEnqueues the specified request object to be provided
        in gRPC request stream.

        Parameters
        ----------
        request : ModelInferRequest
            The protobuf message holding the ModelInferRequest

        zThe stream is no longer in valid state, the error detail is reported through provided callback. A new stream should be started after stopping the current stream.N)r   r   r   r   r   requestr   r   r   _enqueue_requestl   s
   
z_InferStream._enqueue_requestc                 C   s   | j  }|S )aY  Returns the request details in the order they were added.
        The call to this function will block until the requests
        are available in the queue. InferStream._enqueue_request
        adds the request to the queue.

        Returns
        -------
        protobuf message
            The ModelInferRequest protobuf message.

        )r   getr(   r   r   r   _get_request   s   
z_InferStream._get_requestc              
   C   s   z,| j D ]%}| jrt| d }}|jdkrt|jd}nt|j}| j||d qW dS  tj	y` } z'| j 
 | _| tjjkrJt| }nt|}| jd|d W Y d}~dS d}~ww )ztWorker thread function to iterate through the response stream and
        executes the provided callbacks.

        N )msg)resulterror)r   r
   r    error_messageInferenceServerExceptionr   infer_responser	   grpcRpcError	is_activer   code
StatusCode	CANCELLEDr   detailsr   )r   responser/   r0   	rpc_errorr   r   r   r$      s$   


	z_InferStream._process_responseN)F)__name__
__module____qualname____doc__r   r   r   r'   r*   r,   r$   r   r   r   r   r   '   s    	
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_RequestIteratorzAn iterator class to provide data to gRPC request stream.

    Parameters
    ----------
    stream : InferStream
        The InferStream that holds the context to an active stream.

    c                 C   s
   || _ d S N)_stream)r   streamr   r   r   r      s   
z_RequestIterator.__init__c                 C   s   | S rB   r   r   r   r   r   __iter__   s   z_RequestIterator.__iter__c                 C   s   | j  }|d u rt|S rB   )rC   r,   StopIterationr(   r   r   r   __next__   s   
z_RequestIterator.__next__N)r=   r>   r?   r@   r   rE   rG   r   r   r   r   rA      s
    	rA   )r   r"   r4   tritonclient.utils_infer_resultr   _utilsr   r   r   r   rA   r   r   r   r   <module>   s    