【问题标题】:Lua socket asynchronous callsLua 套接字异步调用
【发布时间】:2011-08-13 07:37:25
【问题描述】:

我正在编写一个使用 Lua 套接字与 http 服务器通信的程序。 我使用的 API 是“socket.http.request”,我发现它是同步的。我的理解是它会等到它得到一些响应或超时。 我的理解正确吗?如果是这样,我更喜欢使用异步 API。

我还发现了另一个API“socket.http.request_cb”,它在处理请求时调用了一个回调函数。但是,它似乎在这里不起作用。 (此 API 在我使用的版本上不可用。)我在这里使用 Lua 5.1 和 Lua socket 2.0.2。谁能告诉我哪个版本的 Lua 或 Lua 套接字有这个 API?

【问题讨论】:

标签: lua luasocket


【解决方案1】:

使用 connection:settimeout() 您可以设置连接超时。这在 Lua Socket 的并行下载器示例中使用:

function download (host, file, port)
    port = port or 80
    print (host, file, port)    
    local connectStatus, myConnection = pcall (socket.connect,host,port)
    if (connectStatus) then
        myConnection:settimeout(0.01) -- do not block you can play with this value
        local count = 0 -- counts number of bytes read
        -- May be easier to do this LuaSocket's HTTP functions
        myConnection:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
        local lastStatus = nil
        while true do
            local buffer, status, overflow = receive(myConnection, lastStatus)
            -- If buffer is not null the call was a success (changed in LuaSocket 2.0)
            if (buffer ~= nil) then
                 io.write("+")
                 io.flush()
                 count = count + string.len(buffer)
            else
                print ("\n\"" .. status .. "\" with " .. string.len(overflow) .. " bytes of " .. file)
                io.flush()
                count = count + string.len(overflow)
            end
            if status == "closed" then break end
                lastStatus=status
            end
        myConnection:close()
        print(file, count)
    else
        print("Connection failed with error : " .. myConnection)
        io.flush()
    end
end

threads = {} -- list of all live threads

function get (host, file, port)
    -- create coroutine
    local co = coroutine.create(
        function ()
            download(host, file, port)
        end)
    -- insert it in the 
    table.insert(threads, co)
end

function receive (myConnection, status)
    if status == "timeout" then
        print (myConnection, "Yielding to dispatcher")
        io.flush()
        coroutine.yield(myConnection)
    end
    return myConnection:receive(1024)
end

function dispatcher ()
    while true do
        local n = table.getn(threads)
        if n == 0 then break end -- no more threads to run
        local connections = {}
        for i=1,n do
            print (threads[i], "Resuming")
            io.flush()
            local status, res = coroutine.resume(threads[i])
            if not res then -- thread finished its task?
                table.remove(threads, i)
                break
            else -- timeout
                table.insert(connections, res)
            end
        end
        if table.getn(connections) == n then
            socket.select(connections)
        end
    end
end

host = "www.w3.org"
get(host, "/TR/html401/html40.txt")
get(host,"/TR/2002/REC-xhtml1-20020801/xhtml1.pdf")
get(host,"/TR/REC-html32.html")
get(host,"/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt")
dispatcher()

【讨论】:

    【解决方案2】:

    我正在使用lua-ev 进行所有 IO 多路复用工作。它是一种类似于 node.js 背后的事件循环实现。一个线程,没有种族。

    【讨论】:

      【解决方案3】:

      您可能会在luaThread 中找到一些灵感。它的一个演示是异步wget

      最近开发的线程库lua-llthreads支持the ZMQ "socket library that acts as a concurrency framework"lua-zmq

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-25
        • 1970-01-01
        相关资源
        最近更新 更多