class_docstr: Sample cluster-wide memory usage every <interval> seconds.
  
  **Usage**
  
  .. code-block:: python
  
     client = Client()
     ms = MemorySampler()
  
     with ms.sample("run 1"):
         <run first workflow>
     with ms.sample("run 2"):
         <run second workflow>
     ...
     ms.plot()
  
  or with an asynchronous client:
  
  .. code-block:: python
  
     client = await Client(asynchronous=True)
     ms = MemorySampler()
  
     async with ms.sample("run 1"):
         <run first workflow>
     async with ms.sample("run 2"):
         <run second workflow>
     ...
     ms.plot()
class_docstr_tok: ["Sample","cluster-wide","memory","usage","every","<","interval",">","seconds",".","*","*","Usage","*","*","..","code-block",":",":","python","client","=","Client","(",")","ms","=","MemorySampler","(",")","with","ms.sample","(","``","run","1","''",")",":","<","run","first","workflow",">","with","ms.sample","(","``","run","2","''",")",":","<","run","second","workflow",">","...","ms.plot","(",")","or","with","an","asynchronous","client",":","..","code-block",":",":","python","client","=","await","Client","(","asynchronous=True",")","ms","=","MemorySampler","(",")","async","with","ms.sample","(","``","run","1","''",")",":","<","run","first","workflow",">","async","with","ms.sample","(","``","run","2","''",")",":","<","run","second","workflow",">","...","ms.plot","(",")"]
docstr: Context manager that records memory usage in the cluster.
  This is synchronous if the client is synchronous and
  asynchronous if the client is asynchronous.
  
  The samples are recorded in ``self.samples[<label>]``.
  
  Parameters
  ==========
  label: str, optional
      Tag to record the samples under in the self.samples dict.
      Default: automatically generate a random label
  client: Client, optional
      client used to connect to the scheduler.
      Default: use the global client
  measure: str, optional
      One of the measures from :class:`distributed.scheduler.MemoryState`.
      Default: sample process memory
  interval: float, optional
      sampling interval, in seconds.
      Default: 0.5
docstr_tok: ["Context","manager","that","records","memory","usage","in","the","cluster",".","This","is","synchronous","if","the","client","is","synchronous","and","asynchronous","if","the","client","is","asynchronous",".","The","samples","are","recorded","in","``","self.samples","[","<","label",">","]","``",".","Parameters","==========","label",":","str",",","optional","Tag","to","record","the","samples","under","in","the","self.samples","dict",".","Default",":","automatically","generate","a","random","label","client",":","Client",",","optional","client","used","to","connect","to","the","scheduler",".","Default",":","use","the","global","client","measure",":","str",",","optional","One","of","the","measures","from",":","class",":","`","distributed.scheduler.MemoryState","`",".","Default",":","sample","process","memory","interval",":","float",",","optional","sampling","interval",",","in","seconds",".","Default",":","0.5"]
code: def sample(
      self,
      label: str | None = None,
      *,
      client: Client | None = None,
      measure: str = "process",
      interval: float = 0.5,
  ) -> Any:
      """Context manager that records memory usage in the cluster.
      This is synchronous if the client is synchronous and
      asynchronous if the client is asynchronous.
  
      The samples are recorded in ``self.samples[<label>]``.
  
      Parameters
      ==========
      label: str, optional
          Tag to record the samples under in the self.samples dict.
          Default: automatically generate a random label
      client: Client, optional
          client used to connect to the scheduler.
          Default: use the global client
      measure: str, optional
          One of the measures from :class:`distributed.scheduler.MemoryState`.
          Default: sample process memory
      interval: float, optional
          sampling interval, in seconds.
          Default: 0.5
      """
      if not client:
          from distributed.client import get_client
  
          client = get_client()
  
      if client.asynchronous:
          return self._sample_async(label, client, measure, interval)
      else:
          return self._sample_sync(label, client, measure, interval)
  
file_path: distributed/distributed/diagnostics/memory_sampler.py
imports: from __future__ import annotations
  import uuid
  from collections.abc import AsyncIterator, Iterator
  from contextlib import asynccontextmanager, contextmanager
  from datetime import datetime
  from typing import TYPE_CHECKING, Any
  from distributed.compatibility import PeriodicCallback
label_desc: Use image node_id 2 for calling the MemorySampler obj's underlying member method code with example usage: obj.sample(label) and returns: self, self