写了一个(不完整的)基于协程的task调度库

sample code如下

my_spawn(
function ()
print('f: 1') local t1 = my_spawn(
function ()
print('f: 3')
task_yield_to_be_schedule()
print('f: 4')
end
) --task_yield_to_be_schedule()
my_wait_task(t1)
print('f: 2')
end
) local num_run =
while my_run_once() do
dbgprint('>>exec ', num_run)
num_run = num_run +
end

features

  • 支持spwan
  • 支持在task里面spawn
  • 支持task里面yield
  • 支持task里面等待其他task

todo

  • 支持在task里面sleep
  • 支持在task里面设置和等待event

完整源代码如下

require('mobdebug').coro()
local inspect = require('inspect') local debug_on = true local function dbgprint(...)
if debug_on then
print(...)
end
end if false then
function f()
print('in f')
coroutine.yield()
print('in f2') end local task1 = coroutine.create(f)
print(coroutine.status(task1))
local ret, r2, r3 = coroutine.resume(task1)
print(ret)
print(coroutine.status(task1))
print(coroutine.resume(task1))
print(coroutine.resume(task1))
print(coroutine.status(task1)) return
end -- scheduler
-- lowlevel support: spawn, wait, events and timeout -- to be run tasks
local tasks_to_be_scheduled = {} -- to be timeout tasks
local tasks_to_be_timeout = {} -- to be set event tasks
local tasks_to_be_event = {} -- to be waited tasks, this is a map where key is task handle, value is the tasks waiting this task
local tasks_to_be_wait = {} -- task yield flags
local yield_flag_to_be_schedule =
local yield_flag_timeout =
local yield_flag_event =
local yield_flag_wait_task = function my_spawn(f)
local t = coroutine.create(f)
table.insert(tasks_to_be_scheduled, t)
return t
end function my_run_once() -- loop to be scueduled tasks
if #tasks_to_be_scheduled > then -- fetch a task
local t = table.remove(tasks_to_be_scheduled, )
assert(coroutine.status(t)=="suspended") -- exec
local ret, data1, data2 = coroutine.resume(t)
dbgprint ('>>resume coroutine returns ', ret, inspect(data1), data2)
assert(ret) -- handle following
if coroutine.status(t) ~= 'dead' then
assert(data1 and data1.yield_flag)
local dispatch={}
dispatch[yield_flag_to_be_schedule] = function ()
table.insert(tasks_to_be_scheduled, t)
end dispatch[yield_flag_wait_task] = function ()
end local disp = dispatch[data1.yield_flag]
if disp then
disp()
else
assert()
end
else
-- loop to see who are waiting this task?
local tasks_to_be_schedule = tasks_to_be_wait[t]
if tasks_to_be_schedule then
dbgprint ('>>trigger depent tasks ', inspect(tasks_to_be_schedule))
for i, task_to_be_schedule in ipairs(tasks_to_be_schedule) do
table.insert(tasks_to_be_scheduled, task_to_be_schedule)
end
tasks_to_be_wait[t] = nil
end end return true
end return false
end function task_yield_to_be_schedule()
coroutine.yield ({yield_flag=yield_flag_to_be_schedule})
end function my_wait_task(t)
-- when t done, need notify this_task
local this_task = coroutine.running()
if not tasks_to_be_wait[this_task] then
tasks_to_be_wait[t] = {this_task}
else
table.insert(tasks_to_be_wait[t], this_task)
end -- yield from current task
coroutine.yield ({yield_flag=yield_flag_wait_task, handle=t})
end my_spawn(
function ()
print('f: 1') local t1 = my_spawn(
function ()
print('f: 3')
task_yield_to_be_schedule()
print('f: 4')
end
) --task_yield_to_be_schedule()
my_wait_task(t1)
print('f: 2')
end
) local num_run =
while my_run_once() do
dbgprint('>>exec ', num_run)
num_run = num_run +
end

