This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author twisteroid ambassador
Recipients asvetlov, epiphyte, jnwatson, twisteroid ambassador, yselivanov
Date 2019-02-18.13:06:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1550495188.15.0.35802053828.issue35945@roundup.psfhosted.org>
In-reply-to
Content
There is a way to distinguish whether a task is being cancelled from the "inside" or "outside", like this:

    async def task1func():
        task2 = asyncio.create_task(task2func())
        try:
            await asyncio.wait((task2,))
        except asyncio.CancelledError:
            print('task1 is being cancelled from outside')
            # Optionally cancel task2 here, since asyncio.wait() shields task2 from
            # being cancelled from the outside
            raise
        assert task2.done()
        if task2.cancelled():
            print('task2 was cancelled')
            # Note that task1 is not cancelled here, so if you want to cancel
            # task1 as well, do this:
            # raise asyncio.CancelledError
        
        task2_result = task2.result()
        # Use your result here
History
Date User Action Args
2019-02-18 13:06:28twisteroid ambassadorsetrecipients: + twisteroid ambassador, asvetlov, yselivanov, jnwatson, epiphyte
2019-02-18 13:06:28twisteroid ambassadorsetmessageid: <1550495188.15.0.35802053828.issue35945@roundup.psfhosted.org>
2019-02-18 13:06:28twisteroid ambassadorlinkissue35945 messages
2019-02-18 13:06:28twisteroid ambassadorcreate