o
    e                     @   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 G dd dZ	G dd	 d	Z
dS )
    N)*   )InferResult)get_error_grpcraise_errorc                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )_InferStreamap  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 InferResult and
        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.
    c                 C   s&   || _ || _t | _d | _d| _d S )NT)	_callback_verbosequeueQueue_request_queue_handler_active)selfcallbackverbose r   OD:\Projects\ConvertPro\env\Lib\site-packages\tritonclient/grpc/_infer_stream.py__init__7   s
   

z_InferStream.__init__c                 C   s   |    d S N)closer   r   r   r   __del__>   s   z_InferStream.__del__c                 C   sF   | j dur!| jd | j  r| j   | jrtd d| _ dS dS )zGracefully close underlying gRPC streams. Note that this call
        blocks till response of all currently enqueued requests are not
        received.
        Nzstream stopped...)r   r   putis_alivejoinr	   printr   r   r   r   r   A   s   



z_InferStream.closec                 C   sF   | j dur	td tj| j|f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)targetargszstream started...)r   r   	threadingThread_process_responsestartr	   r   )r   Zresponse_iteratorr   r   r   _init_handlerN   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_requestb   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_requestu   s   
z_InferStream._get_requestc              
   C   s   z+|D ]%}| j rt| d }}|jdkrt|jd}nt|j}| j||d qW dS  tjyO } z|	 | _
t|}| jd|d W Y d}~dS d}~ww )a  Worker thread function to iterate through the response stream and
        executes the provided callbacks.

        Parameters
        ----------
        responses : iterator
            The iterator to the response from the server for the
            requests in the stream.

        N )msg)resulterror)r	   r   error_messageZInferenceServerExceptionr   Zinfer_responser   grpcZRpcErrorZ	is_activer   r   )r   	responsesresponser+   r,   Z	rpc_errorr   r   r   r!      s    

	
z_InferStream._process_responseN)__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 r   )_stream)r   streamr   r   r   r      s   
z_RequestIterator.__init__c                 C   s   | S r   r   r   r   r   r   __iter__   s   z_RequestIterator.__iter__c                 C   s   | j  }|d u rt|S r   )r6   r(   StopIterationr$   r   r   r   __next__   s   
z_RequestIterator.__next__N)r1   r2   r3   r4   r   r8   r:   r   r   r   r   r5      s
    	r5   )r
   r   r.   Ztritonclient.utilsZ_infer_resultr   _utilsr   r   r   r5   r   r   r   r   <module>   s   |