title: Python Asyncio Task Cancellation
body: <p>I was reading asyncio documentation for task cancel and I came across <a href="https://docs.python.org/3/library/asyncio-task.html?highlight=cancel#task-object" rel="noreferrer">this</a> -</p>
  
  <blockquote>
    <p>To cancel a running Task use the cancel() method. Calling it will cause the Task to throw a CancelledError exception into the wrapped coroutine. If a coroutine is awaiting on a Future object during cancellation, the Future object will be cancelled.</p>
    
    <p>cancelled() can be used to check if the Task was cancelled. The method returns True if the wrapped coroutine <strong>did not suppress the CancelledError exception</strong> and was actually cancelled.</p>
  </blockquote>
  
  <p>I have a few questions here - </p>
  
  <ul>
  <li><p>Is wrapped coroutine the coroutine in which cancel is called? Let's take an example here -</p>
  
  <pre><code>async def wrapped_coroutine():
      for task in asyncio.Task.all_tasks():
          task.cancel()
  </code></pre>
  
  <p>So <code>wrapped_coroutine()</code> is the wrapped coroutine where task will throw an exception?</p></li>
  <li><p>When will this exception be thrown? And where?</p></li>
  <li><p>What does suppress the exception mean here? Does it mean this -</p>
  
  <pre><code>async def wrapped_coroutine():
      for task in asyncio.Task.all_tasks():
          task.cancel()
          try:
              await task
          except asyncio.CancelledError:
              print("Task cancelled")
  </code></pre>
  
  <p>If not, please provide an example on how to suppress this exception.</p></li>
  </ul>
  
  <p>And an unrelated(it's related to cancelling tasks), how do I retrieve exceptions out of these tasks when I'm cancelling these so I don't see this -</p>
  
  <pre><code>Task exception was never retrieved future:
  </code></pre>
  
  <p>Is it before <code>task.cancel()</code> or in <code>try</code> before <code>await task</code> (in the above example) ?</p>