lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



> On 12 Oct 2022, at 13:10, bil til <biltil52@gmail.com> wrote:
> 
> Am Mi., 12. Okt. 2022 um 11:11 Uhr schrieb Thijs Schreijer
> <thijs@thijsschreijer.nl>:
>> I’ve followed the discussions here and find it really interesting. A major drawback is the missing ability of Lua to run “nested” coroutines without getting into nasty problems.
>> 
> Can you give an example for this WITHOUT using an "environment like
> Copas" (which I unfortunately do not know?
> 

It depends on how you use coroutines. If you write a for loop, then typically, it is hierarchically nested. That’ll work. But if you have a scheduler which operates your IO and timers, then that scheduler typically is at “the top of the food chain”. 

So if you have a for loop using coroutines (yielding a next iterator result on each “resume”) and then you have a main task (coroutine that started the for-loop and consumes it). And now inside the iterator, there is a need to “sleep”, so it calls [insert-schduler-of-choice].sleep(1). The “sleep” function will yield with parameters to indicate what it wants to do, to the scheduler; coroutine.yield(“sleep”, 1) and this is where the problem hits; that yield will not end up in the scheduler, but in the main task that was running the iterator (because of the hierarchical nature of coroutines). So the iterator (let’s assume it was iterating over IP addresses), suddenly returns a: “sleep", 1 result where the iterator expected to get an IP address.

Thijs