transitions icon indicating copy to clipboard operation
transitions copied to clipboard

How to achieve other conditions except default `conditions' or `unless' in one condition

Open firefoxbug opened this issue 11 years ago • 1 comments

Hi, dear tyarkoni

I have another question whiling using your FSM, I want to achieve like this: start a task to wget a large file, and then to check is the wget task finished or not once in a while, because result may be three status: wget_ing, wget_failure, wget_success. Pseudocode is like bellow:

def start_wget():

    return 0

def check_wget():

    return random('WGET_RUNNING', 'WGET_SUCCESS', 'WGET_FAILURE')

# WGET_EXEC
# WGET_RUNNING
# WGET_SUCCESS
# WGET_FAILURE

state = 'WGET_EXEC'

# here we do not care
start_wget()

result = check_wget()

if result == 1:
    state = 'WGET_RUNNING'
elif result == -1:
    state = 'WGET_FAILURE'
elif result == 0:
    state = 'WGET_SUCCESS'

And I do not know how to achieve in your FSM, please help me!

transitions = [
    { 
        'trigger': 'mycall', 
        'source': 'WGET_EXEC', 
        'dest': 'WGET_RUNNING' ,
        'conditions': 'start_wget'
    },
    { 
        'trigger': 'mycall', 
        'source': 'WGET_RUNNING', 
        'dest': 'WGET_SUCCESS' ,
        'conditions': 'check_wget' 
    },
    { 
        'trigger': 'mycall', 
        'source': 'WGET_RUNNING', 
        'dest': 'WGET_RUNNING' ,
    },
    { 
        'trigger': 'mycall', 
        'source': 'WGET_RUNNING', 
        'dest': 'WGET_FAILURE' ,
    },
   ... 
]

fsm = Machine(states=states, initial='WGET_EXEC')

firefoxbug avatar Dec 01 '15 13:12 firefoxbug

I think you're asking a more general programming question that isn't really specific to this package, but maybe take a look at #36, as variants of this issue have come up before. The short of it is that you probably want to run a while loop to poll state periodically and trigger events as needed. But this kind of thing should probably be posted to Stack Overflow in future, as it's about general usage.

tyarkoni avatar Dec 01 '15 16:12 tyarkoni