Skip to content

Commit

Permalink
bug - race condition in the cli spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Nov 22, 2011
1 parent 97e1690 commit 0d131f9
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions cli/src/katello/client/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,22 @@ def format_date(date, from_format="%Y-%m-%dT%H:%M:%SZ", to_format="%Y/%m/%d %H:%


class Spinner(threading.Thread):
"""
Spinner shows nice cli "spinner" while function is executing.
Each spinner instance can be started only once. Typical usage:
s = Spinner()
s.start()
...
s.stop()
s.join()
"""

def __init__(self, msg=""):
self._msg = msg
threading.Thread.__init__(self)
self._stopevent = threading.Event()

def _putMessage(self):
sys.stdout.write(self._msg)
Expand Down Expand Up @@ -420,21 +432,18 @@ def _eraseSpinner(self):
self._resetCaret()

def run(self):
self._stop = False

self._putMessage()
while True:
for char in '/-\|':
self._putChar(char)
if self._stop:
if self._stopevent.wait(0.1):
self._eraseSpinner()
self._eraseMessage()
return
time.sleep( 0.1 )
self._resetCaret()

def stop(self):
self._stop = True
self._stopevent.set()

class ProgressBar():

Expand Down

0 comments on commit 0d131f9

Please sign in to comment.