Get Dynamically Updating Values while Python Script is Running #2025
-
Hello, I was wondering if the Python Scripting package had the capability of getting values that update dynamically while the script is still running. Currently, it seems that the Get and Eval modules only take the last value once the script finishes, and they only subscribe when the script is done. Ideally, I would like Get and/or Eval to output the current value of the variable, even if the program has not finished executing. My goal with this is to create a user-friendly Python GUI for a Bonsai program we're running, which might be too lofty of an idea. An example could be to change the trial duration in the GUI and have Bonsai get that value to set the duration of a Timer module. This change would happen while the GUI Is still open, so users could play around with parameters and test them in Bonsai. Currently, we can do this if we set all values in the GUI and then close it (which is amazing on its own), but it would be nice if it could remain open. To make things easier, I was testing this with a simple Python script, and the ideal outputs would be 1, then 2, then 3, etc. until 100:
But currently, it would just output 100. Is there any way of doing this (ie. outputting 1, 2, 3, etc.) with the Scripting.Python modules? I have workarounds if not, it would just be extremely convenient to do it this way. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi there, Yes, this is possible. The key lies in performing the example() function asynchronously. The current behavior you're seeing is due to python's Global Interpreter Lock (GIL), which ensures that the Python code you are running executes synchronously in a single thread. To access your variables dynamically, you'll need to run your function asynchronously in a separate thread. Fortunately, there are several options for doing this in Python. I'm attaching an example using the Bonsai.Scripting.Python package to demonstrate one approach that I've had success with. It uses the python packages In the bonsai workflow, you simply call the run function using Not sure if this is useful, but we use a similar approach for the Bonsai.ML package to fit model parameters asynchronously during optimization while still allowing the model to perform online inference without being blocked. Hopefully this helps. |
Beta Was this translation helpful? Give feedback.
Hi there,
Yes, this is possible. The key lies in performing the example() function asynchronously. The current behavior you're seeing is due to python's Global Interpreter Lock (GIL), which ensures that the Python code you are running executes synchronously in a single thread. To access your variables dynamically, you'll need to run your function asynchronously in a separate thread. Fortunately, there are several options for doing this in Python.
I'm attaching an example using the Bonsai.Scripting.Python package to demonstrate one approach that I've had success with. It uses the python packages
asyncio
andthreading
to run the example function asynchronously in a seperate thread. The exam…