
    c                        d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ  ej        e          ZdZ G d d          Zedk    r	  ej        dej        	            ej        e dd
         ej                  Ze                                Ze                    dd                    ej                              ej        d e            d           e                    dej                   dS dS )a  Worker ("slave") process used in computing distributed Latent Semantic Indexing (LSI,
:class:`~gensim.models.lsimodel.LsiModel`) models.

Run this script on every node in your cluster. If you wish, you may even run it multiple times on a single machine,
to make better use of multiple cores (just beware that memory footprint increases linearly).


How to use distributed LSI
--------------------------

#. Install needed dependencies (Pyro4) ::

    pip install gensim[distributed]

#. Setup serialization (on each machine) ::

    export PYRO_SERIALIZERS_ACCEPTED=pickle
    export PYRO_SERIALIZER=pickle

#. Run nameserver ::

    python -m Pyro4.naming -n 0.0.0.0 &

#. Run workers (on each machine) ::

    python -m gensim.models.lsi_worker &

#. Run dispatcher ::

    python -m gensim.models.lsi_dispatcher &

#. Run :class:`~gensim.models.lsimodel.LsiModel` in distributed mode:

    .. sourcecode:: pycon

        >>> from gensim.test.utils import common_corpus, common_dictionary
        >>> from gensim.models import LsiModel
        >>>
        >>> model = LsiModel(common_corpus, id2word=common_dictionary, distributed=True)


Command line arguments
----------------------

.. program-output:: python -m gensim.models.lsi_worker --help
   :ellipsis: 0, -3

    N)lsimodel)utilsc                   X   e Zd Zd Zej        d             Zej        ej        d                         Z e	j
        d          d             Zej         e	j
        d          d                         Zej         e	j
        d          d                         Zej        d             Zd	S )
Workerc                     d| _         dS )zPartly initialize the model.

        A full initialization requires a call to :meth:`~gensim.models.lsi_worker.Worker.initialize`.

        N)modelselfs    8lib/python3.11/site-packages/gensim/models/lsi_worker.py__init__zWorker.__init__M   s     


    c                     t          j                    | _        d| _        || _        || _        d| _        t                              d|           t          j
        di || _        dS )a  Fully initialize the worker.

        Parameters
        ----------
        myid : int
            An ID number used to identify this worker in the dispatcher object.
        dispatcher : :class:`~gensim.models.lsi_dispatcher.Dispatcher`
            The dispatcher responsible for scheduling this worker.
        **model_params
            Keyword parameters to initialize the inner LSI model, see :class:`~gensim.models.lsimodel.LsiModel`.

        r   Fzinitializing worker #%sN )	threadingLocklock_updatejobsdonemyid
dispatcherfinishedloggerinfor   LsiModelr   )r
   r   r   model_paramss       r   
initializezWorker.initializeU   sa     %>++	$-t444&6666


r   c                    | j         t          d          d}|F| j        s?	 | j                            | j                  }n# t          j        $ r Y ;w xY w|| j        ?|\t          	                    d| j        | j
                   |                     |           | j                            | j                   dS t          	                    d| j                   dS )a  Request jobs from the dispatcher, in a perpetual loop until :meth:`~gensim.models.lsi_worker.Worker.getstate`
        is called.

        Raises
        ------
        RuntimeError
            If `self.model` is None (i.e. worker not initialized).

        Nz0worker must be initialized before receiving jobszworker #%s received job #%iz#worker #%i stopping asking for jobs)r   RuntimeErrorr   r   getjobr   QueueEmptyr   r   r   
processjobjobdone)r
   jobs     r   
requestjobzWorker.requestjobl   s     : 	SQRRR 	$- 	o,,TY77;     	$- 	  	JKK5ty$-PPPOOC   O##DI.....KK=tyIIIIIs   A AAr   c                 ,   | j                             |           | xj        dz  c_        t          r`| j        t          z  dk    rOt          j                            t          j                    d          }| j         	                    |           dS dS dS )zIncrementally process the job and potentially logs progress.

        Parameters
        ----------
        job : iterable of list of (int, float)
            Corpus in BoW format.

           r   zlsi_worker.pklN)
r   add_documentsr   
SAVE_DEBUGospathjointempfile
gettempdirsave)r
   r#   fnames      r   r!   zWorker.processjob   s     	
  %%% 	#$-*49 	#GLL!4!6!68HIIEJOOE"""""	# 	# 	# 	#r   c                     t                               d| j        | j                   t	          | j        j        t          j                  sJ d| _	        | j        j        S )zLog and get the LSI model's current projection.

        Returns
        -------
        :class:`~gensim.models.lsimodel.Projection`
            The current projection.

        z,worker #%i returning its state after %s jobsT)
r   r   r   r   
isinstancer   
projectionr   
Projectionr   r	   s    r   getstatezWorker.getstate   sO     	BDIt}]]]$*/1DEEEEEz$$r   c                     t                               d| j                   | j        j                                        | j        _        d| _        dS )z4Reset the worker by deleting its current projection.zresetting worker #%iFN)r   r   r   r   r2   
empty_liker   r	   s    r   resetzWorker.reset   sA     	*DI666 $
 5 @ @ B B
r   c                 n    t                               d| j                   t          j        d           dS )zTerminate the worker.zterminating worker #%ir   N)r   r   r   r)   _exitr	   s    r   exitzWorker.exit   s,     	,di888
r   N)__name__
__module____qualname__r   Pyro4exposer   onewayr$   r   synchronousr!   r4   r7   r:   r   r   r   r   r   L   s          \7 7 \7, \
\J J \ \J6 U}%%# # &%# \U}%%% % &% \% \U}%%  &% \ \  \  r   r   __main__z)%(asctime)s - %(levelname)s - %(message)s)formatleveliy)descriptionformatter_classz
running %s zgensim.lsi_workerT)random_suffixzfinished running %s)__doc__r)   sysloggingargparser   r,   queuer   r>   gensim.modelsr   gensimr   	getLoggerr;   r   r(   r   basicConfigINFOArgumentParserRawTextHelpFormatterparser
parse_args_r   r+   argvpyro_daemonprogr   r   r   <module>r[      s  / /b 
			 



             " " " " " "       
	8	$	$ 
i i i i i i i iX z 	4GJRYR^____$X$$QYQnoooFA
KKchhsx00111E)66884HHHH
KK%v{33333	4 	4r   