Code Monkey home page Code Monkey logo

glua-async's Introduction

glua-async

An async/await implement for gopher-lua.

Installation

go get github.com/CuberL/glua-async

Example Usage

First, you need to modify your blocking function by wrapping the blocking part(sleep, i/o, etc) in async.AsyncRun. And the return values of the async.AsyncRun will be the return values of the await.

func Sleep(L *lua.LState) int {
    sec := L.CheckInt(1)
    caller := L.CheckString(2)

    async.AsyncRun(func() []lua.LValue {
        time.Sleep(time.Second * time.Duration(sec))
        return []lua.LValue{lua.LString(caller)}
    }, L)

    return 0
}

And you can use it in your function. Use async to start a coroutine and use await to wait some blocking result in an async function.

function main(str)
    -- A
    async(function()
        for i=3, 1, -1 do
            result = await(sleep, 1, "a")
            print("it's "..result)
        end
    end)

    -- B
    async(function()
        for i=3, 1, -1 do
            result = await(sleep, 2, "b")
            print("it's "..result)
        end
    end)
    
    -- C
    async(function()
        for i=3, 1, -1 do
            result = await(sleep, 3, "c")
            print("it's "..result)
        end
    end)

    return str
end

Finally, you can call main function, and you also need to wrap your function in async.WrapAsyncFunc.

L.CallByParam(lua.P{
    Fn:      async.WrapAsyncFunc(L, L.GetGlobal("main").(*lua.LFunction)),
    NRet:    1,
    Protect: true,
}, lua.LString("hello"))

for i := 1; i <= L.GetTop(); i++ {
    log.Println(L.Get(i).String())
}

glua-async's People

Contributors

cuberl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

glua-async's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.