atpbar can display multiple progress bars simultaneously growing to
show the progresses of iterations of loops in
threading or
multiprocessing
tasks. atpbar can display progress bars on terminal and Jupyter
Notebook. atpbar can be used with
Mantichora.
atpbar started its development in 2015 as part of
alphatwirl. atpbar
prevented physicists from terminating their running analysis codes,
which would take many hours to complete, by showing progress bars
indicating their codes were actually running. The progress bars have
saved the physicists countless hours total. atpbar had been the
sub-package
progressbar
of alphatwirl until it became an independent package, with the name
atpbar, in February 2019.
I will show here how to use atpbar by simple examples.
Import libraries
To create simple loops in the examples, we use two python standard
libraries, time and
random. Import the
two packages as well as atpbar.
importtime, randomfromatpbarimportatpbar
Note: import the object atpbar from the package atpbar rather
than importing the package atpbar itself.
One loop
The object atpbar is an iterable that can wrap another iterable and
shows the progress bars for the iterations. (The idea of making the
interface iterable was inspired by
tqdm.)
The task in the above code is to sleep for 0.0001 seconds in each
iteration of the loop. The number of the iterations of the loop is
randomly selected from between 1000 and 10000.
In order for atpbar to show a progress bar, the wrapped iterable
needs to have a length. If the length cannot be obtained by len(),
atpbar won't show a progress bar.
Nested loops
atpbar can show progress bars for nested loops as in the following
example.
The outer loop iterates 4 times. The inner loop is similar to the loop
in the previous example---sleeps for 0.0001 seconds. You can
optionally give the keyword argument name to specify the label on
the progress bar.
In the snapshot of the progress bars above, the outer loop is in its
3rd iteration. The inner loop has completed twice and is running the
third. The progress bars for the completed tasks move up. The progress
bars for the active tasks are growing at the bottom.
Threading
atpbar can show multiple progress bars for loops concurrently
iterating in different threads.
The function run_with_threading() in the following code shows an
example.
The task to sleep for 0.0001 seconds is defined as the function
task. The task is concurrently run 5 times with threading.
atpbar can be used in any threads. Five progress bars growing
simultaneously will be shown. The function flush() returns when the
progress bars have finished updating.
It starts four workers in subprocesses with multiprocessing and have
them run ten tasks.
In order to use atpbar in a subprocess, the reporter, which can be
found in the main process by the function find_reporter(), needs to
be brought to the subprocess and registered there by the function
register_reporter().
Simultaneously growing progress bars will be shown.
This example uses multiprocessing. To use threading, set the option mode to threading (the default is
multiprocessing), i.e., mantichora(mode='threading')
This feature works as well with nested loops, threading, and
multiprocessing. For example, in the following code, the loops in five
threads break at 1235th iteration.
The atpbar sensibly works regardless of the order in which multiple
instances of atpbar in multiple threads and multiple processes start
and end. The progress bars in the example above indicates that the
loops in four threads have already ended before the loop in the main
threads ended; the loop in the last thread ended afterwards.
On Jupyter Notebook
On Jupyter Notebook, atpbar shows progress bars based on
widgets.
You can try interactively online:
Non TTY device
If neither on Jupyter Notebook or on a TTY device, atpbar is not
able to show progress bars. atpbar occasionally prints the status.
请发表评论