Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chapter_2/listing_2_12.py #22

Open
maskalev opened this issue Mar 28, 2024 · 0 comments
Open

chapter_2/listing_2_12.py #22

maskalev opened this issue Mar 28, 2024 · 0 comments

Comments

@maskalev
Copy link

Hi Matt!

It is probably better to include the following line in the finally block to confirm whether the task was cancelled:

print(f'Was the task cancelled? {delay_task.cancelled()}')

This ensures that you can accurately determine whether the task has been cancelled, especially when users manipulate the time values in the code.

async def main():
    delay_task = asyncio.create_task(delay(2))
    try:
        result = await asyncio.wait_for(delay_task, timeout=1)
        print(result)
    except asyncio.exceptions.TimeoutError:
        print('Got a timeout!')
    finally:
        print(f'Was the task cancelled? {delay_task.cancelled()}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant