Lua 5.3 协程简单示例
Lua 5.3 协程简单示例
来源 http://blog.csdn.net/vermilliontear/article/details/50547852
生产者->过滤器->消费者 模式的协程示例
function receive(prod)
local status, value = coroutine.resume(prod)
return value
end function send(x)
coroutine.yield(x)
end function producer()
return coroutine.create(function ()
while true do
local x = io.read()
send(x)
end
end)
end function filter(prod)
return coroutine.create(function ()
local line =
while true do
local x = receive(prod)
x = string.format("%5d %s", line, x)
send(x)
line = line +
end
end)
end function consumer(prod)
while true do
local x = receive(prod)
io.write(x, "\n")
end
end --[[ "producer()" 创建了一个"coroutine", 由filter掌控;
"filter()" 创建了一个"coroutine", 由consumer掌控. --]]
consumer(filter(producer()))
运行截图现象

coroutine.wrap 与 coroutine.create 的区别
-- coroutine.wrap 返回函数
co1 = coroutine.wrap(function (a)
local c = coroutine.yield(a+)
print("wrap yield before a and c: ", a, c)
return * a
end) b = co1()
print(b)
d = co1(+)
print(d) print("----------------") -- coroutine.create 返回协程本身
co2 = coroutine.create(function (a)
local c = coroutine.yield(a+)
print("wrap yield before a and c: ", a, c)
return * a
end) k, v = coroutine.resume(co2, )
print(k, v)
k, v = coroutine.resume(co2, +)
print(k, v)
运行现象:

使用”coroutines”实现了简单的抢占式线程
threads = {}
time = os.clock()
limit_time = 0.111
function cal(from, to)
local sum = ;
for i = from, to, do
sum = sum + i
if(os.clock() - time) >= limit_time then
print(string.format("Worker %d calculating, limit_time(%f), time(%f), %f%%.",
worker, limit_time, time, (i / to * )))
time = os.clock()
coroutine.yield()
end
end
end
function job(from, to)
local co = coroutine.create(function ()
cal(from, to)
end)
table.insert(threads, co)
end
job(, )
job(, )
job(, )
job(, )
while true do
local n = #threads
if n == then
break
end
for i = , n do
worker = i -- 全局变量
local status = coroutine.resume(threads[i])
if not status then
table.remove(threads, i)
break
end
end
end
运行现象:

Lua 5.3 协程简单示例的更多相关文章
- lua中的协程
lua中的协程和线程类似: 1. 协程拥有自己的独立的栈,局部变量,和指令: 2. 所有协程都可以共享全局变量: 3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程 ...
- Lua 5.3 迭代器的简单示例
Lua 5.3 迭代器的简单示例 创建"closure"模式的"iterator" function allowrds() local line = io.re ...
- [转]skynet Lua中的协程
Lua中的协程 http://www.outsky.org/code/lua-coroutine.html Sep 6, 2014 Lua中的协程和其他变量一样,都是第一类值(first-class ...
- lua编程之协程介绍
一,lua协程简介 协程(coroutine),意思就是协作的例程,最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程属于侵入式组件,线程实现的系统称之为抢占式 ...
- 小爬爬4.协程基本用法&&多任务异步协程爬虫示例(大数据量)
1.测试学习 (2)单线程: from time import sleep import time def request(url): print('正在请求:',url) sleep() print ...
- Python 生成器和协程使用示例
一.生成器的创建及使用 生成器比迭代器更节省内存空间,使用生成器,可以生成一个值的序列用于迭代,并且这个值的序列不是一次生成的,而是使用一个,再生成一个,的确可以使程序节省大量的内存损耗 创建生成器, ...
- 聊聊并发,进程通信方式,go协程简单应用场景
开篇提问 知道并发,并行,线程,协程概念吗?或者知道大概含义吗? 有线程为什么还要有协程?区别是什么? 『进程』通信方式知道几种?有没有超过3种? golang『协程』通信方式推荐? 使用并发的目的是 ...
- Swoole 协程使用示例及协程优先级
示例一: Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); Co\run(function () { go(function() { var_dump(fil ...
- python之进程,线程,协程简单理解
进程:资源单位,由操作系统控制调度.正在执行的一个程序或者过程,进程之间不共享资源,进程间通讯手段:管道,队列,信号量等.多用于计算密集型场景,如金融计算 线程:是cpu的最小执行单位,由操作系统控制 ...
随机推荐
- TCP/IP(四)网络层
前言 前面给大家介绍了计算机网络的基本概述,物理层和数据链路层.这一篇给大家介绍面试中经常会被问到的网络层.在介绍之前我们回顾一下之前学习的知识! CP/IP协议栈:物理层.链路层.网络层.传输层.应 ...
- Html语言,使用<a>标签发送电子邮件
<a>标签有一个功能是可以链接Email地址,使用mailto能让访问者便捷向网站管理者发送电子邮件. 使用mailto 属性时请参考下表: 如果mailto里同时有多个参数,第一个参数必 ...
- mint-ui vue双向绑定
由于最近项目需求,用上了mint-ui来重构移动端页面,从框架本身来讲我觉得很强大了,用起来也很不错,但是文档就真的是,,,,让我无言以对,给的api对于我们这些小菜鸟来讲真的是处处是坑呀(ps:用v ...
- Integer的自动拆箱
public class Test2{ public static void main(String[] args){ Integer a=1; Integer b=2; Integer c=3; I ...
- MySQL数据备份之mysqldump使用(转)
mysqldump常用于MySQL数据库逻辑备份. 1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump ...
- Android 从ImageView中获取Bitmap对象方法
showImageView.setDrawingCacheEnabled(true); Bitmap bitmap=showImageView.getDrawingCache(); showImage ...
- Linux系统centOS7在虚拟机下的安装及XShell软件的配置
前面的话 本文将详细介绍Linux系统centOS7在虚拟机下的安装 准备工作 [系统下载] 在安装centOS7之前,首先在官网下载合适的版本 然后,选择一个链接下载即可 [虚拟机配置] 接下来,需 ...
- 550 Create directory operation failed
往Linux系统中上传文件时候,我们经常会使用FTP连接Linux,也经常会使用mkdir命令来创建目录.最近发现用mkdir创建目录时提示550 Create directory operation ...
- Window window = Window.GetWindow(控件)
Window window = Window.GetWindow(控件)
- http中错误代码的含义整理
HTTP网页错误代码大全带解释 HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁 ...