Created
August 3, 2017 04:09
-
-
Save junnplus/e6ce33dfa759c749b3da63ba0028347a to your computer and use it in GitHub Desktop.
to print a spin bar while a computation is running
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 sys | |
import time | |
import multiprocessing | |
DELAY = 0.1 | |
DISPLAY = ['|', '/', '-', '\\'] | |
def spinner_func(before='', after=''): | |
write, flush = sys.stdout.write, sys.stdout.flush | |
pos = -1 | |
while True: | |
pos = (pos + 1) % len(DISPLAY) | |
msg = before + DISPLAY[pos] + after | |
write(msg) | |
flush() | |
write('\x08' * len(msg)) | |
time.sleep(DELAY) | |
def long_computation(): | |
# emulate a long computation | |
time.sleep(3) | |
if __name__ == '__main__': | |
spinner = multiprocessing.Process( | |
None, spinner_func, args=('Please wait ... ', '')) | |
spinner.start() | |
try: | |
long_computation() | |
print 'Computation done' | |
finally: | |
spinner.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://mail.python.org/pipermail/python-list/2009-February/525280.html