-
Notifications
You must be signed in to change notification settings - Fork 5
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
update_smooth
blocks with sleep
#5
Comments
I realized this, and thats why i packed both I initially experimented with threading, since that allowed me to also add in some animations like a spinning bar, but it came with its own set of issues that required quite some additional code and increased complexity. For example, one issue was; calling But I'm open to a good solution that doesn't increase code complexity too much |
Threads will increase complexity for sure and fighting race conditions can be difficult but as the project is in such an early stage, it might be quite doable to architect for that. Alternatively, one could use a delta time approach to keep the printing operations in the main thread but have the timer in a background thread, using that as the progress value between the previous and the target percentage. Then you could also quite easily give the user the option to configure the animation duration. I think there's also ready-made solutions for tweening but you still need a time value but without a concrete update loop, a thread is needed to implement that update loop independent of main. (I think) Food for thought 😄 |
Yeah, I'm sure there's a solution, but it's just about finding and implementing the right one. Will look into the delta time approach. |
I have added a disclaimer in the Usage section of README.md stating the delay caused by |
@puttehi I have implemented something simple in threading, and pushed it to the def __init__(self, bar_load_fill: str="#", bar_blank_fill: str="-"):
...
self.bar_is_smoothing: bool = False
...
def _update_bar_smooth(self, percentage: int):
self.bar_is_smoothing = True
distance: int = percentage - self.completion
for i in range(distance):
del i
self.completion += 1
self._update_bar()
time.sleep(0.005)
self.bar_is_smoothing = False
def _block_when_smoothing(self):
"""
Blocks the main thread if bar is still running _update_bar_smooth()
Only used if another function call is performed. If the bar is
left to smooth properly without calling other functions, it will
be non-blocking
"""
while True:
if not self.bar_is_smoothing:
break
def update_smooth(self, percentage: int):
if not isinstance(percentage, int):
raise TypeError("percentage should be type int")
self._block_when_smoothing()
threading.Thread(target=self._update_bar_smooth, args=[percentage]).start()
What do you think about this implementation? |
I have tested and pushed the changes to the master branch, closing this issue as completed. |
lowbar/src/lowbar/__init__.py
Lines 139 to 143 in 1fe79e4
Using
update_smooth
will slow down your program. Every time you call it, you add 5 ms to your program execution per percent of progress. It should be non-blocking to have an use case.One idea would be to have the bar in its own thread and have the context manager hide the ugly thread details from the user, keeping the same or similar interface.
The text was updated successfully, but these errors were encountered: