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 协程简单示例的更多相关文章

  1. lua中的协程

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

  2. Lua 5.3 迭代器的简单示例

    Lua 5.3 迭代器的简单示例 创建"closure"模式的"iterator" function allowrds() local line = io.re ...

  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. 小爬爬4.协程基本用法&&多任务异步协程爬虫示例(大数据量)

    1.测试学习 (2)单线程: from time import sleep import time def request(url): print('正在请求:',url) sleep() print ...

  6. Python 生成器和协程使用示例

    一.生成器的创建及使用 生成器比迭代器更节省内存空间,使用生成器,可以生成一个值的序列用于迭代,并且这个值的序列不是一次生成的,而是使用一个,再生成一个,的确可以使程序节省大量的内存损耗 创建生成器, ...

  7. 聊聊并发,进程通信方式,go协程简单应用场景

    开篇提问 知道并发,并行,线程,协程概念吗?或者知道大概含义吗? 有线程为什么还要有协程?区别是什么? 『进程』通信方式知道几种?有没有超过3种? golang『协程』通信方式推荐? 使用并发的目的是 ...

  8. Swoole 协程使用示例及协程优先级

    示例一: Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); Co\run(function () { go(function() { var_dump(fil ...

  9. python之进程,线程,协程简单理解

    进程:资源单位,由操作系统控制调度.正在执行的一个程序或者过程,进程之间不共享资源,进程间通讯手段:管道,队列,信号量等.多用于计算密集型场景,如金融计算 线程:是cpu的最小执行单位,由操作系统控制 ...

随机推荐

  1. Redis学习笔记之二 :在Java项目中使用Redis

    成功配置redis之后,便来学习使用redis.首先了解下redis的数据类型. Redis的数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set( ...

  2. java中判断字符串是否为数字的方法的几种方法

    1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < str.length(); i++){ ...

  3. BZOJ1854_游戏_KEY

    1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备, ...

  4. 面试题之-----String,StringBuffer,StringBuilder的区别

    String :字符串常量,值不能改变. String s="abc"; s=s+"def"; System.out.println(s); 输出结果为: ab ...

  5. 第4章 同步控制 Synchronization ----事件(Event Objects)

    Win32 中最具弹性的同步机制就属 events 对象了.Event 对象是一种核心对象,它的唯一目的就是成为激发状态或未激发状态.这两种状态全由程序来控制,不会成为 Wait...() 函数的副作 ...

  6. [VirtualBox] 1、NAT模式下端口映射

    1.VirtualBox中有4中网络连接方式 VirtualBox中有4中网络连接方式:NAT.Bridged Adapter.Internal.Host-only Adapter,VMWare中有三 ...

  7. Web服务器自定义错误页面

    在使用Web服务器运行程序的时候,难免会出现诸如 404.500 等错误,那么如何针对不同的错误代码来自定义错误页面呢? 1.找到web项目的 web.xml 文件打开,添加以下标签代码,规则是 er ...

  8. 【转】Python中实现远程调用(RPC、RMI)简单例子

    远程调用使得调用远程服务器的对象.方法的方式就和调用本地对象.方法的方式差不多,因为我们通过网络编程把这些都隐藏起来了.远程调用是分布式系统的基础. 远程调用一般分为两种,远程过程调用(RPC)和远程 ...

  9. 【转】 中兴OLT-C300常用命令

    中兴OLT C300show running-config (加载各种板卡)show gpon onu uncfg (查看OLT所有未配置的ONU)show gpon onu uncfg gpon-o ...

  10. linux RPM软件包管理

    linux RPM软件包管理 目录 1.软件包的介绍 2.rpm包管理 3.rpm包管理前端工具 1.软件包的介绍 在我们在对软件包管理前,先对软件包有个很好的了解,这样更方便我们来对其进行管理. 软 ...