-
-
Notifications
You must be signed in to change notification settings - Fork 102
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
Make ProgressBar a Generator so it cleans up when interrupted #214
Conversation
This pull request fixes 10 alerts when merging 94732f4 into 62c5b45 - view on LGTM.com fixed alerts:
|
Very nice work! I was actually working on something similar (and still working on writing a response to your elaborate comment) but this is excellent! I was using a slightly slimpler method and I'm not sure about the upsides or downsides. I simply changed the |
Having It took me a while to work out if it was possible to combine them - it came down to needing to manually put in the I can't think of any other pro/con. I think they're behaviorally the same other than that, although I'd be interested to hear if that's wrong - Python is always more weird than i think :p |
I don't mean a new object, I meant replacing Here's the current code: https://github.com/WoLpH/python-progressbar/blob/62c5b4543efd00b155898c2fe18fdbfa5a6b9e12/progressbar/bar.py#L522-L536 I was thinking of replacing it with something like this: def __iter__(self):
try:
if self.start_time is None:
self.start()
for value in self._iterable:
self.update(self.value + 1)
yield value
except StopIteration:
self.finish()
except GeneratorExit:
self.finish(dirty=True) |
When
When the class in question is a Generator itself, and |
You're right, I hadn't tested it well and I was wondering why I chose for this method when writing it some years ago ;) |
I've finally had some time to test this and it does look like it fixes a few issues but definitely not all of them. For example, try this test: import sys
import time
import progressbar
try:
bar = progressbar.ProgressBar(fd=sys.stdout, redirect_stdout=True)
for i in bar(range(20)):
print('Some text', i)
time.sleep(0.2)
if i == 5:
raise KeyboardInterrupt
except:
pass
print('a')
time.sleep(0.3)
print('b')
time.sleep(0.3)
print('c')
time.sleep(0.3)
print('d')
time.sleep(0.3)
print('e')
time.sleep(0.3)
print('f') That returns:
Still though, as soon as I've got the tests fully working again I'll create a new release :) |
Yeah, I can see the behavior in your test being kinda weird; Do you have an idea what behavior you'd like in the above case? |
Make ProgressBar a Generator so it cleans up when interrupted
Apologies for the really slow response, it's been a very busy month and I do very much appreciate all the help :) I'm still working on merging this. Specifically fixing the tests, for some reason the output redirection breaks as soon as the progressbar becomes a generator. Not entirely sure what the reason is but I'll get it working one way or another. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This is an attempt to make the ProgressBar class from an Iterator into a Generator.
This means that when it's garbage collected,
close()
will be called, and so the ProgressBar can guarantee that stdout is unwrappedfixes #212