prefect入門見上篇https://my.oschina.net/u/914655/blog/5272886
本篇從並行任務角度出發。
代碼
import time
from prefect import task, Flow
@task(nout=2)
def prenums():
time.sleep(2)
print('prepare nums')
return [1,1,1], [2,2,2]
@task
def add(a, b):
time.sleep(2)
print(a, b)
return a + b
@task
def sum_fn(nums):
time.sleep(2)
print(f'sum: {nums}')
return sum(nums)
@task
def echo(n):
print(f'echo sum: {n}')
@task
def email(n):
time.sleep(2)
print(f'email sum, {n}')
@task
def wechat(n):
print(f'wechat num. {n}')
@task
def done():
print('done')
with Flow("Run Me") as flow:
a, b = prenums()
h = add.map(a=a, b=b)
n = sum_fn(h)
done(upstream_tasks=[echo(n), email(n), wechat(n)])
from prefect.executors import DaskExecutor
executor = DaskExecutor(address="tcp://localhost:8786")
flow.run(executor=executor)
不同任務的並行
主要在此處done(upstream_tasks=[echo(n), email(n), wechat(n)])
done收尾任務前面三個不同的並行任務,其依賴關係是由upstream_tasks參數來指定的
同任務的並行
見 h = add.map(a=a, b=b)
add一個任務給予了多個參數
任務依賴
如上,upstream_tasks是指定無參數返回任務的依賴用法
如要執行task有引用其他task的返回,則prefect會自動賦予依賴關係,如:n = sum_fn(h),h就是上一個任務,需要上一個完成,才能做這個任務,自然就是個chain了