[lua] future模式*协程
以下是lua实现的future模式。基于cocos客户端
local function param_pack( params, callback )
table.insert(params, callback)
return params
end local function future( ... )
local order = {result = true}
local function callback( ... )
order.res = { ... }
if order.co then
coroutine.resume(order.co)
end
end
function order:result()
if (self.res == nil) then
local co, main = coroutine.running()
if not main then self.co = co else return end
coroutine.yield()
end
return unpack(self.res)
end
local params = {...}
local host, service = params[], table.remove(params, )
if type(params[#params]) == 'function' then
params = table.remove(params)(params, callback)
else
params = param_pack(params, callback)
end
if type(host[service]) == 'function' then
host[service](unpack(params))
return order
else
print('service:'..service..' not implement at '..tostring(host))
end
end
local function asyncall( ... )
return future(...):result()
--[[
local co, main = coroutine.running()
if main then
print('Please use .call(...) in .run(func) context')
return
end
local function callback( ... )
return coroutine.resume(co, ...)
end
local params = {...}
local host, service = params[1], table.remove(params, 2)
if type(params[#params]) == 'function' then
params = table.remove(params)(params, callback)
else
params = param_pack(params, callback)
end
if type(host[service]) == 'function' then
return coroutine.yield(host[service](unpack(params)))
else
print('service:'..service..' not implement at '..tostring(host))
end
--]]
end local function http(...)
-- local order = http([method,] url [, params])
-- status, resp = order:result()
local method, url, params
if select('#', ...) < then
method, url, params = 'GET', ...
else
method, url, params = ...
end
method = string.upper(method)
local support = {GET = true, POST = true}
if not support[method] then return end local request = cc.XMLHttpRequest:new()
request:registerScriptHandler(function()
local callback = request.callback
if callback then
callback(request.status, request.responseText)
end
end)
function request:script(params, callback)
self.callback = callback
self.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON
local handler = {}
function handler.GET()
self:open(method, url..'?'..params, true)
self:send()
end
function handler.POST()
self:open(method, url, true)
self:setRequestHeader("Content-Type","application/x-www-form-urlencoded;")
self:send(params)
end
if (handler[method]) then
handler[method]()
end
end
if params~=nil then
local function url_encode(params)
if type(params) ~= 'table' then
return string.urlencode(tostring(params))
end local pp = {}
for k, v in pairs(params) do
pp[#pp+] = k..'='..string.urlencode(tostring(v))
end
return table.concat(pp, '&')
end
return future(request, 'script', url_encode(params))
end
return future(request, 'script')
end local function runProcess( ... )
local func = select(-, ...)
assert(type(func)=='function', 'the last argument must be a function for coroutine process')
local co = coroutine.create(func) local function process( ... )
coroutine.resume(co, ...)
end
process(...)
return process
end local target = {
call = asyncall,
book = future,
http = http,
run = runProcess
} return target --[[
-- example
local Plugin = plugin.AgentManager:getUserPlugin()
target.run(function ( ... )
local code, msg, info = target.call(Plugin, 'queryThirdAccountBindState', 'weixin')
if code == ThirdAccountStatus.kBinded then
code, msg, info = target.call(Plugin, 'queryThirdInfo', 'weixin')
if code == AsyncQueryStatus.kSuccess then
dump(info)
else
print(msg)
end
end
-- following using future model
local order = target.http('http://huodong.uc108.org:922/CheckIn/AddCheckIn', {ActId=‘123’, UserId=77681})---
local checkin = order
order = target.http('http://huodong.uc108.org:922/floorreward/GetFloorRewardConfig', {actid='666'})
local code, resp = order:result()
code, resp = checkin:result()
end) --]]
[lua] future模式*协程的更多相关文章
- Lua 5.3 协程简单示例
Lua 5.3 协程简单示例 来源 http://blog.csdn.net/vermilliontear/article/details/50547852 生产者->过滤器->消费者 模 ...
- lua中的协程
lua中的协程和线程类似: 1. 协程拥有自己的独立的栈,局部变量,和指令: 2. 所有协程都可以共享全局变量: 3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程 ...
- [转]skynet Lua中的协程
Lua中的协程 http://www.outsky.org/code/lua-coroutine.html Sep 6, 2014 Lua中的协程和其他变量一样,都是第一类值(first-class ...
- lua编程之协程介绍
一,lua协程简介 协程(coroutine),意思就是协作的例程,最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程属于侵入式组件,线程实现的系统称之为抢占式 ...
- [lua] 游戏客户端逻辑使用lua协程
我们的游戏有这样一种情景:客户端中角色需要用到一些公会的数据,但服务器不会在玩家(创角后)一进入到游戏里就推送给玩家,而是需要客户端自己在需要的时候向服务器请求公会的数据,之前的实现就是在请求消息的时 ...
- 高并发 Nginx+Lua OpenResty系列(11)——流量复制/AB测试/协程
流量复制 在实际开发中经常涉及到项目的升级,而该升级不能简单的上线就完事了,需要验证该升级是否兼容老的上线,因此可能需要并行运行两个项目一段时间进行数据比对和校验,待没问题后再进行上线.这其实就需要进 ...
- Openresty Lua协程调度机制
写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...
- Lua协程的一个例子
很久没记录笔记了,还是养成不了记录的习惯 下面是来自 programming in lua的一个协程的例(生产者与用户的例子) 帖代码,慢慢理解 -- Programming in Lua Corou ...
- 写个百万级别full-stack小型协程库——原理介绍
其实说什么百万千万级别都是虚的,下面给出实现原理和测试结果,原理很简单,我就不上图了: 原理:为了简单明了,只支持单线程,每个协程共享一个4K的空间(你可以用堆,用匿名内存映射或者直接开个数组也都是可 ...
随机推荐
- react性能调谐与diff算法
一个页面其实就相当于是一颗dom树,里面有很多它的子节点,然后你每次去操作一个事件,它都会生成一个虚拟dom,它会跟上一个虚拟dom进行比对,这里运用的算法叫做diff算法,当它找到需要改变的组件的时 ...
- esp32(M5STACK) ARDUINO开发环境搭建(ubuntu)
首先去官网下载arduino https://www.arduino.cc/en/main/software 由于国产链接下载慢的缘故,所以可以采用百度网盘的方式进行下载,具体下载方法 ...
- 图的遍历---DFS
类型一:邻接表 题目一:员工的重要性 题目描述 给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度 ...
- [Kernel参数]----/etc/sysctl.conf
修改sysctl方法 方法一:修改/proc下内核参数文件内容 直接修改内核参数ip_forward对应在/proc下的文件/proc/sys/net/ipv4/ip_forward.用下面命令查看i ...
- 第二次训练 密码acmore
网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26733#overview 贪心全场!!!! A题: #include <io ...
- dubbo-dubboAdmin安装(一)
简介 Dubbo是什么? dubbo是阿里开源的分布式服务治理框架,对服务的负载均衡,权重,监控,路由规则,禁用启用的管理,以及服务的自动注册和发现 分布式架构下面临问题 在分布式架构下,我们会将一个 ...
- 关于约束ENABLE NOVALIDATE的一个疑问
http://www.dbunix.com/?p=188 关于约束ENABLE NOVALIDATE的一个疑问 CREATE TABLE test (id varchar2(12), name var ...
- Maven错误:[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?的解决方法
错误: [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather tha ...
- 一次完整的http事务
一次完整的http事务 https://www.processon.com/view/link/56c6679ce4b0f0c4285e69c0 规范把 HTTP 请求分为三个部分:状态行.请求头.消 ...
- caffe中ConvolutionLayer的前向和反向传播解析及源码阅读
一.前向传播 在caffe中,卷积层做卷积的过程被转化成了由卷积核的参数组成的权重矩阵weights(简记为W)和feature map中的元素组成的输入矩阵(简记为Cin)的矩阵乘积W * Cin. ...