Last active
January 17, 2023 23:20
-
-
Save pidement/6f003e770feb40b8b89cc5c2a99bce5d to your computer and use it in GitHub Desktop.
Simple spinner for long proccess
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from multiprocessing import Process, Value | |
class spinner(): | |
def __init__(self, desc="", animation="|/-\\", end="✔", sleep=0.05): | |
self.text = desc | |
self.end = end | |
self.animation = animation | |
self.sleep = sleep | |
self.idx = 0 | |
self.p = Process(target=self._thread_func) | |
self.running = Value('i', True) | |
def __enter__(self): | |
self.p.start() | |
def _thread_func(self): | |
while self.running.value: | |
print('\r' + self.text + self.animation[self.idx], end='', flush=True) | |
self.idx = (self.idx + 1) % len(self.animation) | |
time.sleep(self.sleep) | |
def __exit__(self, arg2, arg3, arg4): | |
self.running.value = False | |
self.p.join() | |
print('\r' + self.text + self.end) | |
with spinner(desc='Loading model '): | |
time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment