title: Wait for timeout or event being set for asyncio.Event
body: <p>I have a class with a method that looks like this:</p>
  
  <pre><code># self.stop_event -&gt; threading.Event
  def run(self):
      while not self.stop_event.wait(3):  # i.e. repeat every 3 sec
          pass  # do stuff
  </code></pre>
  
  <p>The idea is that several of these are run in their own thread and at some point one thread does <code>stop_event.set()</code>, which naturally stops all others. I want to switch to asyncio for this, because the tasks in <code>run</code> are mostly sleeping and doing IO. Thus, I got to:</p>
  
  <pre><code># self.stop_event -&gt; asyncio.Event
  async def run(self):
      while not self.stop_event.is_set():
          await asyncio.sleep(3)
          pass  # do stuff
  </code></pre>
  
  <p>The problem is that the <code>asyncio.Event</code> cannot be waited on, so when it is set, there are at most 3 seconds to wait before the method completes. This is a problem, because the sleep time may be minutes. Currently, I am working around this by wrapping the <code>run</code> in an <code>asyncio.Task</code> and then cancelling it like <code>event_loop.call_soon(the_task.cancel)</code>.</p>
  
  <p>I want to ask if there is a better way to achieve the above? Is there a way I can wait on an <code>asyncio.Event</code> with a timeout somehow, similar to the <code>threading.Event</code>?</p>