code: async def run_with_interrupt(task, *events, loop=None):
      
      loop = loop or asyncio.get_event_loop()
      task = asyncio.ensure_future(task, loop=loop)
      event_tasks = [loop.create_task(event.wait()) for event in events]
      done, pending = await asyncio.wait([task] + event_tasks,
                                         loop=loop,
                                         return_when=asyncio.FIRST_COMPLETED)
      for f in pending:
          f.cancel()  # cancel unfinished tasks
      for f in done:
          f.exception()  # prevent "exception was not retrieved" errors
      if task in done:
          return task.result()  # may raise exception
      else:
          return None
docs: Awaits a task while allowing it to be interrupted by one or more
      `asyncio.Event`s.
  
      If the task finishes without the events becoming set, the results of the
      task will be returned.  If the event become set, the task will be cancelled
      ``None`` will be returned.
  
      :param task: Task to run
      :param events: One or more `asyncio.Event`s which, if set, will interrupt
          `task` and cause it to be cancelled.
      :param loop: Optional event loop to use other than the default.