Skip to content

Commit

Permalink
bump version, Merge branch 'parallel-print-fix2'
Browse files Browse the repository at this point in the history
fixes #285 -> #291 -> #329
fixes #422
fixes #439

fixes #323
fixes #324
fixes #334
fixes #407
fixes #418

related to:
- #97
- #143
- #331
- #361
- #384
- #385
- #417
  • Loading branch information
casperdcl committed Sep 26, 2017
2 parents 1707285 + 4278974 commit e32428c
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 154 deletions.
7 changes: 6 additions & 1 deletion Makefile
@@ -1,6 +1,7 @@
# IMPORTANT: for compatibility with `python setup.py make [alias]`, ensure:
# 1. Every alias is preceded by @[+]make (eg: @make alias)
# 2. A maximum of one @make alias or command per line
# 3. Only use tabs, not spaces to indent (compatibility with linux make)
#
# Sample makefile compatible with `python setup.py make`:
#```
Expand Down Expand Up @@ -64,17 +65,21 @@ testcoverage:
@make coverclean
nosetests tqdm --with-coverage --cover-package=tqdm --cover-erase --cover-min-percentage=80 --ignore-files="tests_perf\.py" -d -v

testperf: # do not use coverage (which is extremely slow)
testperf:
# do not use coverage (which is extremely slow)
nosetests tqdm/tests/tests_perf.py -d -v

testtimer:
nosetests tqdm --with-timer -d -v

# another performance test, to check evolution across commits
testasv:
# Test only the last 3 commits (quick test)
asv run -j 8 HEAD~3..HEAD
@make viewasv

testasvfull:
# Test all the commits since the beginning (full test)
asv run -j 8 v1.0.0..master
@make testasv

Expand Down
56 changes: 40 additions & 16 deletions README.rst
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -657,10 +680,8 @@ A reusable canonical example is given below:
.. code:: python
from time import sleep
import contextlib
import sys
from tqdm import tqdm
class DummyTqdmFile(object):
Expand All @@ -674,32 +695,35 @@ A reusable canonical example is given below:
if len(x.rstrip()) > 0:
tqdm.write(x, file=self.file)
def flush(self):
return getattr(self.file, "flush", lambda: None)()
@contextlib.contextmanager
def stdout_redirect_to_tqdm():
save_stdout = sys.stdout
def std_out_err_redirect_tqdm():
orig_out_err = sys.stdout, sys.stderr
try:
sys.stdout = DummyTqdmFile(sys.stdout)
yield save_stdout
sys.stdout, sys.stderr = map(DummyTqdmFile, orig_out_err)
yield orig_out_err[0]
# Relay exceptions
except Exception as exc:
raise exc
# Always restore sys.stdout if necessary
# Always restore sys.stdout/err if necessary
finally:
sys.stdout = save_stdout
sys.stdout, sys.stderr = orig_out_err
def blabla():
print("Foo blabla")
def some_fun(i):
print("Fee, fi, fo,".split()[i])
# Redirect stdout to tqdm.write() (don't forget the `as save_stdout`)
with stdout_redirect_to_tqdm() as save_stdout:
# tqdm call need to specify sys.stdout, not sys.stderr (default)
with std_out_err_redirect_tqdm() as orig_stdout:
# tqdm needs the original stdout
# and dynamic_ncols=True to autodetect console width
for _ in tqdm(range(3), file=save_stdout, dynamic_ncols=True):
blabla()
for i in tqdm(range(3), file=orig_stdout, dynamic_ncols=True):
sleep(.5)
some_fun(i)
# After the `with`, printing is restored
print('Done!')
print("Done!")
Monitoring thread, intervals and miniters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
29 changes: 29 additions & 0 deletions examples/parallel_bars.py
@@ -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))
32 changes: 18 additions & 14 deletions examples/redirect_print.py
Expand Up @@ -29,32 +29,36 @@ def write(self, x):
if len(x.rstrip()) > 0:
tqdm.write(x, file=self.file)

def flush(self):
return getattr(self.file, "flush", lambda: None)()


@contextlib.contextmanager
def stdout_redirect_to_tqdm():
save_stdout = sys.stdout
def std_out_err_redirect_tqdm():
orig_out_err = sys.stdout, sys.stderr
try:
sys.stdout = DummyTqdmFile(sys.stdout)
yield save_stdout
# sys.stdout = sys.stderr = DummyTqdmFile(orig_out_err[0])
sys.stdout, sys.stderr = map(DummyTqdmFile, orig_out_err)
yield orig_out_err[0]
# Relay exceptions
except Exception as exc:
raise exc
# Always restore sys.stdout if necessary
# Always restore sys.stdout/err if necessary
finally:
sys.stdout = save_stdout
sys.stdout, sys.stderr = orig_out_err


def blabla():
print("Foo blabla")
def some_fun(i):
print("Fee, fi, fo,".split()[i])


# Redirect stdout to tqdm.write() (don't forget the `as save_stdout`)
with stdout_redirect_to_tqdm() as save_stdout:
# tqdm call need to specify sys.stdout, not sys.stderr (default)
# Redirect stdout to tqdm.write()
with std_out_err_redirect_tqdm() as orig_stdout:
# tqdm needs the original stdout
# and dynamic_ncols=True to autodetect console width
for _ in tqdm(range(3), file=save_stdout, dynamic_ncols=True):
blabla()
for i in tqdm(range(3), file=orig_stdout, dynamic_ncols=True):
sleep(.5)
some_fun(i)

# After the `with`, printing is restored
print('Done!')
print("Done!")
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -76,7 +76,7 @@ commands =
[testenv:flake8]
deps = flake8
commands =
flake8 --max-line-length=80 --count --statistics --exit-zero -j 8 --exclude .asv .
flake8 --max-line-length=80 --count --statistics --exit-zero -j 8 --exclude .tox,.asv .

[testenv:setup.py]
deps =
Expand Down

0 comments on commit e32428c

Please sign in to comment.