Lua socket 异步调用。
2014-6-24 23:32:44
收藏:0
阅读:196
评论:3
我正在编写一个使用Lua socket与http服务器通信的程序。 我使用的API是"socket.http.request",我发现它是同步的。我的理解是它会等待直到收到响应或超时。 我的理解是否正确?如果是,我更喜欢使用异步API。
我还发现另一个API"socket.http.request_cb",它在处理请求时调用回调函数。但它在这里似乎无效。(我使用的版本中没有这个API。)我这里使用的是Lua 5.1和Lua socket 2.0.2。有人能告诉我哪个版本的Lua或Lua socket有这个API吗?
原文链接 https://stackoverflow.com/questions/5795419
点赞
stackoverflow用户33252
你可能会在luaThread中找到灵感。其中之一的演示是一个异步的wget
。
最近开发的线程库lua-llthreads支持ZMQ“作为并发框架的套接字库”与lua-zmq。
2011-04-28 15:00:43
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
使用
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) -- 不阻塞,可以尝试不同的值 local count = 0 -- 统计已读取的字节数 -- 或者使用LuaSocket的HTTP函数更容易 myConnection:send("GET " .. file .. " HTTP/1.0\r\n\r\n") local lastStatus = nil while true do local buffer, status, overflow = receive(myConnection, lastStatus) -- 如果buffer不为nil,则调用成功(在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 = {} -- 所有线程的列表 function get (host, file, port) -- 创建协程 local co = coroutine.create( function () download(host, file, port) end) -- 将协程插入列表中 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 -- 没有线程需要运行 local connections = {} for i=1,n do print (threads[i], "Resuming") io.flush() local status, res = coroutine.resume(threads[i]) if not res then -- 线程已经完成了任务? table.remove(threads, i) break else -- 超时 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()