lua-users home
lua-l archive

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


Thanks for the responses. My issue is that the scripts I want to run will be installed at run time and will come from a 3rd party. It will be difficult to plan cooperative multitasking if every script is coming from a different source and then there may be a possibility of unreliable scripts which get stuck somewhere and never reach the yield. Maybe there is a way which I am not aware of.

In the meantime I gave it a try to use debug library sethook function with coroutines to run multiple scripts and here is a program I tried:

xco = ""
yco = ""
doing = ""

runningTasks = {}
function scheduler (event, line)
    local xdone,ydone
    for i= 1,#runningTasks do
        if runningTasks[i] == xco then
            xdone = true
        end
        if runningTasks[j] == yco then
            ydone = true
        end
    end
    if not xdone then
        runningTasks[#runningTasks + 1] = xco
        doing = xco
        print(coroutine.resume(xco))
    elseif not ydone then
        runningTasks[#runningTasks + 1] = yco
        doing = yco
        print(coroutine.resume(yco))
    else
        if doing == xco then
            doing = yco
            coroutine.resume(yco)
        else
            doing = xco
            coroutine.resume(xco)
        end
    end
end

x = loadstring([[function coyield(event,line) coroutine.yield() end debug.sethook(coyield, "l") for i = 1,5 do
    print("x"..tostring(i))
end]])

y = loadstring([[function coyield(event,line) coroutine.yield() end debug.sethook(coyield,"l") for i = 1,10 do print("y"..tostring(i)) end]])

xco = coroutine.create(x)
yco = coroutine.create(y)

scheduler()



-------------------------------------------------------------------------------------------------------------

I get an error saying I cannot yield across C-call boundary. That is probably because the script I execute is loaded with loadstring. I even tried making modules of the scripts and requiring them. That also did not work.

So we can't use coroutines across modules? Any way we can do this?


Thanks,
Milind



On Fri, Jan 31, 2014 at 10:52 AM, Andrew Starks <andrew.starks@trms.com> wrote:


On Friday, January 31, 2014, Jorge <xxopxe@gmail.com> wrote:
On 01/31/2014 03:14 PM, Andrew Starks wrote:
This looks really awesome. I was thinking along similar lines as this,
except with a focus on bsd sockets as the interface for organizing
message passing, etc.

I like the structure of this, though. We need inter-(os) thread
communication, so we're playing with nanomsg. It'd be interesting to see
how this would interact with message passing between concurrent threads.
It seems like it could work well...

Lumen has a signal proxy, so you can wait for signals on a remote instance [1].
Signals are passed trough a TCP socket, serialized with either bencode or json. It worked for what I wanted at the moment [2], but have plans for supporting also UDP, as it makes sense in some case uses (including the referenced example :) ).
For now it depends on either luasocket or nixio for socket support, and I have a half-baked ljsycall backend.
Nanomsg would'be a great candidate for transport tough, that's true.

Jorge

[1] http://xopxe.github.io/Lumen/modules/proxy.html
[2] https://github.com/xopxe/Toribio/blob/master/docs/1-Tutorial.md#remote-control-using-a-proxy

I poked around with this. It is pretty sweet. 

I fixed some issues with 5.2, require paths, and windows sleeping. 

I'll submit a pull request later today, once I learn how to do that. :)

-Andrew