-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'coroutines' into devel
- Loading branch information
Showing
8 changed files
with
362 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
Asynchronous examples using `asyncio`, `async` and `await` on `python>=3.7`. | ||
""" | ||
import asyncio | ||
from tqdm.asyncio import tqdm, trange | ||
|
||
|
||
def count(start=0, step=1): | ||
i = start | ||
while True: | ||
new_start = yield i | ||
if new_start is None: | ||
i += step | ||
else: | ||
i = new_start | ||
|
||
|
||
async def main(): | ||
N = int(1e6) | ||
async for row in tqdm(trange(N, desc="inner"), desc="outer"): | ||
if row >= N: | ||
break | ||
with tqdm(count(), desc="coroutine", total=N + 2) as pbar: | ||
async for row in pbar: | ||
if row == N: | ||
pbar.send(-10) | ||
elif row < 0: | ||
assert row == -9 | ||
break | ||
# should be under 10 seconds | ||
for i in tqdm.as_completed(list(map(asyncio.sleep, [1] * 10)), | ||
desc="as_completed"): | ||
await i | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Inserting `tqdm` as a "pipe" in a chain of coroutines. | ||
Not to be confused with `asyncio.coroutine`. | ||
""" | ||
from functools import wraps | ||
from tqdm.auto import tqdm | ||
|
||
|
||
def autonext(func): | ||
@wraps(func) | ||
def inner(*args, **kwargs): | ||
res = func(*args, **kwargs) | ||
next(res) | ||
return res | ||
return inner | ||
|
||
|
||
@autonext | ||
def tqdm_pipe(target, **tqdm_kwargs): | ||
""" | ||
Coroutine chain pipe `send()`ing to `target`. | ||
This: | ||
>>> r = receiver() | ||
>>> p = producer(r) | ||
>>> next(r) | ||
>>> next(p) | ||
Becomes: | ||
>>> r = receiver() | ||
>>> t = tqdm.pipe(r) | ||
>>> p = producer(t) | ||
>>> next(r) | ||
>>> next(p) | ||
""" | ||
with tqdm(**tqdm_kwargs) as pbar: | ||
while True: | ||
obj = (yield) | ||
target.send(obj) | ||
pbar.update() | ||
|
||
|
||
def source(target): | ||
for i in ["foo", "bar", "baz", "pythonista", "python", "py"]: | ||
target.send(i) | ||
target.close() | ||
|
||
|
||
@autonext | ||
def grep(pattern, target): | ||
while True: | ||
line = (yield) | ||
if pattern in line: | ||
target.send(line) | ||
|
||
|
||
@autonext | ||
def sink(): | ||
while True: | ||
line = (yield) | ||
tqdm.write(line) | ||
|
||
|
||
if __name__ == "__main__": | ||
source( | ||
tqdm_pipe( | ||
grep('python', | ||
sink()))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Asynchronous progressbar decorator for iterators. | ||
Includes a default `range` iterator printing to `stderr`. | ||
Usage: | ||
>>> from tqdm.asyncio import trange, tqdm | ||
>>> async for i in trange(10): | ||
... ... | ||
""" | ||
from .auto import tqdm as tqdm_auto | ||
import asyncio | ||
__author__ = {"github.com/": ["casperdcl"]} | ||
__all__ = ['tqdm_asyncio', 'tarange', 'tqdm', 'trange'] | ||
|
||
|
||
class tqdm_asyncio(tqdm_auto): | ||
def __init__(self, iterable=None, *args, **kwargs): | ||
super(tqdm_asyncio, self).__init__(iterable, *args, **kwargs) | ||
self.iterable_awaitable = False | ||
if iterable is not None: | ||
if hasattr(iterable, "__anext__"): | ||
self.iterable_next = iterable.__anext__ | ||
self.iterable_awaitable = True | ||
elif hasattr(iterable, "__next__"): | ||
self.iterable_next = iterable.__next__ | ||
else: | ||
self.iterable_iterator = iter(iterable) | ||
self.iterable_next = self.iterable_iterator.__next__ | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self): | ||
try: | ||
if self.iterable_awaitable: | ||
res = await self.iterable_next() | ||
else: | ||
res = self.iterable_next() | ||
self.update() | ||
return res | ||
except StopIteration: | ||
self.close() | ||
raise StopAsyncIteration | ||
except: | ||
self.close() | ||
raise | ||
|
||
def send(self, *args, **kwargs): | ||
return self.iterable.send(*args, **kwargs) | ||
|
||
@classmethod | ||
def as_completed(cls, fs, *, loop=None, timeout=None, total=None, | ||
**tqdm_kwargs): | ||
""" | ||
Wrapper for `asyncio.as_completed`. | ||
""" | ||
if total is None: | ||
total = len(fs) | ||
yield from cls(asyncio.as_completed(fs, loop=loop, timeout=timeout), | ||
total=total, **tqdm_kwargs) | ||
|
||
|
||
def tarange(*args, **kwargs): | ||
""" | ||
A shortcut for `tqdm.asyncio.tqdm(range(*args), **kwargs)`. | ||
""" | ||
return tqdm_asyncio(range(*args), **kwargs) | ||
|
||
|
||
# Aliases | ||
tqdm = tqdm_asyncio | ||
trange = tarange |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.