import multiprocessing
import time
import queue


def get_queue_size(
    a_queue: multiprocessing.Queue, timeout: float = 0.25) -> int:
  """Return the size of the queue by reading everything from it.

  The built-in Queue.qsize() method is not implemented on Macs and is not
  reliable:
  docs.python.org/3.7/library/multiprocessing.html#multiprocessing.Queue.qsize

  Args:
    a_queue: Queue to return the size of.
    timeout: Seconds before timing out.

  Returns:
    Size of queue.
  """
  size = 0
  deadline = time.time() + timeout
  while time.time() < deadline:
    try:
      a_queue.get_nowait()
      size += 1
    except queue.Empty:
      time.sleep(0.01)
  return size