以下是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模式*协程的更多相关文章

  1. Lua 5.3 协程简单示例

    Lua 5.3 协程简单示例 来源 http://blog.csdn.net/vermilliontear/article/details/50547852 生产者->过滤器->消费者 模 ...

  2. lua中的协程

    lua中的协程和线程类似: 1. 协程拥有自己的独立的栈,局部变量,和指令: 2. 所有协程都可以共享全局变量: 3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程 ...

  3. [转]skynet Lua中的协程

    Lua中的协程 http://www.outsky.org/code/lua-coroutine.html Sep 6, 2014 Lua中的协程和其他变量一样,都是第一类值(first-class ...

  4. lua编程之协程介绍

    一,lua协程简介 协程(coroutine),意思就是协作的例程,最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程属于侵入式组件,线程实现的系统称之为抢占式 ...

  5. [lua] 游戏客户端逻辑使用lua协程

    我们的游戏有这样一种情景:客户端中角色需要用到一些公会的数据,但服务器不会在玩家(创角后)一进入到游戏里就推送给玩家,而是需要客户端自己在需要的时候向服务器请求公会的数据,之前的实现就是在请求消息的时 ...

  6. 高并发 Nginx+Lua OpenResty系列(11)——流量复制/AB测试/协程

    流量复制 在实际开发中经常涉及到项目的升级,而该升级不能简单的上线就完事了,需要验证该升级是否兼容老的上线,因此可能需要并行运行两个项目一段时间进行数据比对和校验,待没问题后再进行上线.这其实就需要进 ...

  7. Openresty Lua协程调度机制

    写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...

  8. Lua协程的一个例子

    很久没记录笔记了,还是养成不了记录的习惯 下面是来自 programming in lua的一个协程的例(生产者与用户的例子) 帖代码,慢慢理解 -- Programming in Lua Corou ...

  9. 写个百万级别full-stack小型协程库——原理介绍

    其实说什么百万千万级别都是虚的,下面给出实现原理和测试结果,原理很简单,我就不上图了: 原理:为了简单明了,只支持单线程,每个协程共享一个4K的空间(你可以用堆,用匿名内存映射或者直接开个数组也都是可 ...

随机推荐

  1. css3基础篇三

    CSS3 文本阴影 在 CSS3 中,text-shadow 可向文本应用阴影. 您能够规定水平阴影.垂直阴影.模糊距离,以及阴影的颜色: 实例 向标题添加阴影: h1 { text-shadow: ...

  2. 函数式编程-将Monad(单子)融入Swift

    前言 近期又开始折腾起Haskell,掉进这个深坑恐怕很难再爬上来了.在不断深入了解Haskell的各种概念以及使用它们去解决实际问题的时候,我会试想着将这些概念移植到Swift中.函数式编程范式的很 ...

  3. MySQL--增删改查分页存储过程以及事务

    添加和修改写在一起了 可以用id判断添加和修改 和事务在一起编码 可以让代码更严谨 在这里简单的说一下事务的四大特性 事务四大特性之原子性:原子性是指事务是一个不可再分割的工作单位,事务中的操作要么都 ...

  4. H5-data属性的一个问题

    关于前端存数据的问题,前面写过一个博客:前端页面存取数据 看个例子: <!DOCTYPE html> <html lang="en"> <head&g ...

  5. Javaweb 使用Servlet技术改写用户登录 使用Filter技术解决中文乱码

    先把实验3的jsp页面复制过来: WebContent->WEB-INF->lib下面的jar包8.0版本也要记得复制: Java Resources->src下的 cn.edu.h ...

  6. Django 路由视图FBV/CBV

    路由层  url路由层结构 from django.conf.urls import url from django.contrib import admin from app01 import vi ...

  7. 【ubuntu子系统】使用windows自带的ubuntu子系统

      在windows10系统中,自带了一款ubuntu子系统,就像是一个应用程序,一款软件,提供ubutnu的terminal窗口,可以使用对应的命令行模式.最重要的是,可以直接用来连接linux服务 ...

  8. Git server出现cache大回收分析

    实例 git server是一个io密集型的服务,当cache量很大的时候,cache会全部一次释放,导致那么一瞬间,IO read压力很大,因为,用户的大量请求,需要重新从磁盘读到内存,但是这个时刻 ...

  9. python-写入csv 文件

    项目要做一个导出客户信息的功能,需要写入csv: 注意文件写入的方式  例如   write open(‘w’) 从头开始写,之前写的会被替换  write open(‘a’) 则代表追加,文件指针放 ...

  10. 爬虫系列(十三) 用selenium爬取京东商品

    这篇文章,我们将通过 selenium 模拟用户使用浏览器的行为,爬取京东商品信息,还是先放上最终的效果图: 1.网页分析 (1)初步分析 原本博主打算写一个能够爬取所有商品信息的爬虫,可是在分析过程 ...