Use coroutine.yield from outside of the child function for use in a coroutine scheduler

I have browsed several topics regarding this, and one thing complexes me. How they are able to yield the coroutine from outside of the function. Apparently you can use a C API hook, but I cannot afford to setup C in my environment. Does anyone have a somewhat simple idea to do this?

Hi.

You could try debug.hook() from Lua, if you think that approach has merit.

What libraries or code are you studying? I think Copas and lua-coronest schedule things in a more conventional way. I vaguely recall one or two others, e.g. Lumen, being mentioned here, but I think some of them are C-based.

Using what you have said with debug.sethook, I have done something like this:

co = coroutine.create(function() while true do print("Worked") end end) debug.sethook(co,function() coroutine.yield() end, "clr", 0) coroutine.resume(co)

It does yield the coroutine, but my only issue is that it isn’t displaying any “Worked” strings. I assume this is because it is being yielded before it can execute. Is there a way to yield after maybe so many lines execute?

Ah. I put a print() around the resume call; it’s reporting an error about yielding across a C boundary. So I guess you do need C to go this route. (This might also be fragile when Corona strips debug symbols.)

What are you trying to do? Would any of those other schedulers work?

I haven’t tried the above schedulers yet just getting ideas from them. One that I have looked at that is somehow able to yield ishttps://ilitomic.net/coroutines-scheduler-in-lua/, but put simply the scheduler needs to be able to run coroutines for a certain amount of time(either by the number of lines of code executed, or a timeout timer) and then yield that coroutine, and move on to the next one. I wonder if a timeout timer might work better for this. Maybe set a for loop timer in the debug.sethook that can be reset somehow but there is still the issue that its not executing the coroutine like it should.

I avoid co routines like the plague - I just don’t find them easy/logic to use.

I split processing across multiple enterFrames() and find this much easier to write, test and debug.

Hi.

You could try debug.hook() from Lua, if you think that approach has merit.

What libraries or code are you studying? I think Copas and lua-coronest schedule things in a more conventional way. I vaguely recall one or two others, e.g. Lumen, being mentioned here, but I think some of them are C-based.

Using what you have said with debug.sethook, I have done something like this:

co = coroutine.create(function() while true do print("Worked") end end) debug.sethook(co,function() coroutine.yield() end, "clr", 0) coroutine.resume(co)

It does yield the coroutine, but my only issue is that it isn’t displaying any “Worked” strings. I assume this is because it is being yielded before it can execute. Is there a way to yield after maybe so many lines execute?

Ah. I put a print() around the resume call; it’s reporting an error about yielding across a C boundary. So I guess you do need C to go this route. (This might also be fragile when Corona strips debug symbols.)

What are you trying to do? Would any of those other schedulers work?

I haven’t tried the above schedulers yet just getting ideas from them. One that I have looked at that is somehow able to yield ishttps://ilitomic.net/coroutines-scheduler-in-lua/, but put simply the scheduler needs to be able to run coroutines for a certain amount of time(either by the number of lines of code executed, or a timeout timer) and then yield that coroutine, and move on to the next one. I wonder if a timeout timer might work better for this. Maybe set a for loop timer in the debug.sethook that can be reset somehow but there is still the issue that its not executing the coroutine like it should.

I avoid co routines like the plague - I just don’t find them easy/logic to use.

I split processing across multiple enterFrames() and find this much easier to write, test and debug.