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 Davy Durham
Recipients Davy Durham, asvetlov, yselivanov
Date 2020-05-28.05:36:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I was searching for a way to "yield" from task/coroutinue back to the event loop (not yielding a value in terms of a generator) and not finding anything documented, I found this bug report and PR:

It states that asyncio.sleep(0) should cause the coroutine to send control back to the event loop without wastefully doing other work.  That makes sense and a perfectly good way to do that.

And, the code appears to handle a value of <= 0 specifically:

However, using sleep(0) to yield back does not cause it to raise a CancelledError if the task has been cancelled as cancel()'s documentation indicates it should.  But sleeping for anything >0 does (e.g. 0.001)

The below code snippet will demonstrate the problem:



import asyncio
import time

async def cancel_me():
    print('cancel_me(): before sleep')

        while True:
            print("doing some really intensive cpu stuff")

            # now I want to yield control back to the event loop in order to determine if we've been cancelled 
            await asyncio.sleep(0) # I'm expecting this to throw CancelledError, but it never does.. it DOES throw if the delay > 0 (e.g. 0.001)

    except asyncio.CancelledError:
        print('cancel_me(): cancelled!')

async def main():
    task = asyncio.create_task(cancel_me())

    await asyncio.sleep(1)

        await task
    except asyncio.CancelledError:
        print("main(): cancel_me is cancelled now")
Date User Action Args
2020-05-28 05:36:59Davy Durhamsetrecipients: + Davy Durham, asvetlov, yselivanov
2020-05-28 05:36:59Davy Durhamsetmessageid: <>
2020-05-28 05:36:59Davy Durhamlinkissue40800 messages
2020-05-28 05:36:59Davy Durhamcreate