-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add queue handler #49
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from ._progress_handler import TQDMProgressHandler | ||
from ._progress_subscriber import TQDMProgressSubscriber | ||
from ._publisher import TQDMPublisher | ||
from ._subscriber import TQDMProgressSubscriber | ||
|
||
__all__ = ["TQDMPublisher", "TQDMProgressSubscriber"] | ||
__all__ = ["TQDMPublisher", "TQDMProgressSubscriber", "TQDMProgressHandler"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import queue | ||
|
||
from ._progress_subscriber import TQDMProgressSubscriber | ||
|
||
|
||
class TQDMProgressHandler: | ||
def __init__(self): | ||
self.listeners = [] | ||
|
||
def listen(self): | ||
q = queue.Queue(maxsize=25) | ||
self.listeners.append(q) | ||
return q | ||
|
||
def create(self, iterable, additional_metadata: dict = dict(), **tqdm_kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this has to be a separate novel method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In particular, it's a little odd to have to use this roundabout approach to 'create' a new one. You might see this structure as a class constructor like @classmethod
def from_iterable(cls, iterable: Iterable, ...) -> Self:
.... but in general, inheriting from one of the parents and using OOP pattern would be preferred There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class handles more than one progress bar, so we can't just directly inherit from one of those classes. This could instead be called
Where:
It only doesn't work this way because it wouldn't be compatible with passing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can add this as another method—but AFAIK we need the original for our downstream code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why do we need a single class to handle more than one bar? The natural use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I'd written this down for myself but never noted it on the new PRs. We need a single class to handle more than one bar's updates and forward them to the client. Server-side events require that the connection is established on one endpoint ( The new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I was trying to communicate this here, things just got quite spread out: #46 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha. OK, I see now, thanks I will think more on this then... |
||
return TQDMProgressSubscriber( | ||
iterable, | ||
lambda progress_update: self._announce(dict(**progress_update, **additional_metadata)), | ||
**tqdm_kwargs, | ||
) | ||
|
||
def _announce(self, msg): | ||
for i in reversed(range(len(self.listeners))): | ||
try: | ||
self.listeners[i].put_nowait(msg) | ||
except queue.Full: | ||
del self.listeners[i] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,6 @@ | |
|
||
|
||
class TQDMProgressSubscriber(TQDMPublisher): | ||
def __init__(self, iterable, *, announcer: "?", request_id: str, message: dict, **tqdm_kwargs): | ||
def __init__(self, iterable, on_progress_update: callable, **tqdm_kwargs): | ||
super().__init__(iterable, **tqdm_kwargs) | ||
|
||
def on_progress_update(format_dict) -> None: | ||
""" | ||
Describe what this announcer is all about... | ||
""" | ||
announcer.announce(dict(request_id=request_id, **message)) | ||
|
||
self.subscribe(callback=on_progress_update) | ||
self.subscribe(lambda format_dict: on_progress_update(dict(progress_bar_id=self.id, format_dict=format_dict))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to undo this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only because there was never anything called an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docstrings everywhere would be helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure I'll add these when we dial it in—unless you'd prefer them for initial review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It helps with review to have a least basic descriptors of the intention of your design, yes