title: How to wait for object to change state
body: <p>Inside my <code>async</code> handler I want to wait until task's state is changed. For now, I just check the state in an endless loop and wait. Here is an example, the <code>wait_until_done</code> function:</p>
  
  <pre><code>import asyncio
  
  
  class LongTask:
      state = 'PENDING'
  
  my_task = LongTask()
  
  
  def done():
      my_task.state = 'DONE'
  
  async def wait_until_done():
      while True:
          if my_task.state == 'PENDING':
              await asyncio.sleep(2)
          else:
              break
      print("Finally, the task is done")
  
  
  def main(loop, *args, **kwargs):
      asyncio.ensure_future(wait_until_done())
      loop.call_later(delay=5, callback=done)
  
  loop = asyncio.get_event_loop()
  main(loop)
  loop.run_forever()
  </code></pre>
  
  <p>Is there a better way for doing that?</p>