add multithreaded use to official documentation

This commit is contained in:
Casper da Costa-Luis 2017-09-21 01:54:51 +01:00
parent bff4720bfd
commit 790b568b67
No known key found for this signature in database
GPG Key ID: 986B408043AE090D
2 changed files with 53 additions and 1 deletions

View File

@ -511,7 +511,30 @@ available to keep nested bars on their respective lines.
For manual control over positioning (e.g. for multi-threaded use),
you may specify ``position=n`` where ``n=0`` for the outermost bar,
``n=1`` for the next, and so on.
``n=1`` for the next, and so on:
.. code:: python
from time import sleep
from tqdm import trange
from multiprocessing import Pool, freeze_support, Lock
L = list(range(9))
def progresser(n):
interval = 0.001 / (n + 2)
total = 5000
text = "#{}, est. {:<04.2}s".format(n, interval * total)
for i in trange(total, desc=text, position=n):
sleep(interval)
if __name__ == '__main__':
freeze_support() # for Windows support
p = Pool(len(L),
# again, for Windows support
initializer=tqdm.set_lock, initargs=(Lock(),))
p.map(progresser, L)
print("\n" * (len(L) - 2))
Hooks and callbacks
~~~~~~~~~~~~~~~~~~~

29
examples/parallel_bars.py Normal file
View File

@ -0,0 +1,29 @@
from __future__ import print_function
from time import sleep
from tqdm import tqdm
from multiprocessing import Pool, freeze_support, Lock
L = list(range(9))
def progresser(n):
interval = 0.001 / (n + 2)
total = 5000
text = "#{}, est. {:<04.2}s".format(n, interval * total)
for _ in tqdm(range(total), desc=text, position=n):
sleep(interval)
if __name__ == '__main__':
freeze_support() # for Windows support
p = Pool(len(L),
initializer=tqdm.set_lock,
initargs=(Lock(),))
p.map(progresser, L)
print("\n" * (len(L) - 2))
# alternatively, on UNIX, just use the default internal lock
p = Pool(len(L))
p.map(progresser, L)
print("\n" * (len(L) - 2))