code: def add_accept_handler(
      sock: socket.socket,
      callback: Callable[[socket.socket, Any], None],
  ) -> Callable[[], None]:
      """Adds an `.IOLoop` event handler to accept new connections on ``sock``.
  
      When a connection is accepted, ``callback(connection, address)`` will
      be run (``connection`` is a socket object, and ``address`` is the
      address of the other end of the connection).  Note that this signature
      is different from the ``callback(fd, events)`` signature used for
      `.IOLoop` handlers.
  
      A callable is returned which, when called, will remove the `.IOLoop`
      event handler and stop processing further incoming connections.
  
      .. versionchanged:: 5.0
         The ``io_loop`` argument (deprecated since version 4.1) has been removed.
  
      .. versionchanged:: 5.0
         A callable is returned (``None`` was returned before).
      """
      io_loop = IOLoop.current()
      removed = [False]
  
      def accept_handler(fd: socket.socket, events: int) -> None:
          # More connections may come in while we're handling callbacks;
          # to prevent starvation of other tasks we must limit the number
          # of connections we accept at a time.  Ideally we would accept
          # up to the number of connections that were waiting when we
          # entered this method, but this information is not available
          # (and rearranging this method to call accept() as many times
          # as possible before running any callbacks would have adverse
          # effects on load balancing in multiprocess configurations).
          # Instead, we use the (default) listen backlog as a rough
          # heuristic for the number of connections we can reasonably
          # accept at once.
          for i in range(_DEFAULT_BACKLOG):
              if removed[0]:
                  # The socket was probably closed
                  return
              try:
                  connection, address = sock.accept()
              except BlockingIOError:
                  # EWOULDBLOCK indicates we have accepted every
                  # connection that is available.
                  return
              except ConnectionAbortedError:
                  # ECONNABORTED indicates that there was a connection
                  # but it was closed while still in the accept queue.
                  # (observed on FreeBSD).
                  continue
              callback(connection, address)
  
      def remove_handler() -> None:
          io_loop.remove_handler(sock)
          removed[0] = True
  
      io_loop.add_handler(sock, accept_handler, IOLoop.READ)
      return remove_handler
  
imports: import asyncio
  import concurrent.futures
  import errno
  import os
  import sys
  import socket
  import ssl
  import stat
  from tornado.concurrent import dummy_executor, run_on_executor
  from tornado.ioloop import IOLoop
  from tornado.util import Configurable, errno_from_exception
  from typing import List, Callable, Any, Type, Dict, Union, Tuple, Awaitable, Optional
label_desc: Use image node_id 2 for calling a global function with example usage: add_accept_handler(sock, callback) and returns: remove_handler