UIs and automatically adding yield()

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

UIs and automatically adding yield()

Abhijit Nandy
Hi,

I have a long running chunk of Lua code which runs in the UI thread of my application. To keep the UI responsive, I sometimes run this in a coroutine and yield() in between so the UI can process pending messages. I can manually add the yields() if the code is under my control. However some of the code is submitted by my users and I would like to add the yields() later in the correct points by modifying the code string. But this is prone to errors, especially if there are multi-line strings in the code or statements are split into multiple lines.

Is there anyway to do this at regular intervals in Lua code that's certain not to break syntax?

Alternatively can the Lua interpretor be setup to yield automatically at regular intervals when running a coroutine, at safe points? So basically move from a co-operative to a pre-emptive model?

The other alternative for me has been to execute the long running code in another thread, but if possible, I would like to avoid threads for now.

Has anyone had this issue and how did you solve it without threads?

Thanks,
Abhi



Reply | Threaded
Open this post in threaded view
|

Re: UIs and automatically adding yield()

Sam Putman


On Mon, Jun 17, 2019 at 12:20 PM Abhijit Nandy <[hidden email]> wrote:
Hi,

I have a long running chunk of Lua code which runs in the UI thread of my application. To keep the UI responsive, I sometimes run this in a coroutine and yield() in between so the UI can process pending messages. I can manually add the yields() if the code is under my control. However some of the code is submitted by my users and I would like to add the yields() later in the correct points by modifying the code string. But this is prone to errors, especially if there are multi-line strings in the code or statements are split into multiple lines.

Is there anyway to do this at regular intervals in Lua code that's certain not to break syntax?


The pattern I use to do this is to replace

return val, msg

with 

yield(val, msg)
return nil

This seems tractable to change in bytecode, though you'd have to research how for
a given Lua engine.

If you wanted to yield out of time-consuming loops, you can do a similar thing at the 
end:

for k,v in pairs(t) do
   --[[...]]
  yield()
end

Hope this helps! Can't guarantee it won't break your code. But it shouldn't.

cheers,
-Sam.
Reply | Threaded
Open this post in threaded view
|

Re: UIs and automatically adding yield()

aryajur
In reply to this post by Abhijit Nandy
I wrote a module which allowed me to load some Lua Code and execute it in a controlled way yielding it after a certain number of steps of bytecode execution. It allowed me to run multiple Lua Scripts in parallel all controlled by a master Lua script. It may be useful to have a look at it:  https://github.com/aryajur/LuaStepper

On Mon, Jun 17, 2019 at 4:20 AM Abhijit Nandy <[hidden email]> wrote:
Hi,

I have a long running chunk of Lua code which runs in the UI thread of my application. To keep the UI responsive, I sometimes run this in a coroutine and yield() in between so the UI can process pending messages. I can manually add the yields() if the code is under my control. However some of the code is submitted by my users and I would like to add the yields() later in the correct points by modifying the code string. But this is prone to errors, especially if there are multi-line strings in the code or statements are split into multiple lines.

Is there anyway to do this at regular intervals in Lua code that's certain not to break syntax?

Alternatively can the Lua interpretor be setup to yield automatically at regular intervals when running a coroutine, at safe points? So basically move from a co-operative to a pre-emptive model?

The other alternative for me has been to execute the long running code in another thread, but if possible, I would like to avoid threads for now.

Has anyone had this issue and how did you solve it without threads?

Thanks,
Abhi



Reply | Threaded
Open this post in threaded view
|

Re: UIs and automatically adding yield()

Thomas Jericke
In reply to this post by Abhijit Nandy
Am 17.06.2019 um 13:19 schrieb Abhijit Nandy:

Is there anyway to do this at regular intervals in Lua code that's certain not to break syntax?


You can yield form a counthook. Just make sure to check if you actually can yield by calling lua_isyieldable or coroutine.isyieldable first. There are some situation where Lua can't yield.

--

Thomas

Reply | Threaded
Open this post in threaded view
|

Re: UIs and automatically adding yield()

Abhijit Nandy
Thanks a lot everyone. I will try out the text replacement pattern and the debug hook options :)

On Tue, Jun 18, 2019 at 1:58 PM Thomas Jericke <[hidden email]> wrote:
Am 17.06.2019 um 13:19 schrieb Abhijit Nandy:

Is there anyway to do this at regular intervals in Lua code that's certain not to break syntax?


You can yield form a counthook. Just make sure to check if you actually can yield by calling lua_isyieldable or coroutine.isyieldable first. There are some situation where Lua can't yield.

--

Thomas