class_docstr: Semaphore
  
  This `semaphore <https://en.wikipedia.org/wiki/Semaphore_(programming)>`_
  will track leases on the scheduler which can be acquired and
  released by an instance of this class. If the maximum amount of leases are
  already acquired, it is not possible to acquire more and the caller waits
  until another lease has been released.
  
  The lifetime or leases are controlled using a timeout. This timeout is
  refreshed in regular intervals by the ``Client`` of this instance and
  provides protection from deadlocks or resource starvation in case of worker
  failure.
  The timeout can be controlled using the configuration option
  ``distributed.scheduler.locks.lease-timeout`` and the interval in which the
  scheduler verifies the timeout is set using the option
  ``distributed.scheduler.locks.lease-validation-interval``.
  
  A noticeable difference to the Semaphore of the python standard library is
  that this implementation does not allow to release more often than it was
  acquired. If this happens, a warning is emitted but the internal state is
  not modified.
  
  .. warning::
  
      This implementation is susceptible to lease overbooking in case of
      lease timeouts. It is advised to monitor log information and adjust
      above configuration options to suitable values for the user application.
  
  Parameters
  ----------
  max_leases: int (optional)
      The maximum amount of leases that may be granted at the same time. This
      effectively sets an upper limit to the amount of parallel access to a specific resource.
      Defaults to 1.
  name: string (optional)
      Name of the semaphore to acquire.  Choosing the same name allows two
      disconnected processes to coordinate.  If not given, a random
      name will be generated.
  register: bool
      If True, register the semaphore with the scheduler. This needs to be
      done before any leases can be acquired. If not done during
      initialization, this can also be done by calling the register method of
      this class.
      When registering, this needs to be awaited.
  scheduler_rpc: ConnectionPool
      The ConnectionPool to connect to the scheduler. If None is provided, it
      uses the worker or client pool. This parameter is mostly used for
      testing.
  loop: IOLoop
      The event loop this instance is using. If None is provided, reuse the
      loop of the active worker or client.
  
  Examples
  --------
  >>> from distributed import Semaphore
  ... sem = Semaphore(max_leases=2, name='my_database')
  ...
  ... def access_resource(s, sem):
  ...     # This automatically acquires a lease from the semaphore (if available) which will be
  ...     # released when leaving the context manager.
  ...     with sem:
  ...         pass
  ...
  ... futures = client.map(access_resource, range(10), sem=sem)
  ... client.gather(futures)
  ... # Once done, close the semaphore to clean up the state on scheduler side.
  ... sem.close()
  
  Notes
  -----
  If a client attempts to release the semaphore but doesn't have a lease acquired, this will raise an exception.
  
  
  When a semaphore is closed, if, for that closed semaphore, a client attempts to:
  
  - Acquire a lease: an exception will be raised.
  - Release: a warning will be logged.
  - Close: nothing will happen.
  
  
  dask executes functions by default assuming they are pure, when using semaphore acquire/releases inside
  such a function, it must be noted that there *are* in fact side-effects, thus, the function can no longer be
  considered pure. If this is not taken into account, this may lead to unexpected behavior.
