lua中的协程
lua中的协程和线程类似:
1. 协程拥有自己的独立的栈,局部变量,和指令;
2. 所有协程都可以共享全局变量;
3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程;
如何使用协程:
coroutine.create:创建一个协程,返回一个协程句柄;
coroutine.status:查看一个协程的状态,suspended,running,dead,normal;
coroutine.resume:恢复一个协程的执行,如果正常就返回true,错误返回false和一条错误信息;
coroutine.yield:挂起一个协程的执行;
resume-yield的数据返回:
例子:
local co2 = coroutine.create(function(a, b, c)
return coroutine.yield(a + b, b + c, c + a)
end)
print("resume", coroutine.resume(co2, 1, 2, 3)) // 打印 resume true 3 5 4 yield时,返回yield的参数
print("resume", coroutine.resume(co2, 1, 2, 3)) // 打印 resume true 1 2 3 resume时,返回resume的参数
生产者和消费者实例:
以消费者为驱动,即主循环在消费者,这里有两个消费者,同时向一个生产者获取数据。生产者每产生一个数据就会被挂起,知道下次被消费者激活。
协程程序一旦执行完成,协程状态就会被设置为dead,即死亡状态,如果试图用resume去激活一个死亡状态的协程会出现错误,可以通过判断resume的第一个返回判断协程运行是否正常。
local producer = coroutine.create(function()
local i = ; while true do
i = i + ;
--print(i) coroutine.yield(i)
end
end) function comsumer_func()
local status = data =
while true do
if (data >= ) then
break
end status, data = coroutine.resume(producer)
print("comsumer1:", data) coroutine.yield()
end
end local comsumer1 = coroutine.create(comsumer_func)
local comsumer2 = coroutine.create(comsumer_func)
local comsumers = coroutine.create(function(...)
local threads = {...}
local threads_status = {}
for i, v in ipairs(threads) do
threads_status[i] = true
end while true do
local status = false for i, v in ipairs(threads) do
if (threads_status[i]) then
threads_status[i] = coroutine.resume(threads[i])
end status = status or threads_status[i];
end if (not status) then
break
end
end
end) print(coroutine.resume(comsumers, comsumer1, comsumer2)) print(coroutine.status(producer))
print(coroutine.status(comsumer1))
print(coroutine.status(comsumer2))
排列组合迭代器实例:
function generate(t, i, n)
if (i >= n) then
coroutine.yield(t)
else
for j = i, n do
t[j], t[i] = t[i], t[j];
generate(t, i+, n);
t[j], t[i] = t[i], t[j];
end
end
end function iter(t)
local co = coroutine.create(function()
generate(t, , #t)
end) return function()
local status, data = coroutine.resume(co)
return data
end
end local t = {"a", "b", "c", "e"}
for t in iter(t) do
for j = , #t do
io.write(t[j])
end
io.write("\n")
end
多线程下载文件实例:
require "socket" function http_get_file(host, file)
local con = assert(socket.connect(host, ))
con:settimeout()
con:send("GET "..file.." HTTP/1.0\r\n\r\n") print("start to download "..file) while true do
local data, status, partial = con:receive()
io.write(data or partial) if status == "timeout" then
--print("timeout")
coroutine.yield(con)
elseif status == "closed" then
print("closed")
break
end
end con:close()
end local download_threads = {} function create_download_task(host, file)
local thread = assert(coroutine.create(function()
http_get_file(host, file)
end)) print("thread", thread) table.insert(download_threads, thread)
end function start_to_download()
local i =
while true do
if (#download_threads == ) then break end if (download_threads[i]) then
local status, con = coroutine.resume(download_threads[i]) if not con then
table.remove(download_threads, i)
i = i -
end
end i = (i + )
if i > #download_threads then
i =
end
end
end create_download_task("news.zol.com.cn", "/591/5916908.html")
create_download_task("www.baidu.com", "/") start_to_download()
lua中的协程的更多相关文章
- [转]skynet Lua中的协程
Lua中的协程 http://www.outsky.org/code/lua-coroutine.html Sep 6, 2014 Lua中的协程和其他变量一样,都是第一类值(first-class ...
- Unity中的协程(一)
这篇文章很不错的问题,推荐阅读英文原版: Introduction to Coroutines Scripting with Coroutines 这篇文章转自:http://blog.csdn. ...
- 深入tornado中的协程
tornado使用了单进程(当然也可以多进程) + 协程 + I/O多路复用的机制,解决了C10K中因为过多的线程(进程)的上下文切换 而导致的cpu资源的浪费. tornado中的I/O多路复用前面 ...
- Lua 5.3 协程简单示例
Lua 5.3 协程简单示例 来源 http://blog.csdn.net/vermilliontear/article/details/50547852 生产者->过滤器->消费者 模 ...
- python中的协程及实现
1.协程的概念: 协程是一种用户态的轻量级线程.协程拥有自己的寄存器上下文和栈. 协程调度切换时,将寄存器上下文和栈保存到其他地方,在切换回来的时候,恢复先前保存的寄存器上下文和栈. 因此,协程能保留 ...
- fasthttp中的协程池实现
fasthttp中的协程池实现 协程池可以控制并行度,复用协程.fasthttp 比 net/http 效率高很多倍的重要原因,就是利用了协程池.实现并不复杂,我们可以参考他的设计,写出高性能的应用. ...
- Golang 入门系列(六)理解Go中的协程(Goroutine)
前面讲的都是一些Go 语言的基础知识,感兴趣的朋友可以先看看之前的文章.https://www.cnblogs.com/zhangweizhong/category/1275863.html. 今天就 ...
- python中的协程:greenlet和gevent
python中的协程:greenlet和gevent 协程是一中多任务实现方式,它不需要多个进程或线程就可以实现多任务. 1.通过yield实现协程: 代码: import time def A(): ...
- xlua 实现协程替换Unity中的协程
C#中的协程: IEnumerator ShowSpiritInfo() { UIMessageMgr.ShowMsgWait(true); DestroyUIModelInfo(); bool is ...
随机推荐
- 使用函数库(JAVA API)
/*使用函数库(JAVA API) * 在JAVA的API里类被封装在一个个的package,要使用package的类之前必须 * 要知道这个类属于哪个package * 引用类方式: * 1.通过i ...
- iOS 根据UIImage 修改UIImageView Frame (包括截取图片中间部分)
iOS UIImageView 根据需求调整frame 1.图片的宽和高不相等,截取图片的中间部分,截取的部分Size明确 2.图片的宽度要等于其父视图的类的宽度,然后根据宽度计算高度,保证 图片不变 ...
- 深入了解Hibernate的缓存使用
Hibernate缓存 缓存是计算机领域的概念,它介于应用程序和永久性数据存储源(如在硬盘上的文件或者数据库)之间,其作用是降低应用程序 直接读写永久性数据存储源的频率,从而提高应用的运行性能.缓存中 ...
- Oracle数据库初级学习 2
今天我们介绍Oracle数据库中剩余的查询方法,今天的查询方法会比昨天的更为复杂一些(PS:我也是个初学者,请见谅..). 一.分组函数 分组函数是为了区分同一个表中的不同数据而建立,其关键字为GRO ...
- 《JavaScript模式》第4章 函数
@by Ruth92(转载请注明出处) 第4章:函数 一.JavaScript 中函数的两个重要特征 函数是第一类对象,可以作为带有属性和方法的值以及参数进行传递: 函数提供了局部作用域,而其他大括号 ...
- oop、configparser、xml模块
本节大纲:一:在python中,有两种编程思想.1:函数式编程.2:oop.无论是函数式编程还是oop都是相辅相成.并不是说oop比函数式编程就好.各有各的优缺点.在其他语言中java等只能以面向对象 ...
- CentOS 7 为firewalld添加开放端口及相关资料
1.运行.停止.禁用firewalld 启动:# systemctl start firewalld 查看状态:# systemctl status firewalld 或者 firewall-cm ...
- linux常见目录的作用
根目录下的bin 和sbin,usr目录下的bin和sbin四个都是保存系统命令.bin下的普通用户都可以使用,sbin下只有超级用户root可以使用 pro和syc不能直接操作,是内存的过载点 / ...
- 億万笑者 - Radwimps
億万笑者 作曲:野田洋次郎 作词:野田洋次郎 明日に希望を持った者だけに 絶望があるんだ何かを信じた者だけに 裏切りはあるんだ勇者だけに与えられた 名誉の負傷とでも言うのかそれにしてはずいぶんと 割に ...
- Jquery 中的$(this) 和javascript中的this
this 是 JavaScript 中的关键字. $(this) 可以认为是用 jQuery 包装过 JavaScript 中的 this,包装后 $(this) 就会继承 jQuery 的方法. 本 ...