Vadym Dytyniak

    Vadym Dytyniak

    9 months ago
    Hi. Is it any difference in task calling syntax?
    class SetupStorageTask(Task): 
        def run(self, mode) -> Storage:    
            return Storage(mode=mode)
    
    setup_storage_task = SetupStorageTask()
    storage = setup_storage_task(mode='rw')
    and
    class SetupStorageTask(Task):
    
        def __init__(self, mode):
            self.mode = mode
        
        def run(self) -> Storage:
            return Storage(mode=self.mode)
    
    setup_storage_task = SetupStorageTask(mode='rw')
    storage = setup_storage_task()
    
    or can be just
    storage = SetupStorageTask(mode='rw')
    Anna Geller

    Anna Geller

    9 months ago
    Both can work, it depends on how you want to use this task. E.g. if you want to pass some data into the task, then it makes sense to accept this argument within the “run” method rather than only on init. Many tasks in our task library set all attributes as optional allowing them to be set both at initialization and within the Flow constructor and include some logic within the task itself to check whether the required values are set and if not raise ValueError. I think if you have a look at a couple of tasks in the task library code, you’ll get a better grasp of how it works.
    @Vadym Dytyniak but on the other note: do you wish to use it to set Flow storage? if so, I think this won’t work, because Storage doesn’t inherit from Task
    Vadym Dytyniak

    Vadym Dytyniak

    9 months ago
    no, it is project related storage
    but what about
    setup_storage_task = SetupStorageTask(mode='rw')
    storage = setup_storage_task()
    
    or can be just
    
    storage = SetupStorageTask(mode='rw')
    looks like both versions works
    if run does not have args
    you can just use storage = SetupStorageTask(mode='rw')
    is it true?
    Kevin Kho

    Kevin Kho

    9 months ago
    It has to be :
    storage = SetupStorageTask(mode='rw')()
    The first parenthesis is the
    init
    and the second is the
    run
    Vadym Dytyniak

    Vadym Dytyniak

    9 months ago
    but it works without last ()
    Anna Geller

    Anna Geller

    9 months ago
    yes, you can. Again, I think you’ll get a better feel for it when you look at some existing tasks, there are many that use a similar pattern but if you’re asking how it should be called within Flow, then as Kevin said:
    with Flow() as flow:
       storage = SetupStorageTask(mode='rw')()
    or initialize first and then call in a flow:
    xyz = SetupStorageTask(mode='rw')
    with Flow() as flow:
        xyz()
    Vadym Dytyniak

    Vadym Dytyniak

    9 months ago
    It makes sense I just was surprised that without last () it do the same thing
    probably just sideeffect
    run without args