code: def find_free_network_port() -> int:
      """Finds a free port on localhost."""
      if constants.LIGHTNING_CLOUDSPACE_HOST is not None:
          return _find_free_network_port_cloudspace()
  
      port = None
  
      for _ in range(10):
          sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          sock.bind(("", 0))
          port = sock.getsockname()[1]
          sock.close()
  
          if port not in _reserved_ports:
              break
  
      if port in _reserved_ports:
          # Prevent an infinite loop, if we tried 10 times and didn't get a free port then something is wrong
          raise RuntimeError(
              "Couldn't find a free port. Please open an issue at `https://github.com/Lightning-AI/lightning/issues`."
          )
  
      _reserved_ports.add(port)
      return port
  
imports: import socket
  from functools import wraps
  from typing import Any, Callable, Dict, Optional
  from urllib.parse import urljoin
  import requests
  from lightning_cloud.rest_client import GridRestClient, LightningClient, create_swagger_client
  from requests import Session
  from requests.adapters import HTTPAdapter
  from requests.exceptions import ConnectionError, ConnectTimeout, ReadTimeout
  from urllib3.util.retry import Retry
  from lightning.app.core import constants
  from lightning.app.utilities.app_helpers import Logger
label_desc: Use image node_id 1 for calling a global function with example usage: find_free_network_port() and returns: port, _find_free_network_port_cloudspace