Hi folks, I have a Flow defined with a few tasks i...
# ask-community
m
Hi folks, I have a Flow defined with a few tasks involved. Plan is to call a task from another task that’s called from a flow
With
context. Has anyone been able to successfully do that?
k
If you just want to use the task in another,
task.run()
ill run but the Python underneath but this will not be treated as a task with it’s own states and retries. It sounds like maybe you want to do some kind of
if-else
conditional to run another task?
m
Yeah basically a conditional; but take a look at this error: This was raised after I called
task.run()
from another task that gets called in flow with context.
k
Could you show me the code? Or make a smaller example?
m
Here is the code that calls the other task.
k
This looks alright. How is it used in the Flow?
m
This is the flow definition
k
I think
batch_num
needs to be in the
run()
. That’s the only thing I can see immediately
m
Hm ok, let me try it out. thanks @Kevin Kho
k
It should be
query_fishnet.run(batch_num=10)
m
Still failing; have you been able to test this before?
k
Yep let me give you an example
Check this
Copy code
import prefect
from prefect import task, Flow

@task
def abc(x):
    return x+1

@task
def bcd():
    x = abc.run(1)
    logger = prefect.context.get("logger")
    <http://logger.info|logger.info>(x)

with Flow("TestFlow") as flow:
    bcd()

flow.run()
And doing
x = abc(1).run()
replicates your error:
Copy code
`ValueError: Could not infer an active Flow context while creating edge to <Task: abc>. This often means you called a task outside a `with Flow(...)` block. If you're trying to run this task outside of a Flow context, you need to call `abc.run(...)`
m
Its strange, I’m still getting the same error! anything else you can think of?
k
Can you show me your syntax for the
run
call?
x=abc().run(1)
will also throw an error. I don’t fully remember the code, but do you call that task anywhere else?
m
Sure, here
I don’t call the `query_fishnet()`task anywhere else!
k
Remove the
()
right after
query_fishnet
. Change
query_fishnet().run(10)
to
query_fishnet.run(10)
m
That was it! Thank you 🙂
👍 1