import threading


def wait_for_events(completion_queues, deadline):
  """
  Args:
    completion_queues: list of completion queues to wait for events on
    deadline: absolute deadline to wait until

  Returns:
    a sequence of events of length len(completion_queues).
  """

  results = [None] * len(completion_queues)
  lock = threading.Lock()
  threads = []
  def set_ith_result(i, completion_queue):
    result = completion_queue.next(deadline)
    with lock:
      results[i] = result
  for i, completion_queue in enumerate(completion_queues):
    thread = threading.Thread(target=set_ith_result,
                              args=[i, completion_queue])
    thread.start()
    threads.append(thread)
  for thread in threads:
    thread.join()
  return results