Hi everyone. I am trying to use `PrefectResult` in...
# ask-community
p
Hi everyone. I am trying to use
PrefectResult
in a flow that has a resource manager. The idea is to easily allow flow restarts without having to set up a different result backend. However, I am running into an issue with the resource manager tasks. The
setup
method of the resource manager returns an object that is not json serializable which causes the task to fail. Do you have any suggestions to workaround this? Is the only solution to use a different result backend for this task? Thanks!
k
Hi @Pedro Machado, I guess this is because the default serializer for results is to pickle them. Maybe you can try defining a serializer like this:
Copy code
class NoOpSerializer(Serializer):
    """A `Serializer` that does nothing."""

    def serialize(self, value):
        return value

    def deserialize(self, value):
        return value
Then
Copy code
PrefectResult(serializer=NoOpSerializer())
p
Hi Kevin, thanks for your reply. Let my try that.
Hi Kevin, I got
Copy code
TypeError: PrefectResult only supports JSONSerializer or DateTimeSerializer
I am going to try to use a different result backend for this task (I still have
PrefectResult
at the flow level). Currently, my resource manager is defined in a different python module as follows:
Copy code
@resource_manager
class TableauServer:
   ...
then, it's imported in the flow and used as a context manager:
Copy code
with TableauServer(
        user=tableau_user,
        password=tableau_password,
        server_url=tableau_server_url,
        site=tableau_site,
    ) as server:
        ...
I tried assigning a
result
to the context manager and it did not work. Do you have any suggestions to allow me to define the resource manager in a separate module and use a results class when using it?
k
I looked around and it looks like you just can’t assign a result to the context manager. You need to assign it on the Flow level/task level. If your code looks like:
Copy code
with Flow("example") as flow:
    with MyResource(...) as resource:
        some_task(resource)
        other_task(resource)
Then the Result would be on
some_task
and
other_task
p
I am going to have to refactor the task to avoid the resource manager. I did not find a way to set a result on the
setup
task from the flow. The resource manager is not essential in this case, but it would be nice to figure out a pattern to be able to use PrefectResults at the flow level with resource managers when the resource is not json serializable.
Thanks for your help! Let me know if you think of anything else.
k
What is your setup task returning?