Skip to content

Instantly share code, notes, and snippets.

@pidement
Last active January 17, 2023 23:20
Show Gist options
  • Save pidement/6f003e770feb40b8b89cc5c2a99bce5d to your computer and use it in GitHub Desktop.
Save pidement/6f003e770feb40b8b89cc5c2a99bce5d to your computer and use it in GitHub Desktop.
Simple spinner for long proccess
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