写了一个(不完整的)基于协程的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. python获取第前多少天的日期

    1. 显示昨天(前一天)的日期 from datetime import date, timedelta yesterday_date = (date.today() + timedelta(days ...

  2. CentOS7安装Redis单实例

    由于环境差异,安装过程可能遇到各种各样的问题,不要慌,根据错误提示解决即可. 1.下载redis下载地址在:redis.io比如把Redis安装到/usr/local/soft/ cd /usr/lo ...

  3. js HTTP 下载 处理 api 请求 返回数据流

    axios({ method: 'post', url: 'url....', data:{}, timeout: 1000*60, responseType: 'blob',// 此选项必须设置 否 ...

  4. Linux postfix配置方法

    第七题 配置邮件服务器 postfix学习网站:https://blog.csdn.net/mycms5/article/details/78773308  system1和systemc2分别执行 ...

  5. 结构型模式(六) 享元模式(Flyweight)

    一.动机(Motivate) 在软件系统中,采用纯粹对象方案的问题在于大量细粒度的对象会很快充斥在系统中,从而带来很高的运行时代价--主要指内存需求方面的代价.如何在避免大量细粒度对象问题的同时,让外 ...

  6. Java线程调度方式

    在Java多线程环境中,为保证所有线程的执行能按照一定的规则执行,JVM实现了一个线程调度器,它定义了线程调度的策略,对于CPU运算的分配都进行了规定,按照这些特定的机制为多个线程分配CPU的使用权. ...

  7. Mac上django 报错 [Errno 13] Permission denied: '/static'

    将setting文件中的 改成:

  8. Idea导入maven项目

    1.idea中有项目的关闭项目  File>>close project  回到截图 下一步>下一步

  9. [React] Write a Custom State Hook in React

    Writing your own custom State Hook is not as a daunting as you think. To keep things simple, we'll r ...

  10. 微信网站防屏蔽防红的措施以及微信域名检测API等工具的技术原理

    为什么关心这种技术?因为我经常听到身边搞微商.搞微信项目的朋友都在叫苦连天,由于微信域名屏蔽.微信域名被拦截.弄得他们尸横遍野,损失的连过年回家的路费都没了,曾经的叱咤风云一下变成了今日的倒亏损.腾讯 ...