question

Tomi Kosunen avatar image
3 Likes"
Tomi Kosunen asked Phil BoBo edited

Using coroutine in a User command

I tried to use coroutine (await) in a user command with no success. If I run the code in a Process Flow -> Custom code, it works fine. If I take the same code into User command, it does not work. Is that how it is meant to be? See attachment -> "Use await here".

test.fsm


FlexSim 22.1.0
coroutinesuspend methodsuspending
test.fsm (28.4 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Phil BoBo avatar image
1 Like"
Phil BoBo answered Phil BoBo edited

The await keyword returns a coroutine to the calling function if the awaited function cannot be immediately finished. To get a coroutine back and await on it, we added a treenode.coevaluate() method.

If you await from a user command, then the calling function can't get that returned Coroutine object directly in order to await on it. The calling function needs to call treenode.coevalute() instead of simply executing the user command directly.

From the Custom Code activity's code, you need to pass the returned Coroutine back to the Custom Code activity so that it can block the token from progressing until that function returns a value that isn't a Coroutine.

Currently in FlexSim 22.1, to await on the execution of a user command, you would need to call coevaluate on its node rather than calling the command directly:

treenode commandNode = Model.find("Tools/UserCommands/command1/code");
await commandNode.coevaluate(token);

1649088485893.png

I'll add a case to the dev list to consider the implications of changing user command execution into coevaluation so that instead you could simply "await command1(token);" from the Custom Code activity's code.


1649088485893.png (78.9 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.