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

Fire and forget async ctx blocks anti-pattern #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gsalgado
Copy link
Contributor

@gsalgado gsalgado commented Jun 1, 2020

No description provided.

Copy link
Member

@pipermerriam pipermerriam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the example code here needs to be fleshed out a bit more. Ideally using something self-contained that doesn't lean on our trinity architecture which won't be familiar to everyone.

Example in-my-opinion should include the implementation of the async context manager, showing it returning a future/task type object, and then a version of the code you posted showing how you can end up with a dead background task if you don't include awaiting it as well.

@gsalgado
Copy link
Contributor Author

@pipermerriam care to have another look? I've added a self-contained example but also left the reference to the trinity code where I first noticed this pattern as I think it's good to have an example from real production code

@gsalgado gsalgado requested a review from pipermerriam June 14, 2020 23:34
```

In the above example, if the background task running `producer(stream_writer)` terminates without
closing the the writer, the `run()` function would continue running indefinitely and the exception
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closing the the writer

async with acmanager(writer) as streaming_task:
await asyncio.wait(
[consumer(reader), streaming_task],
return_when=asyncio.FIST_COMPLETED)
Copy link
Contributor

@cburgdorf cburgdorf Jun 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: FIRST_COMPLETED

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

Successfully merging this pull request may close these issues.

3 participants