Page 1 of 1

Python in background

Unread postPosted: 14 Sep 2024, 09:33
by mickenx
Hello.

I have tried to run a python script that runs in backround and pokes a LUA script. It works in the emulator but not on calculator. The python scripts pokes LUA , but I can't change page when it is running,

Is this possible?

Re: Python in background

Unread postPosted: 14 Sep 2024, 11:05
by Adriweb
Hi,

What do you mean exactly by "The python scripts pokes LUA" ?
As far as I know, this is only kind of doable indirectly by changing some variables that the lua script monitors.

Can you share your .tns file?

Re: Python in background

Unread postPosted: 14 Sep 2024, 12:01
by mickenx
Adriweb wrote:Hi,

What do you mean exactly by "The python scripts pokes LUA" ?
As far as I know, this is only kind of doable indirectly by changing some variables that the lua script monitors.

Can you share your .tns file?


I am using change of variables. I followed a example , it might be yours. EDIT: yes it was you:
https://www.cemetech.net/forum/viewtopi ... 06&start=0

The script is running to set variables that animates the graph. Python makes the change happen. I wanted to inject functions but python doesn't like to change variables with strings.

It works just fine on emulator. But on the calculator I can see from the printouts that the on.varChange is working. However I can't change page. The device is singletasking. The tns is kind of mess. but attaching it anyway.

Re: Python in background

Unread postPosted: 14 Sep 2024, 12:50
by Adriweb
Ok I see.
The simulator refreshes (invalidates, rather) the lua stuff much more often than on the calculator (which only does it when needed, to save power), so this is actually expected.

You could try forcing a platform.window:invalidate() inside an on.timer() (don't forget to start the timer first) if you want continuous background update, but check what happens if you do that only inside on.varChange first.

But also yeah, it's not true multitasking, just some event loops.
Try also splitting the screen python/lua in a single tab, to see how it reacts without having to change tabs.

Re: Python in background

Unread postPosted: 14 Sep 2024, 19:18
by mickenx
I had timer initially, but wanted to see if I could do it another way.. I got the CX II some days ago, and it is a decent update to my old CX. I also bought a micro:bit, because of fun.

Re: Python in background

Unread postPosted: 14 Sep 2024, 19:24
by Adriweb
well sure - I'm wondering if on.varChange gets called while a python script is still running, I guess (in which case you can do the "platform.window:invalidate()" inside there)

Re: Python in background

Unread postPosted: 14 Sep 2024, 19:37
by mickenx
Adriweb wrote:well sure - I'm wondering if on.varChange gets called while a python script is still running, I guess (in which case you can do the "platform.window:invalidate()" inside there)

It is called. I print out the variables for the graph in python and they are modified because of the varChange thing. It is interesting to have LUA going as a background. I just wish that I could navigate with python running.

Re: Python in background

Unread postPosted: 14 Sep 2024, 19:43
by Adriweb
Ok yeah, then I guess there isn't much more we can do here, at least one of the languages can run "in background" :P

Re: Python in background

Unread postPosted: 14 Sep 2024, 19:47
by mickenx
dual.jpg
If the shell was shared with the graph graph is animated, but navigation still doesn't work.
Would be awesome if I could inject functions from python. I played with doing it from basic functions,that I called from Python , but unfortunately I can't set global variables in functions.