Exemple #1
0
  def GenerateUnique(cls, name: str) -> "RunId":
    """Generate a unique run ID with the given name.

    The uniqueness of run IDs is provided by storing the most-recently generated
    run ID in /tmp/ml4pl_previous_run_id.txt. If run IDs are requested at the
    same time, their timestamps will collide, and this method will block until
    the run ID is unique. Note the implementation is not truly atomic - there is
    still an incredibly slight possibility of generating duplicate run IDs if
    enough concurrent jobs attempt to grab run IDs at the same time.

    Args:
      name: The name of the run ID to generate.

    Returns:
      A run ID instance.
    """
    # Truncate the name if required.
    name = name[:16]

    # Compute a new run ID.
    timestamp = time.strftime("%y%m%d%H%M%S")
    hostname = system.HOSTNAME[:12]
    run_id = f"{name}:{timestamp}:{hostname}"

    # Begin inter-process locked region.
    with fasteners.InterProcessLock(_RUN_ID_LOCK_PATH):
      # Check if there is already a run with this ID.
      previous_run_id_path = filesystem_paths.TemporaryFilePath(
        f"previous_run_id_{name}.txt"
      )
      previous_run_id = None
      if previous_run_id_path.is_file():
        previous_run_id = fs.Read(previous_run_id_path)

      # The run ID is unique, so write it and return it.
      if run_id != previous_run_id:
        previous_run_id_path.parent.mkdir(exist_ok=True, parents=True)
        fs.Write(previous_run_id_path, run_id.encode("utf-8"))
        return RunId.FromString(run_id)
    # End of inter-process locked region.

    # We didn't get a unique run ID, so try again.
    time.sleep(0.2)
    return cls.GenerateGlobalUnique()
Exemple #2
0
import time
from typing import NamedTuple
from typing import Union

import fasteners
import sqlalchemy as sql

from deeplearning.ml4pl import filesystem_paths
from labm8.py import app
from labm8.py import fs
from labm8.py import system


FLAGS = app.FLAGS

_RUN_ID_LOCK_PATH = filesystem_paths.TemporaryFilePath("run_id.lock")


RUN_ID_MAX_LEN: int = 40
RUN_ID_REGEX = re.compile(
  r"(?P<script_name>[A-Za-z0-9_]+):"
  r"(?P<timestamp>[0-9]{12}):"
  r"(?P<hostname>[A-Za-z0-9]+)"
)


class RunId(NamedTuple):
  """A run ID.

  A run ID is a <= 40 character string with the format:
Exemple #3
0
import re
import sys
import time
from typing import NamedTuple
from typing import Union

import sqlalchemy as sql

from deeplearning.ml4pl import filesystem_paths
from labm8.py import app
from labm8.py import fs
from labm8.py import system

FLAGS = app.FLAGS

_PREVIOUS_RUN_ID_PATH = filesystem_paths.TemporaryFilePath(
    "previous_run_id.txt")

RUN_ID_MAX_LEN: int = 40
RUN_ID_REGEX = re.compile(r"(?P<script_name>[A-Za-z0-9_]+):"
                          r"(?P<timestamp>[0-9]{12}):"
                          r"(?P<hostname>[A-Za-z0-9]+)")


class RunId(NamedTuple):
    """A run ID.

  A run ID is a <= 40 character string with the format:

      <script_name>:<timestamp>:<hostname>

  Where <script_name> is a <= 16 char script stem, <timestamp> is a UTC-formatted