import queue


def get_messages_from_queue(mp_queue, timeout=0.01):
  """Safely get all messages from a multiprocessing queue.

  Args:
      mp_queue (queue): a multiprocess Queue instance
      timeout (float): seconds to block other processes out from the queue

  Returns:
      list: List of messages or an empty list if there weren't any
  """
  msgs = []
  # According to the python docs https://docs.python.org/2/library/multiprocessing.html
  # after putting an object on an empty queue there may be an
  #  infinitesimal delay before the queue's empty() method returns False
  #
  # We've actually run into this (SPT-1354) so we'll first kick the
  # tires with a get() and then see if there are more using empty()
  try:
    msgs.append(mp_queue.get(True, timeout))
  except queue.Empty:
    pass
  else:
    while not mp_queue.empty():
      msgs.append(mp_queue.get_nowait())
  return msgs