lua:写了个基于协程的task调度库
写了一个(不完整的)基于协程的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调度库的更多相关文章
- 基于协程的Python网络库gevent
import gevent def test1(): print 12 gevent.sleep(0) print 34 def test2(): print 56 gevent.sleep(0) p ...
- Python实现基于协程的异步爬虫
一.课程介绍 1. 课程来源 本课程核心部分来自<500 lines or less>项目,作者是来自 MongoDB 的工程师 A. Jesse Jiryu Davis 与 Python ...
- 发布一个基于协程和事件循环的c++网络库
目录 介绍 使用 性能 实现 日志库 协程 协程调度 定时器 Hook RPC实现 项目地址:https://github.com/gatsbyd/melon 介绍 开发服务端程序的一个基本任务是处理 ...
- python基于协程的网络库gevent、eventlet
python网络库也有了基于协程的实现,比较著名的是 gevent.eventlet 它两之间的关系可以参照 Comparing gevent to eventlet, 本文主要简单介绍一下event ...
- 并发编程(六)——进程/线程池、协程、gevent第三方库
进程/线程池.协程.gevent第三方库 一.进程/线程池 1.进程池 (1)什么是进程池 如果需要创建的子进程数量不大,可以直接利用multiprocess中的Process来创建.但是当需要创建上 ...
- Lua基础之coroutine(协程)
概括:1.创建协程2.coroutine的函数3.coroutine的基本流程4.yield对coroutine流程的干预5.resume, function()以及yield之间的参数传递和返回值传 ...
- lua学习笔记13:协程具体解释和举例
一.coroutine.create创建协程 參数是协程的主函数,返回一个thread对象 co = coroutine.create(function() print("coroutine ...
- 在PHP中使用协程实现多任务调度
PHP5.5一个比较好的新功能是加入了对迭代生成器和协程的支持.对于生成器,PHP的文档和各种其他的博客文章已经有了非常详细的讲解.协程相对受到的关注就少了,因为协程虽然有很强大的功能但相对比较复杂, ...
- python --- 协程编程(第三方库gevent的使用)
1. 什么是协程? 协程(coroutine),又称微线程.协程不是线程也不是进程,它的上下文关系切换不是由CPU控制,一个协程由当前任务切换到其他任务由当前任务来控制.一个线程可以包含多个协程,对于 ...
随机推荐
- js对属性的操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 剑指Offer(二十四):二叉树中和为某一值的路径
剑指Offer(二十四):二叉树中和为某一值的路径 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- Vue移动组件库Mint UI的安装与使用
一.什么是 Mint UI 1.Mint UI 包含丰富的 CSS 和 JS 组件,可以提升移动端开发效率 2.Mint UI 按需加载组件 3.Mint UI 轻量化 二.Mint UI 的安装 1 ...
- MySQL 索引原理及慢查询优化
MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会从职位 ...
- 接口中的JSON报文
原始报文: 请求报文: [ { "appNo" : "", "applicantCredentialCode" : "" ...
- Django --- cookie与session,中间件
目录 1.cookie与session 1.cookie 2.session 2.中间件 1.中间件作用 2.用户可以自定义的五个方法 3.自定义中间件 1.cookie与session 1.cook ...
- 棋盘覆盖 状压DP+矩阵快速幂
题意:有一个m 行n 列的矩形方格棋盘,1 < = m< = 5,1=< n< =10^9,用1*2 的骨牌(可横放或竖放)完全覆盖,骨牌不能重叠,有多少种不同的覆盖的方法.你 ...
- Dobbox
一.向本地仓库导入Dubbox依赖 1.1解压压缩包 1.2打开cmd窗口切到源码包路径 1.3输入命令行 1.4成功后展示如图 1.5输入命令行 1.6成功后如图 public class DoSo ...
- Linux sudo权限绕过(CVE-2019-14287)
2019年10月14日,Sudo官方发布了Sudo 1.8.28版本,其中包含sudo root权限绕过漏洞的补丁修复. 此漏洞编号是CVE-2019-14287,当sudo配置为允许用户以任意方式运 ...
- 1.6synchronized代码块
1.synchronized可以使用任意的Object进行加锁,用法比较灵活 ============================================================= ...