class_docstr: Used to keep track of local ports in order to prevent race conditions that
  can occur between port acquisition and usage by the kernel.  All locally-
  provisioned kernels should use this mechanism to limit the possibility of
  race conditions.  Note that this does not preclude other applications from
  acquiring a cached but unused port, thereby re-introducing the issue this
  class is attempting to resolve (minimize).
  See: https://github.com/jupyter/jupyter_client/issues/487
class_docstr_tok: ["Used","to","keep","track","of","local","ports","in","order","to","prevent","race","conditions","that","can","occur","between","port","acquisition","and","usage","by","the","kernel",".","All","locally-","provisioned","kernels","should","use","this","mechanism","to","limit","the","possibility","of","race","conditions",".","Note","that","this","does","not","preclude","other","applications","from","acquiring","a","cached","but","unused","port",",","thereby","re-introducing","the","issue","this","class","is","attempting","to","resolve","(","minimize",")",".","See",":","https",":","\/\/github.com\/jupyter\/jupyter_client\/issues\/487"]
code: def find_available_port(self, ip: str) -> int:
      while True:
          tmp_sock = socket.socket()
          tmp_sock.setsockopt(
              socket.SOL_SOCKET, socket.SO_LINGER, b"\0" * 8
          )
          tmp_sock.bind((ip, 0))
          port = tmp_sock.getsockname()[1]
          tmp_sock.close()
  
          # This is a workaround for https://github.com/jupyter/jupyter_client/issues/487
          # We prevent two kernels to have the same ports.
          if port not in self.currently_used_ports:
              self.currently_used_ports.add(port)
              return port
  
imports: from __future__ import annotations
  import errno
  import glob
  import json
  import os
  import socket
  import stat
  import tempfile
  import warnings
  from getpass import getpass
  from typing import TYPE_CHECKING, Any, Dict, Union, cast
  import zmq
  from jupyter_core.paths import jupyter_data_dir, jupyter_runtime_dir, secure_write
  from traitlets import Bool, CaselessStrEnum, Instance, Integer, Type, Unicode, observe
  from traitlets.config import LoggingConfigurable, SingletonConfigurable
  from .localinterfaces import localhost
  from .utils import _filefind
label_desc: Use image node_id 2 for calling the LocalPortCache obj's underlying member method code with example usage: obj.find_available_port(ip) and returns: port