Andreas Nord
02/06/2024, 10:15 AMfrom prefect import task, flow
from prefect.utilities.annotations import quote
@task
def big_output():
return [1 for _ in range(10**7)]
@task
def big_input(x):
print(len(x))
@flow
def slow_flow():
x = big_output.submit()
big_input.submit(quote(x))
if __name__ == "__main__":
slow_flow()
I'm trying to get the big_input task to start quickly after big_output task, but I get this error:
TypeError: object of type 'PrefectFuture' has no len()Marvin
02/06/2024, 10:16 AMquote
with task.submit()
.
However, searching the GitHub issues for the error you mentioned did not yield any directly related issues in the PrefectHQ repository.
To address your usage question with quote
and task.submit()
, here's what you need to know:
The quote
function from Prefect's utilities.annotations
module is used to avoid early evaluation of task outputs, which allows you to compose task dependencies that pass outputs between tasks without serializing and copying large data immediately.
To fix the TypeError
you're facing, big_input.submit()
needs to properly unwrap the quoted future. When you pass quote(x)
to big_input
, you're passing a PrefectFuture
object, and you cannot take the length of that future directly, as it is not the actual result of the task.
One potential solution is to have big_input
wait for the actual value of x
inside the task. This can be done using the get
method on the future object. Here's a simple modification to your big_input
function:
python
@task
def big_input(x):
real_value = x.get() # Unwrap the actual value from the future.
print(len(real_value))
And your slow_flow
function should remain unchanged:
python
@flow
def slow_flow():
x = big_output.submit()
big_input.submit(quote(x))
This should allow your code to execute without the TypeError
, as the get()
method on the quoted Prefect future is used to obtain the real value once it's available.
If you still encounter issues, you might need deeper insight into Prefect's execution model and the specific use of quote
with task.submit()
. You could consider reaching out on Prefect's community Slack or support channels for more direct assistance.