简体   繁体   English

将 tqdm 与延迟执行与 python 中的 dask 相结合

[英]combining tqdm with delayed execution with dask in python

tqdm and dask are both amazing packages for iterations in python. tqdmdask都是用于 Python 迭代的惊人包。 While tqdm implements the needed progress bar, dask implements the multi-thread platform and they both can make iteration process less frustrating. tqdm实现了所需的进度条,而dask实现了多线程平台,它们都可以使迭代过程不那么令人沮丧。 Yet - I'm having troubles to combine them both together.然而 - 我很难将它们结合在一起。

For example, the following code implements a delayed execution in dask , with tqdm.trange progress bar.例如,以下代码在dask实现了延迟执行,带有tqdm.trange进度条。 The thing is that since the delayed is performed quickly, the progress bar ends immediately, while the real computation-time effort is done during the compute part.问题是,由于delayed执行得很快,进度条会立即结束,而真正的计算时间工作是在compute部分完成的。

from dask import delayed,compute
from tqdm import trange
from time import sleep

ct = time()
result= []

def fun(x):
    sleep(x)
    return x

for i in trange(10):
    result.append(delayed(fun)(i))

print compute(result)

How can I attach the progress bar to the actual execution in compute command?如何将进度条附加到compute命令中的实际执行?

Consider Dask's progress bar 考虑一下Dask的进度条

from dask.diagnostics import ProgressBar

with ProgressBar():
    compute(result)

Build a diagnostic of your own 建立自己的诊断

You can use this plugin architecture to get a signal at the end of every task. 您可以使用此插件体系结构在每个任务结束时发出信号。 http://dask.pydata.org/en/latest/diagnostics.html http://dask.pydata.org/en/latest/diagnostics.html

Here is an example of someone doing exactly this: https://github.com/tqdm/tqdm/issues/278 这是某人正是这样做的示例: https : //github.com/tqdm/tqdm/issues/278

Based on :基于 :

Dask Integration数据集成

from tqdm.dask import TqdmCallback

with TqdmCallback(desc="compute"):
    ...
    arr.compute()

# or use callback globally
cb = TqdmCallback(desc="global")
cb.register()
arr.compute()

Applied to the code in the question:应用于问题中的代码:

from dask import delayed,compute
from tqdm.auto import tqdm
# from tqdm import trange
from time import sleep

from tqdm.dask import TqdmCallback

# ct = time()
result= []

def fun(x):
    sleep(x)
    return x

for i in tqdm(range(10)):
    result.append(delayed(fun)(i))

with TqdmCallback(desc="compute"):
    print(compute(result))

screenshot of output in jupyter : jupyter 输出截图: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM