Thread
#prefect-community
    s

    shijas km

    7 months ago
    Hi I am facing an issue I need to add a shell task before my python tasks in a flow but its not working , throwing some error like flow outside cant be called but when i comment out all task except the shell task it works can I combine both shell task and python task in a single flow? any example??
    Kevin Kho

    Kevin Kho

    7 months ago
    Hi @shijas km,you have two options. First as the first task of the Flow
    shell = ShellTask(...)
    with Flow(...) as flow:
        s = shell()
        something_else(upstream_tasks=[s])
    if you really need it before the flow begins, you need to use it in a State Handler . This is not a task though. It’s just like a Python function.
    def mystatehandler(obj,old_state,new_state):
        if new_state.is_running():
            ShellTask().run(...)
    
    with Flow(..., state_handlers=[mystatehandler]) as flow:
        ....
    s

    shijas km

    7 months ago
    so what is this something__else()
    Nate

    Nate

    7 months ago
    @shijas km hi! here's a very basic but functional example using both
    ShellTask
    and normal prefect tasks
    from prefect import Flow, task
    from prefect.tasks.shell import ShellTask
    
    @task(log_stdout=True)
    def regular_task():
        print('hi')
    
    my_shell_task = ShellTask(
        helper_script="cd ~",
    )
    
    with Flow("My Flow") as flow:
        # task will be executed in home directory
        contents = my_shell_task(command='ls')
    
        regular_task()
    
    if __name__ == "__main__":  
        flow.run()
    s

    shijas km

    7 months ago
    where do we run flow.run()
    Kevin Kho

    Kevin Kho

    7 months ago
    something_elsewould be another task but Nate has a full example for you
    s

    shijas km

    7 months ago
    @Nate but I need the shell comand to run first , is this possible?
    Kevin Kho

    Kevin Kho

    7 months ago
    You can set it as the upstream task or put it in the state handler like i outlined earlier
    s

    shijas km

    7 months ago
    @Kevin Kho ok to make it simple if I call shell task first and then call the other task then it will run in that order rite is that fine
    Kevin Kho

    Kevin Kho

    7 months ago
    Order is not guaranteed unless you set it as an explicit upstream like my first code snippet above