lua:写了个基于协程的task调度库的更多相关文章

  1. 基于协程的Python网络库gevent

    import gevent def test1(): print 12 gevent.sleep(0) print 34 def test2(): print 56 gevent.sleep(0) p ...

  2. Python实现基于协程的异步爬虫

    一.课程介绍 1. 课程来源 本课程核心部分来自<500 lines or less>项目,作者是来自 MongoDB 的工程师 A. Jesse Jiryu Davis 与 Python ...

  3. 发布一个基于协程和事件循环的c++网络库

    目录 介绍 使用 性能 实现 日志库 协程 协程调度 定时器 Hook RPC实现 项目地址:https://github.com/gatsbyd/melon 介绍 开发服务端程序的一个基本任务是处理 ...

  4. python基于协程的网络库gevent、eventlet

    python网络库也有了基于协程的实现,比较著名的是 gevent.eventlet 它两之间的关系可以参照 Comparing gevent to eventlet, 本文主要简单介绍一下event ...

  5. 并发编程(六)——进程/线程池、协程、gevent第三方库

    进程/线程池.协程.gevent第三方库 一.进程/线程池 1.进程池 (1)什么是进程池 如果需要创建的子进程数量不大,可以直接利用multiprocess中的Process来创建.但是当需要创建上 ...

  6. Lua基础之coroutine(协程)

    概括:1.创建协程2.coroutine的函数3.coroutine的基本流程4.yield对coroutine流程的干预5.resume, function()以及yield之间的参数传递和返回值传 ...

  7. lua学习笔记13:协程具体解释和举例

    一.coroutine.create创建协程 參数是协程的主函数,返回一个thread对象 co = coroutine.create(function() print("coroutine ...

  8. 在PHP中使用协程实现多任务调度

    PHP5.5一个比较好的新功能是加入了对迭代生成器和协程的支持.对于生成器,PHP的文档和各种其他的博客文章已经有了非常详细的讲解.协程相对受到的关注就少了,因为协程虽然有很强大的功能但相对比较复杂, ...

  9. python --- 协程编程(第三方库gevent的使用)

    1. 什么是协程? 协程(coroutine),又称微线程.协程不是线程也不是进程,它的上下文关系切换不是由CPU控制,一个协程由当前任务切换到其他任务由当前任务来控制.一个线程可以包含多个协程,对于 ...

随机推荐

  1. 使用Cloudera Manager搭建HDFS完全分布式集群

    使用Cloudera Manager搭建HDFS完全分布式集群 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于Cloudera Manager的搭建我这里就不再赘述了,可以参考 ...

  2. CDH构建大数据平台-使用自建的镜像地址安装Cloudera Manager

    CDH构建大数据平台-使用自建的镜像地址安装Cloudera Manager 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.搭建CM私有仓库 详情请参考我的笔记: http ...

  3. Scrum会议博客以及测试报告

    3组Alpha冲刺阶段博客目录 一.Scrum Meeting1. 第六周会议记录(链接地址:https://www.cnblogs.com/Cherrison-Time/articles/11788 ...

  4. Python正则提取数据单引号内数据,并判断是否是空列表(是否提取到数据)

    #coding=utf- import re string1="asdfgh'355'dfsfas" string2="fafafasfasdfasdf" pa ...

  5. scala 基础知识 FAQ

    问题1: 抽象成员初始化规则 ① 父类先初始化 ② 在初始化的过程中,如果 val 发生重写,只有最后一个重写生效.前面的会变成零值,后面的会直接继承. 参考资料:https://docs.scala ...

  6. 题解 UVa11388

    题目大意 \(T\) 组数据,每组数据给定两个整数 \(G,L\),输出数对 \(x,y\) 满足 \(GCD(x,y)=G,LCM(x,y)=L\) 且 \(x\) 最小.若无解则输出 \(-1\) ...

  7. Spark运行架构及作业提交流程

    1.yarn-cluster模式: (1)client客户端提交spark Application应用程序到yarn集群. (2)ResourceManager收到了请求后,在集群中选择一个NodeM ...

  8. js中检测类型问题

    <script>    // var str = '少壮不努力,老大徒伤悲';    // console.log(str instanceof String);    // consol ...

  9. win10 安装Borland C++Builder 6后编译运行出

    win10系统安装bcb后打开bcb后显示 Unable to rename ‘c:\Program Files(x86)\Borland\CBuilder6\Bin\bcb.$$$'to‘cc:\P ...

  10. 「LibreOJ NOI Round #2」单枪匹马

    嘟嘟嘟 这题没卡带一个\(log\)的,那么就很水了. 然后我因为好长时间没写矩阵优化dp,就只敲了一个暴力分--看来复习还是很关键的啊. 这个函数显然是从后往前递推的,那么令第\(i\)位的分子分母 ...