Skip to content

Commit c65e4b4

Browse files
committed
added multiple threaded progress bars example
1 parent 11cfea0 commit c65e4b4

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

README.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,68 @@ Bar with wide Chinese (or other multibyte) characters
237237
)
238238
for i in bar(range(10)):
239239
time.sleep(0.1)
240+
241+
Showing multiple (threaded) independent progress bars in parallel
242+
==============================================================================
243+
244+
While this method works fine and will continue to work fine, a smarter and
245+
fully automatic version of this is currently being made:
246+
https://github.com/WoLpH/python-progressbar/issues/176
247+
248+
.. code:: python
249+
250+
import random
251+
import sys
252+
import threading
253+
import time
254+
255+
import progressbar
256+
257+
output_lock = threading.Lock()
258+
259+
260+
class LineOffsetStreamWrapper:
261+
UP = '\033[F'
262+
DOWN = '\033[B'
263+
264+
def __init__(self, lines=0, stream=sys.stderr):
265+
self.stream = stream
266+
self.lines = lines
267+
268+
def write(self, data):
269+
with output_lock:
270+
self.stream.write(self.UP * self.lines)
271+
self.stream.write(data)
272+
self.stream.write(self.DOWN * self.lines)
273+
self.stream.flush()
274+
275+
def __getattr__(self, name):
276+
return getattr(self.stream, name)
277+
278+
279+
bars = []
280+
for i in range(5):
281+
bars.append(
282+
progressbar.ProgressBar(
283+
fd=LineOffsetStreamWrapper(i),
284+
max_value=1000,
285+
)
286+
)
287+
288+
if i:
289+
print('Reserve a line for the progressbar')
290+
291+
292+
class Worker(threading.Thread):
293+
def __init__(self, bar):
294+
super().__init__()
295+
self.bar = bar
296+
297+
def run(self):
298+
for i in range(1000):
299+
time.sleep(random.random() / 100)
300+
self.bar.update(i)
301+
302+
303+
for bar in bars:
304+
Worker(bar).start()

0 commit comments

Comments
 (0)