[Async-sig] killing tasks that won't cancel

Yury Selivanov yselivanov at gmail.com
Tue Feb 19 15:27:15 EST 2019


FYI Chris has started a parallel discussion on the same topic here: https://bugs.python.org/issue32363.  Chris, let's keep this discussion in one place (now it's this list, I guess). It's hard to handle the same discussion in two different places.  Please don't split discussions like this.

I'll summarize what I said in the above referenced bpo here:

1. Task.set_result() and Task.set_exception() have never ever worked properly.  They never actually communicated the set result/exception to the underlying coroutine.  The fact that they were exposed at all was a simple oversight.

I can guess how Task.set_exception() can be implemented in theory: the exception would be thrown into the wrapped coroutine.  But I don't quite understand how Task.set_result() can be implemented at all.

2. Task and coroutine maintain a simple relationship: Task wraps its coroutine.  The result of the coroutine is the result of the Task (not the other way around).  The Task can request its coroutine to cancel.  The coroutine may ignore that request by ignoring the asyncio.CancelledError exception.

If the latter happens, the Task cannot terminate the coroutine, this is by design.  Moreover, you can always write

   while True:
      try:
         await asyncio.sleep(1)
      except:
         pass

and then nothing can terminate your coroutine.  IOW, if your code chooses to ignore CancelledError the Task can do nothing about it.

3. For proper bi-directional communication between coroutines asyncio has queues.  One can easily implement a message queue to implement injection of an exception or result into a coroutine.

[Chris]
>  I was asking if there is a way to end such a task. Is there? 

No, there's no way to end tasks like that.  The key question here is: is this a theoretical problem you're concerned with?  Or is this something that happens in real-world framework/library/code that you're dealing with?

Yury


> On Feb 19, 2019, at 2:53 PM, Chris Jerdonek <chris.jerdonek at gmail.com> wrote:
> 
> I have an asyncio question.
> 
> In Python 3.7, is there a way to reliably end a task after having
> already tried calling cancel() on it and waiting for it to end?
> 
> In Python 3.6, I did this with task.set_exception(), but in 3.7 that
> method was removed.
> 
> --Chris
> _______________________________________________
> Async-sig mailing list
> Async-sig at python.org
> https://mail.python.org/mailman/listinfo/async-sig
> Code of Conduct: https://www.python.org/psf/codeofconduct/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20190219/17a74591/attachment-0001.html>


More information about the Async-sig mailing list