code: def lock_file(path, timeout=3):
      """Lock a file
  
      path - path to the file
  
      timeout - timeout in seconds when waiting for announcement
  
      returns True if we obtained the lock, False if the file is already owned.
      """
      lock_path = get_lock_path(path)
      start_boundary()
      uid = uuid.uuid4().hex
      try:
          fd = os.open(lock_path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
          with os.fdopen(fd, "a") as f:
              f.write(
                  the_boundary.external_request_address + "\n" + uid
              )
      except OSError as e:
          if e.errno != errno.EEXIST:
              raise
          LOGGER.info(
              "Lockfile for %s already exists - contacting owner" % path
          )
          with open(lock_path, "r") as f:
              remote_address = f.readline().strip()
              remote_uid = f.readline().strip()
          if len(remote_address) > 0 and len(remote_uid) > 0:
              LOGGER.info("Owner is %s" % remote_address)
              request_socket = the_boundary.zmq_context.socket(zmq.REQ)
              request_socket.setsockopt(zmq.LINGER, 0)
              assert isinstance(request_socket, zmq.Socket)
              request_socket.connect(remote_address)
  
              lock_request = LockStatusRequest(remote_uid)
              lock_request.send_only(request_socket)
              poller = zmq.Poller()
              poller.register(request_socket, zmq.POLLIN)
              keep_polling = True
              while keep_polling:
                  keep_polling = False
                  for socket, status in poller.poll(timeout * 1000):
                      keep_polling = True
                      if (
                          socket == request_socket
                          and status == zmq.POLLIN
                      ):
                          lock_response = lock_request.recv(socket)
                          if isinstance(lock_response, LockStatusReply):
                              if lock_response.locked:
                                  LOGGER.info("%s is locked" % path)
                                  return False
                              keep_polling = False
          #
          # Fall through if we believe that the other end is dead
          #
          with open(lock_path, "w") as f:
              f.write(the_boundary.request_address + "\n" + uid)
      #
      # The coast is clear to lock
      #
      q = queue.Queue()
      start_lock_thread()
      __lock_queue.put((None, LOCK_REQUEST, (uid, path), q))
      q.get()
      return True
  
imports: import errno
  import json
  import logging
  import os
  import queue
  import sys
  import threading
  import uuid
  import numpy
  import zmq
  import cellprofiler_core.utilities.grid
  from cellprofiler_core.utilities.zmq._boundary import Boundary
  from cellprofiler_core.utilities.zmq.communicable.reply import LockStatusReply, Reply
  from cellprofiler_core.utilities.zmq.communicable.request import LockStatusRequest, Request
label_desc: Use image node_id 15 for calling a global function with example usage: lock_file(path, timeout) and returns: True, False