docstr: Lock manager with build-in timeout
  
  filename -- filename
  mode -- the open mode, 'a' or 'ab' should be used for writing
  truncate -- use truncate to emulate 'w' mode, None is disabled, 0 is
      truncate to 0 bytes
  timeout -- timeout when trying to acquire a lock
  check_interval -- check interval while waiting
  fail_when_locked -- after the initial lock failed, return an error
      or lock the file
  **file_open_kwargs -- The kwargs for the `open(...)` call
  
  fail_when_locked is useful when multiple threads/processes can race
  when creating a file. If set to true than the system will wait till
  the lock was acquired and then return an AlreadyLocked exception.
  
  Note that the file is opened first and locked later. So using 'w' as
  mode will result in truncate _BEFORE_ the lock is checked.
docstr_tok: ["Lock","manager","with","build-in","timeout","filename","--","filename","mode","--","the","open","mode",",","'","a","'","or","'ab","'","should","be","used","for","writing","truncate","--","use","truncate","to","emulate","'","w","'","mode",",","None","is","disabled",",","0","is","truncate","to","0","bytes","timeout","--","timeout","when","trying","to","acquire","a","lock","check_interval","--","check","interval","while","waiting","fail_when_locked","--","after","the","initial","lock","failed",",","return","an","error","or","lock","the","file","*","*","file_open_kwargs","--","The","kwargs","for","the","`","open","(","...",")","`","call","fail_when_locked","is","useful","when","multiple","threads\/processes","can","race","when","creating","a","file",".","If","set","to","true","than","the","system","will","wait","till","the","lock","was","acquired","and","then","return","an","AlreadyLocked","exception",".","Note","that","the","file","is","opened","first","and","locked","later",".","So","using","'","w","'","as","mode","will","result","in","truncate","_BEFORE_","the","lock","is","checked","."]
code: def __init__(
      self,
      filename,
      mode="a",
      timeout=DEFAULT_TIMEOUT,
      check_interval=DEFAULT_CHECK_INTERVAL,
      fail_when_locked=False,
      flags=LOCK_METHOD,
      **file_open_kwargs,
  ):
      """Lock manager with build-in timeout
  
      filename -- filename
      mode -- the open mode, 'a' or 'ab' should be used for writing
      truncate -- use truncate to emulate 'w' mode, None is disabled, 0 is
          truncate to 0 bytes
      timeout -- timeout when trying to acquire a lock
      check_interval -- check interval while waiting
      fail_when_locked -- after the initial lock failed, return an error
          or lock the file
      **file_open_kwargs -- The kwargs for the `open(...)` call
  
      fail_when_locked is useful when multiple threads/processes can race
      when creating a file. If set to true than the system will wait till
      the lock was acquired and then return an AlreadyLocked exception.
  
      Note that the file is opened first and locked later. So using 'w' as
      mode will result in truncate _BEFORE_ the lock is checked.
      """
  
      if "w" in mode:
          truncate = True
          mode = mode.replace("w", "a")
      else:
          truncate = False
  
      self.fh = None
      self.filename = filename
      self.mode = mode
      self.truncate = truncate
      self.timeout = timeout
      self.check_interval = check_interval
      self.fail_when_locked = fail_when_locked
      self.flags = flags
      self.file_open_kwargs = file_open_kwargs
  
imports: import os
  import time
  import atexit
  import tempfile
  import contextlib
  from .None import exceptions
  from .None import constants
  from .None import portalocker
label_desc: Use image node_id 1 to create a new Lock object from inherited base classes: object with example: obj = Lock(filename, mode, timeout, check_interval, fail_when_locked, flags)