写了一个(不完整的)基于协程的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. 191016 Linux中安装python3

    注意事项:直接在Linux系统中安装python3后会导致yum命令和pip命令失效. 安装python3过程(按下述方法安装依赖包.指定软链接,就不会出错了): # 安装依赖包 yum instal ...

  2. 2013.5.4 - KDD第十六天

    昨天下午的时候中秋给我发短信"待会儿上课吧?上课讨论下?",然后我回复"嗯,好的."然后上课的时候中秋说那个方案也许不太好执行,因为他后来看数据了,数据库里面这种"可以从从协同作者进行判断"的例子并不 ...

  3. 用js刷剑指offer(数组中的逆序对)

    题目描述 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P ...

  4. 神经网络学习中的损失函数及mini-batch学习

    # 损失函数(loss function).这个损失函数可以使用任意函数,# 但一般用均方误差(mean squared error)和交叉熵误差(cross entropy error)等一切都在代 ...

  5. SpringBoot -生成Entity和Dto互转的双向枚举类 -使用注解@Mapper(componentModel = "spring")

    1.导入pom文件 ,版本号自定 <!--mapStruct依赖--> <dependency> <groupId>org.mapstruct</groupI ...

  6. 阿里云 Windows Server 2012 密码过期设置

    不加入域的情况下: 1.服务器管理器>工具>本地安全策略>账户策略>密码策略>密码最长使用期限(修改为0天)或者禁用密码复杂度要求 参考:https://blog.csd ...

  7. CheckList 如何梳理可减少上线的验证时间(总结篇)

    对CheckList的执行发起的思考? (1)功能越来越多,CheckList越补充越多,执行CheckList时间越来越长,如何减少上线的验证时间?(2)减少上线验证的时间外,如何保证质量?上线后少 ...

  8. window10 安装mysql5.6版本

    说明:因为之前都是安装版的,我用的是5.0,版本低不能够支持现在的业务,所以升级.之前的就卸载了!!(废话太多) 下载地址:https://dev.mysql.com/downloads/file/? ...

  9. LightOJ - 1259 - Goldbach`s Conjecture(整数分解定理)

    链接: https://vjudge.net/problem/LightOJ-1259 题意: Goldbach's conjecture is one of the oldest unsolved ...

  10. Centos 下 Apache 原生 Hbase + Phoenix 集群安装(转载)

    前置条件 各软件版本:hadoop-2.7.7.hbase-2.1.5 .jdk1.8.0_211.zookeeper-3.4.10.apache-phoenix-5.0.0-HBase-2.0-bi ...