簡體   English   中英

在完美任務“批處理”中使用迭代器

[英]using iterator in prefect task “batching”

我正在使用prefect並定義一個流程來使用 cosmos db 插入文檔。

問題是query_items()調用是可迭代的,對於大型容器,沒有辦法保存 memory 中的所有條目。

我相信我的問題可以簡化為:

  • given an iterator, how can I create batches to be processed (mapped) in a prefect flow?

例子:

def big_iterable_function_i_cannot_change():
    yield from range(1000000) # some large amount of work

@task
def some_prefect_batching_magic(x):
    # magic code here
    pass


with Flow("needs-to-be-batched"):
    some_prefect_batching_magic.map(big_iterable_function_i_cannot_change())

上面的代碼或類似的代碼會給我一個錯誤:

prefect.FlowRunner | Flow run FAILED: some reference tasks failed.

您收到此錯誤是因為您沒有將big_iterable_function_i_cannot_change定義為task prefect實際上並不直接執行flow flow用於制定schedule ,(用dask的說法)——然后用於執行流程(據我所知)。 prefect中的並行化僅在與dask executor一起使用時發生。

這是我對你的flow的看法。 但是,如果您無法將big_iterable_function_i_cannot_change的任務裝飾器添加到task中,請將其包裝在任務中。 最后 - 不確定您是否可以將生成器傳遞給映射任務。

import prefect
from prefect import Flow, Parameter, task

@task
def big_iterable_function_i_cannot_change():
    return range(5) # some large amount of work

@task
def some_prefect_batching_magic(x):
    # magic code here
    pass


with Flow("needs-to-be-batched") as flow:
    itter_res = big_iterable_function_i_cannot_change()
    post_process_res = some_prefect_batching_magic.map(itter_res)

flow.visualize()
state = flow.run()


flow.visualize(flow_state=state)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM