1#!/usr/bin/env python
2"""
3More complex demonstration of what's possible with the progress bar.
4"""
5from __future__ import unicode_literals
6
7import random
8import threading
9import time
10
11from prompt_toolkit import HTML
12from prompt_toolkit.shortcuts import ProgressBar
13
14
15def main():
16    with ProgressBar(
17            title=HTML('<b>Example of many parallel tasks.</b>'),
18            bottom_toolbar=HTML('<b>[Control-L]</b> clear  <b>[Control-C]</b> abort')) as pb:
19
20        def run_task(label, total, sleep_time):
21            for i in pb(range(total), label=label):
22                time.sleep(sleep_time)
23
24        threads = []
25
26        for i in range(160):
27            label = 'Task %i' % i
28            total = random.randrange(50, 200)
29            sleep_time = random.randrange(5, 20) / 100.
30
31            threads.append(threading.Thread(target=run_task, args=(label, total, sleep_time)))
32
33        for t in threads:
34            t.daemon = True
35            t.start()
36
37        # Wait for the threads to finish. We use a timeout for the join() call,
38        # because on Windows, join cannot be interrupted by Control-C or any other
39        # signal.
40        for t in threads:
41            while t.is_alive():
42                t.join(timeout=.5)
43
44
45if __name__ == '__main__':
46    main()
47