question

Tomi Kosunen avatar image
0 Likes"
Tomi Kosunen asked Kavika F commented

Synchronous Python command

Hi. How do execute synchronous Python command? I'm sending queries to a server from Python command. It takes some time to get an answer from the server so I need to stop the FlexSim clock for that time. In Http request I can use request.sendAndWait(); but how the same is done with Python?

In the example, the server response delay is modelled with sleep command in Python code. The Processor is asking for a process time from Python.

testWait.fsm

time_funktio.py

FlexSim 23.1.1
pythonsynchronous
testwait.fsm (41.5 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

·
Kavika F avatar image
0 Likes"
Kavika F answered Kavika F commented

Hey @Tomi Kosunen, there are a few resources I can point you to to help you setup your queries. Here's a post I made a few months back about someone wanting to run an experimenter, updating parameters through python and a server/client connection. It uses the serverreceive() command which has the option to block the model (i.e., the server) from running until it receives a message from your python code (i.e., the client). There are some custom User Commands to simplify the process in the model from the post, so be sure to take a look at those.

1682528885202.png


1682528885202.png (3.6 KiB)
· 4
5 |100000

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

Tomi Kosunen avatar image Tomi Kosunen commented ·

Thanks @Kavika F Are you sure you linked the correct reference to a earlier post? The " a post I made a few months back" goes to question about AGV trigger and I cannot find anything about Python nor socket connectons.

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦ Tomi Kosunen commented ·
1 Like 1 ·
Tomi Kosunen avatar image Tomi Kosunen commented ·

Thanks @Kavika F . So if I understand correct, the solution would be to open socket connection to Python api and then run Python thru that connection. It would probably work but in this case I think I'll just convert the Python code to FlexScript and use the sendAndWait() command instead.

0 Likes 0 ·
Kavika F avatar image Kavika F ♦ Tomi Kosunen commented ·
Yes that is certainly one way you could do it. Using the client/server and socket commands would be useful. You could also try using coroutines and await statements if you have specific events or time in FlexSim you're waiting for, but I'm less certain about that approach.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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