How to use prefect - 10 common examples

To help you get started, we’ve selected a few prefect examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PrefectHQ / prefect / src / prefect / tasks / core / operators.py View on Github external
def __init__(self, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)

    def run(self, x: Any, y: Any) -> Any:  # type: ignore
        """
        Args:
           - x (Any): a value
           - y (Any): a value

        Returns:
            - Any
        """
        return x // y


class Pow(Task):
    """
    Evaluates `x ** y`

    Args:
        - *args (Any): positional arguments for the `Task` class
        - **kwargs (Any): keyword arguments for the `Task` class
    """

    def __init__(self, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)

    def run(self, x: Any, y: Any) -> Any:  # type: ignore
        """
        Args:
           - x (Any): a value
           - y (Any): a value
github PrefectHQ / prefect / tests / engine / test_task_runner.py View on Github external
def test_task_runner_skips_upstream_check_for_parent_mapped_task_but_not_children(
    executor,
):
    add = AddTask(trigger=prefect.triggers.all_failed)
    ex = Edge(SuccessTask(), add, key="x")
    ey = Edge(ListTask(), add, key="y", mapped=True)
    runner = TaskRunner(add)
    with executor.start():
        res = runner.run(
            upstream_states={ex: Success(result=1), ey: Success(result=[1, 2, 3])},
            executor=executor,
        )
        res.map_states = executor.wait(res.map_states)
    assert isinstance(res, Mapped)
    assert all([isinstance(s, TriggerFailed) for s in res.map_states])
github PrefectHQ / prefect / tests / engine / test_result.py View on Github external
        Result(object, result_handler=LocalResultHandler()),
        NoResult,
        SafeResult("3", result_handler=JSONResultHandler()),
    ],
)
def test_everything_is_pickleable_after_init(obj):
    assert cloudpickle.loads(cloudpickle.dumps(obj)) == obj
github PrefectHQ / prefect / tests / environments / storage / test_docker_storage.py View on Github external
def test_pull_image(capsys, monkeypatch):
    storage = Docker(base_image="python:3.6")

    client = MagicMock()
    client.pull.return_value = [{"progress": "test", "status": "100"}]
    monkeypatch.setattr("docker.APIClient", MagicMock(return_value=client))

    storage.pull_image()
    captured = capsys.readouterr()
    printed_lines = [line for line in captured.out.split("\n") if line != ""]

    assert any(["100 test\r" in line for line in printed_lines])
github PrefectHQ / prefect / tests / engine / test_task_runner.py View on Github external
def test_non_failed_states(self, state):
        new_state = TaskRunner(task=Task()).check_for_retry(state=state, inputs={})
        assert new_state is state
github PrefectHQ / prefect / tests / engine / test_task_runner.py View on Github external
def test_mapped_tasks_parents_and_children_respond_to_individual_triggers():
    task_runner_handler = MagicMock(side_effect=lambda t, o, n: n)

    runner = TaskRunner(
        task=Task(trigger=prefect.triggers.all_failed),
        state_handlers=[task_runner_handler],
    )
    state = runner.run(
        upstream_states={Edge(Task(), Task(), mapped=True): Success(result=[1])}
    )
    # the parent task changed state two times: Pending -> Mapped -> Mapped
    # the child task changed state one time: Pending -> TriggerFailed
    assert isinstance(state, Mapped)
    assert task_runner_handler.call_count == 3
    assert isinstance(state.map_states[0], TriggerFailed)
github PrefectHQ / prefect / tests / engine / test_task_runner.py View on Github external
def test_with_two_finished(self):
        state = Pending()
        new_state = TaskRunner(Task()).check_upstream_finished(
            state=state, upstream_states={1: Success(), 2: Failed()}
        )
        assert new_state is state
github PrefectHQ / prefect / tests / engine / test_task_runner.py View on Github external
def test_unwrap_nested_meta_states(self):
        state = Retrying(run_count=1)
        result = TaskRunner(Task()).initialize_run(
            state=Submitted(state=Queued(state=Submitted(state=Queued(state=state)))),
            context={},
        )
        assert result.state is state
github PrefectHQ / prefect / tests / engine / test_task_runner.py View on Github external
def test_any_successful_pass(self):
        task = Task(trigger=prefect.triggers.any_successful)
        state = Pending()
        new_state = TaskRunner(task).check_task_trigger(
            state=state, upstream_states={1: Success(), 2: Failed()}
        )
        assert new_state is state
github PrefectHQ / prefect / tests / engine / test_task_runner.py View on Github external
def test_all_of_run_context_is_available_to_custom_cache_validators(self):
        ctxt = dict()

        def custom_validator(state, inputs, parameters):
            ctxt.update(prefect.context.to_dict())
            return False

        # have to have a state worth checking to trigger the validator
        with prefect.context(caches={"Task": [State()]}):
            task = Task(
                cache_for=timedelta(seconds=10), cache_validator=custom_validator
            )
            state = TaskRunner(task).run()

        expected_subset = dict(
            map_index=None,
            task_full_name="Task",
            task_run_count=1,
            task_name="Task",
            task_tags=set(),
            task_slug=task.slug,
            checkpointing=False,
        )
        for key, val in expected_subset.items():
            assert ctxt[key] == val