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

Implement exception handling for async generators. #13

Merged
merged 5 commits into from
Nov 8, 2023

Conversation

aebrahim
Copy link
Member

Fixes #12.

once/_iterator_wrappers.py Outdated Show resolved Hide resolved
once/_iterator_wrappers.py Outdated Show resolved Hide resolved
once/_iterator_wrappers.py Show resolved Hide resolved
once/_iterator_wrappers.py Outdated Show resolved Hide resolved
once/_iterator_wrappers.py Show resolved Hide resolved
once_test.py Outdated Show resolved Hide resolved
once_test.py Show resolved Hide resolved
once_test.py Outdated Show resolved Hide resolved
@aebrahim aebrahim requested review from mattalbr and removed request for mattalbr October 19, 2023 15:53
@aebrahim aebrahim marked this pull request as draft October 19, 2023 15:53
@aebrahim aebrahim force-pushed the async_iterator branch 10 times, most recently from 998a946 to 8ee842d Compare October 24, 2023 18:48
Fixes DelfinaCare#12.

The default behavior is to cache Exceptions. However, there is an option
to retry exceptions, which will also respect the concurrency guarentees
from once.
@aebrahim aebrahim marked this pull request as ready for review October 27, 2023 23:50
Copy link
Collaborator

@mattalbr mattalbr left a comment

Choose a reason for hiding this comment

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

Still reviewing but here's what I've got so far

once/__init__.py Outdated Show resolved Hide resolved
Comment on lines +89 to +97
# In the sync case, we also need a sleep(0) for a different reason. While a thread is in the
# WAITING state, it would otherwise hog the Global Interpreter Lock in its while loop until
# the interprer decides to switch threads. With N threads, N - 1 of them would be in the
# WAITING state, and a round-robin interpreter would give each of their while loops the
# thread switching time in execution before reaching the single thread in the GENERATING state.
# Theoretically, this could result in the GIL only working on the thread in the GENERATING
# state 1/Nth of the time, without a time.sleep(0) to manually trigger a GIL switch. In
# practice, removing the time.sleep(0) call would result in large drops in performance when
# running the multithreaded unit tests.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This description isn't wrong, and if you want to keep it as-is, that's totally fine, but it's basically because we've implemented our own spin-lock and this is a common pattern in a spin-lock for the reasons you mentioned.

Copy link
Member Author

Choose a reason for hiding this comment

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

I literally couldn't find a single external description at this level of detail about the issues with spinlocks in python, especially when considering the specifics of asyncio or the GIL.

I'd prefer to keep it in here until such time as we can maybe write this up in a blog post and link to it - I really don't want to forget this reasoning.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good, we can keep it then!

once/_iterator_wrappers.py Show resolved Hide resolved
once/_iterator_wrappers.py Show resolved Hide resolved
Copy link
Collaborator

@mattalbr mattalbr left a comment

Choose a reason for hiding this comment

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

Awesome!


def __init__(self, *args, **kwargs) -> None:
self.active_generation_completed = asyncio.Event()
super().__init__(*args, **kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any reason for interweaving super().init between the two local variables? If there is, a comment might be nice, otherwise I would just call super first.

Copy link
Member Author

Choose a reason for hiding this comment

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

added a comment.

@aebrahim aebrahim merged commit 4e98518 into DelfinaCare:main Nov 8, 2023
51 checks passed
@aebrahim aebrahim deleted the async_iterator branch November 8, 2023 18:47
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.

Exception handling should be well-defined
2 participants