U
    /e                     @  s~   d dl mZ d dlZd dlmZ d dlZd dlmZ d dlm	Z	m
Z
 d dlmZmZmZ d dlmZ edd	d
Zdd ZdS )    )annotationsN)contextmanager)time)rejoinsecede)
get_client
get_workerthread_state)SecedeEventTc                 c  s   | dkrt jd} t j| d} t }t| d}|rlt tj	 }t
  |j|jttj|dt  d |V  |r|t  dS )a!  Get client for this thread

    This context manager is intended to be called within functions that we run
    on workers.  When run as a context manager it delivers a client
    ``Client`` object that can submit other tasks directly from that worker.

    Parameters
    ----------
    timeout : Number or String
        Timeout after which to error out. Defaults to the
        ``distributed.comm.timeouts.connect`` configuration value.
    separate_thread : bool, optional
        Whether to run this function outside of the normal thread pool
        defaults to True

    Examples
    --------
    >>> def func(x):
    ...     with worker_client(timeout="10s") as c:  # connect from worker back to scheduler
    ...         a = c.submit(inc, x)     # this task can submit more tasks
    ...         b = c.submit(dec, x)
    ...         result = c.gather([a, b])  # and gather results
    ...     return result

    >>> future = client.submit(func, 1)  # submit func(1) on cluster

    See Also
    --------
    get_worker
    get_client
    secede
    Nz!distributed.comm.timeouts.connects)timeoutzworker-client-secede-)keyZcompute_durationZstimulus_id)daskconfiggetutilsZparse_timedeltar   r   r   r	   
start_timer   ZloopZadd_callbackZhandle_stimulusr
   r   r   )r   Zseparate_threadZworkerclientduration r   =/tmp/pip-unpacked-wheel-g426oqom/distributed/worker_client.pyworker_client   s&    #

	r   c                  O  s   t d t| |S )Nz'local_client has moved to worker_client)warningswarnr   )argskwargsr   r   r   local_clientJ   s    
r   )NT)
__future__r   r   
contextlibr   r   Zdistributed.metricsr   Zdistributed.threadpoolexecutorr   r   Zdistributed.workerr   r   r	   Z distributed.worker_state_machiner
   r   r   r   r   r   r   <module>   s   ;