Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ For actions with an unknown number of steps you can use a spinner. ::
# Do some work
spinner.next()

Spinners can run in the background, for example, during a long, blocking
call. They will automatically be advanced with a customizable frequency
controlled by the ``frequency`` keyword argument (0.1 seconds by default)
This behvaior is available using the ``with`` statement. ::

from progress.spinner import Spinner

with Spinner('Loading ', frequency=0.1):
# Do some work, or make a blocking call

There are 4 predefined spinners:

- Spinner
Expand Down
19 changes: 19 additions & 0 deletions progress/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from math import ceil
from sys import stderr
from time import time
from threading import Thread, Event


__version__ = '1.2'
Expand Down Expand Up @@ -78,6 +79,24 @@ def iter(self, it):
self.next()
self.finish()

def _run(self):
frequency = getattr(self, 'frequency', 0.1)
while not self._event.is_set():
self.next()
self._event.wait(frequency)

def __enter__(self):
self.start()
self._event = Event()
self._thread = Thread(target=self._run)
self._thread.start()

def __exit__(self, exc_type, exc_value, exc_tb):
self._event.set()
self._thread.join()
self.finish()



class Progress(Infinite):
def __init__(self, *args, **kwargs):
Expand Down
4 changes: 4 additions & 0 deletions test_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@
bar.goto(randint(0, 100))
sleep(0.1)
bar.finish()

with Spinner('ThreadedSpinner '):
sleep(3)
print()