class_docstr_tok: ["Semaphore","This","`","semaphore","<","https",":","\/\/en.wikipedia.org\/wiki\/Semaphore_","(","programming",")",">","`","_","will","track","leases","on","the","scheduler","which","can","be","acquired","and","released","by","an","instance","of","this","class",".","If","the","maximum","amount","of","leases","are","already","acquired",",","it","is","not","possible","to","acquire","more","and","the","caller","waits","until","another","lease","has","been","released",".","The","lifetime","or","leases","are","controlled","using","a","timeout",".","This","timeout","is","refreshed","in","regular","intervals","by","the","``","Client","``","of","this","instance","and","provides","protection","from","deadlocks","or","resource","starvation","in","case","of","worker","failure",".","The","timeout","can","be","controlled","using","the","configuration","option","``","distributed.scheduler.locks.lease-timeout","``","and","the","interval","in","which","the","scheduler","verifies","the","timeout","is","set","using","the","option","``","distributed.scheduler.locks.lease-validation-interval","``",".","A","noticeable","difference","to","the","Semaphore","of","the","python","standard","library","is","that","this","implementation","does","not","allow","to","release","more","often","than","it","was","acquired",".","If","this","happens",",","a","warning","is","emitted","but","the","internal","state","is","not","modified",".","..","warning",":",":","This","implementation","is","susceptible","to","lease","overbooking","in","case","of","lease","timeouts",".","It","is","advised","to","monitor","log","information","and","adjust","above","configuration","options","to","suitable","values","for","the","user","application",".","Parameters","--","--","--","--","--","max_leases",":","int","(","optional",")","The","maximum","amount","of","leases","that","may","be","granted","at","the","same","time",".","This","effectively","sets","an","upper","limit","to","the","amount","of","parallel","access","to","a","specific","resource",".","Defaults","to","1.","name",":","string","(","optional",")","Name","of","the","semaphore","to","acquire",".","Choosing","the","same","name","allows","two","disconnected","processes","to","coordinate",".","If","not","given",",","a","random","name","will","be","generated",".","register",":","bool","If","True",",","register","the","semaphore","with","the","scheduler",".","This","needs","to","be","done","before","any","leases","can","be","acquired",".","If","not","done","during","initialization",",","this","can","also","be","done","by","calling","the","register","method","of","this","class",".","When","registering",",","this","needs","to","be","awaited",".","scheduler_rpc",":","ConnectionPool","The","ConnectionPool","to","connect","to","the","scheduler",".","If","None","is","provided",",","it","uses","the","worker","or","client","pool",".","This","parameter","is","mostly","used","for","testing",".","loop",":","IOLoop","The","event","loop","this","instance","is","using",".","If","None","is","provided",",","reuse","the","loop","of","the","active","worker","or","client",".","Examples","--","--","--","--",">",">",">","from","distributed","import","Semaphore","...","sem","=","Semaphore","(","max_leases=2",",","name='my_database","'",")","...","...","def","access_resource","(","s",",","sem",")",":","...","#","This","automatically","acquires","a","lease","from","the","semaphore","(","if","available",")","which","will","be","...","#","released","when","leaving","the","context","manager",".","...","with","sem",":","...","pass","...","...","futures","=","client.map","(","access_resource",",","range","(","10",")",",","sem=sem",")","...","client.gather","(","futures",")","...","#","Once","done",",","close","the","semaphore","to","clean","up","the","state","on","scheduler","side",".","...","sem.close","(",")","Notes","--","--","-","If","a","client","attempts","to","release","the","semaphore","but","does","n't","have","a","lease","acquired",",","this","will","raise","an","exception",".","When","a","semaphore","is","closed",",","if",",","for","that","closed","semaphore",",","a","client","attempts","to",":","-","Acquire","a","lease",":","an","exception","will","be","raised",".","-","Release",":","a","warning","will","be","logged",".","-","Close",":","nothing","will","happen",".","dask","executes","functions","by","default","assuming","they","are","pure",",","when","using","semaphore","acquire\/releases","inside","such","a","function",",","it","must","be","noted","that","there","*","are","*","in","fact","side-effects",",","thus",",","the","function","can","no","longer","be","considered","pure",".","If","this","is","not","taken","into","account",",","this","may","lead","to","unexpected","behavior","."]
code: def _bind_late(self):
      if self._scheduler is None or self._loop is None:
          try:
              try:
                  worker = get_worker()
                  self._scheduler = self._scheduler or worker.scheduler
                  self._loop = self._loop or worker.loop
  
              except ValueError:
                  client = get_client()
                  self._scheduler = self._scheduler or client.scheduler
                  self._loop = self._loop or client.loop
          except ValueError:
              pass
  
imports: from __future__ import annotations
  import asyncio
  import logging
  import uuid
  import warnings
  from asyncio import TimeoutError
  from collections import defaultdict, deque
  import dask
  from dask.utils import parse_timedelta
  from distributed.compatibility import PeriodicCallback
  from distributed.metrics import time
  from distributed.utils import Deadline, SyncMethodMixin, log_errors, wait_for
  from distributed.utils_comm import retry_operation
  from distributed.worker import get_client, get_worker
label_desc: Use image node_id 4 for calling the Semaphore obj's underlying member method code with example usage: obj._bind_late() without return types