a
    ƹDf7#                     @   sP   d Z ddgZi  ZZddlmZ ddlmZ ddl	m
Z
mZ G dd deZdS )	a  
This module contains map and pipe interfaces to python's threading module.

Pipe methods provided:
    pipe        - blocking communication pipe             [returns: value]
    apipe       - asynchronous communication pipe         [returns: object]

Map methods provided:
    map         - blocking and ordered worker pool        [returns: list]
    imap        - non-blocking and ordered worker pool    [returns: iterator]
    uimap       - non-blocking and unordered worker pool  [returns: iterator]
    amap        - asynchronous worker pool                [returns: object]


Usage
=====

A typical call to a pathos threading map will roughly follow this example:

    >>> # instantiate and configure the worker pool
    >>> from pathos.threading import ThreadPool
    >>> pool = ThreadPool(nodes=4)
    >>>
    >>> # do a blocking map on the chosen function
    >>> print(pool.map(pow, [1,2,3,4], [5,6,7,8]))
    >>>
    >>> # do a non-blocking map, then extract the results from the iterator
    >>> results = pool.imap(pow, [1,2,3,4], [5,6,7,8])
    >>> print("...")
    >>> print(list(results))
    >>>
    >>> # do an asynchronous map, then get the results
    >>> results = pool.amap(pow, [1,2,3,4], [5,6,7,8])
    >>> while not results.ready():
    ...     time.sleep(5); print(".", end=' ')
    ...
    >>> print(results.get())
    >>>
    >>> # do one item at a time, using a pipe
    >>> print(pool.pipe(pow, 1, 5))
    >>> print(pool.pipe(pow, 2, 6))
    >>>
    >>> # do one item at a time, using an asynchronous pipe
    >>> result1 = pool.apipe(pow, 1, 5)
    >>> result2 = pool.apipe(pow, 2, 6)
    >>> print(result1.get())
    >>> print(result2.get())


Notes
=====

This worker pool leverages the python's multiprocessing.dummy module, and thus
has many of the limitations associated with that module. The function f and
the sequences in args must be serializable. The maps in this worker pool
have full functionality whether run from a script or in the python
interpreter, and work reliably for both imported and interactively-defined
functions. Unlike python's multiprocessing.dummy module, pathos.threading maps
can directly utilize functions that require multiple arguments.


ThreadPool_ThreadPool    )AbstractWorkerPool)starargs)	cpu_countr   c                   @   s   e Zd ZdZdd Zejjr,ejjej e_d$ddZdd ZeZd	d
 Z	ej	je	_dd Z
ej
je
_dd Zejje_dd Zejje_dd Zdd Zdd Zdd Zdd Zd%ddZdd Zd d! Zd"d# ZeeeZeeeZeZdS )&r   z/
Mapper that leverages python's threading.
    c                 O   s   d|v }t |}d|v r.|s |r.d}t|n4|rR|rBd}t||d|d< n|rb|d |d< d|v rd|v r~d}t||d|d< |dt | _|dd	| _| jd	u r| j| _|| _|   d	S )
a5  
NOTE: if number of nodes is not given, will autodetect processors.

NOTE: additional keyword input is optional, with:
    id          - identifier for the pool
    initializer - function that takes no input, called when node is spawned
    initargs    - tuple of args for initializers that have args
        nodesnthreadsz3got multiple values for keyword argument 'nthreads'z0got multiple values for keyword argument 'nodes'r   Z	processesz4got multiple values for keyword argument 'processes'idN)len	TypeErrorpopr   _ThreadPool__nodes_id_kwds_serve)selfargskwdsZhasnodesZarglenmsg r   ]/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/pathos/threading.py__init__R   s.    

zThreadPool.__init__Nc                 C   sl   |du r| j }t| jd}|r6||j ks6| j|jkrh|   t|fi | j}||_ | j|_|t| j< |S )z4Create a new server if one isn't already initializedN)r   _ThreadPool__STATEgetr   r   _clearr   )r   r   _poolr   r   r   r   y   s    
zThreadPool._servec                 C   sL   t | jd}|rH| j|jkrH| j|jkrH|  |  t | jd dS )z!Remove server with matching stateN)r   r   r   r   r   closejoinr   r   r   r   r   r   r      s    zThreadPool._clearc                 O   s>   t j| |g|R i | |  }|jt|t| fi |S N)r   _AbstractWorkerPool__mapr   mapstarzipr   fr   r   r   r   r   r   r!      s    zThreadPool.mapc                 O   s>   t j| |g|R i | |  }|jt|t| fi |S r   )r   _AbstractWorkerPool__imapr   imapr"   r#   r$   r   r   r   r'      s    zThreadPool.imapc                 O   s>   t j| |g|R i | |  }|jt|t| fi |S r   )r   r&   r   Zimap_unorderedr"   r#   r$   r   r   r   uimap   s    zThreadPool.uimapc                 O   s>   t j| |g|R i | |  }|jt|t| fi |S r   )r   r    r   Z	map_asyncr"   r#   r$   r   r   r   amap   s    zThreadPool.amapc                 O   s   |   }||||S r   )r   applyr$   r   r   r   pipe   s    zThreadPool.pipec                 O   s   |   }||||S r   )r   Zapply_asyncr$   r   r   r   apipe   s    zThreadPool.apipec                 C   s   | j j| jf}d| S )Nz<pool %s(nthreads=%s)>)	__class____name__r   )r   Zmapargsr   r   r   __repr__   s    zThreadPool.__repr__c                 C   s   | j S )z'get the number of nodes used in the map)r   )r   r   r   r   Z__get_nodes   s    zThreadPool.__get_nodesc                 C   s   |  | || _dS )z'set the number of nodes used in the mapN)r   r   )r   r   r   r   r   Z__set_nodes   s    
zThreadPool.__set_nodesFc                 C   sz   t | jd}|rv| j|jkrv| j|jkrvd}|s@|j|ks@J |   t| jfi | j}| j|_| j|_|t | j< |S )zrestart a closed poolNr   )r   r   r   r   r   _stater   r   )r   forcer   ZRUNr   r   r   restart   s    
zThreadPool.restartc                 C   s*   t | jd}|r&| j|jkr&|  dS )zclose the pool to any new jobsN)r   r   r   r   r   r   r   r   r   r      s    zThreadPool.closec                 C   s*   t | jd}|r&| j|jkr&|  dS )za more abrupt closeN)r   r   r   r   	terminater   r   r   r   r3      s    zThreadPool.terminatec                 C   s*   t | jd}|r&| j|jkr&|  dS )z#cleanup the closed worker processesN)r   r   r   r   r   r   r   r   r   r      s    zThreadPool.join)N)F)r.   
__module____qualname____doc__r   r   r   r   clearr!   r'   r(   r)   r+   r,   r/   Z_ThreadPool__get_nodesZ_ThreadPool__set_nodesr2   r   r3   r   propertyr   r   r   Z	__state__r   r   r   r   r   N   s6   #







N)r6   __all__Z__STATEr   Zpathos.abstract_launcherr   Zpathos.helpers.mp_helperr   r"   Zpathos.helpersr   r   r   r   r   r   r   <module>   s   =