Python Prefect 2.9.0 - TypeError:'fn'必须可调用

rn0zuynd  于 2023-06-07  发布在  Python
关注(0)|答案(1)|浏览(224)

我正在学习Python Prefect,我使用的是2.9.0版本。我正在尝试使用以下代码设置10秒间隔的计划:

@task
def print_values():
    print("Test 1: "+ datetime.now().strftime("%H:%M:%S"))

with Flow("my-flow") as flow:
    print_values()

flow.run_config(
    {"scheduler": IntervalSchedule(interval=timedelta(seconds=10))}
)

但是,我得到以下错误:

Traceback (most recent call last):
  File "D:\...\testworkflow\testSchedule.py", line 17, in <module>
    with Flow("my-flow") as flow:
  File "C:\Users\...\lib\site-packages\prefect\context.py", line 178, in __register_init__
    __init__(__self__, *args, **kwargs)
  File "C:\Users\...\.conda\envs\testenv\lib\site-packages\prefect\flows.py", line 144, in __init__
    raise TypeError("'fn' must be callable")
TypeError: 'fn' must be callable

我已经搜索了Stack Overflow并查看了文档,但我不确定我做错了什么。任何帮助将不胜感激。谢谢你!

cgvd09ve

cgvd09ve1#

对于Prefect 2,您希望使用@flow装饰器。
我会更新到最新的Prefect版本。
如果你想要一个任务和一个流程,你可以这样做:

from prefect import task, flow
from datetime import datetime

@task(log_prints=True)
def print_values():
    print("Test 1: "+ datetime.now().strftime("%H:%M:%S"))

@flow()
def demo():
    print_values()

当连接到Prefect Cloud或Prefect服务器示例时,您可以在CLI中运行以下代码,该代码将创建部署和计划并将其发送到连接的服务器。

prefect deployment build my_file_path.py:demo -n my_deploy -p my_process_pool --interval 10 -a

您只需要一个名为my_process的工作池,并启动一个worker来轮询该工作池中的工作。那么你应该每10秒就能看到跑步。
有几种方法可以创建部署和计划。下面是deployment docsscheduling docs

相关问题