Hi! I am trying to call a task inside a for-loop, ...
# ask-community
s
Hi! I am trying to call a task inside a for-loop, but keep getting the following error:
TypeError: Task is not iterable.
y
Do you have some code snippet for it?
Not that if you add the @task decorator on a function, it will no longer be the same function
s
I think it is because I am trying to iterate over a Prefect parameter. This is the essence of the code:
markets = Parameter('markets', default=['malmo'])
for market in markets:
load_data(market)
y
A simple fix will be taking the
for loop
out of the Flow part, and rewrite it it as a task. Also, the
map
feature in Prefect can also solve the problem
Yeah, it seems like map can do the trick
s
Thanks, will check it out!! 🙂
b
Yes, the result of calling
Parameter
is not a list of markets but a
Task
. You can do
load_data.map(markets)
to run a
load_data
task for each of the markets I think.
s
Yes, I got it sorted out now. Thanks for your replies!
k
I know this is sorted but just want to provide more detail. Task execution is deferred meaning that the value doesn’t exist until run time. The List access
for market in markets
,
markets[0]
or Dict access are done during build time then the Tasks have not been evaluated yet, which is why you get the error. Yes mapping is a great way to dynamically handle this.