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控制,一个协程由当前任务切换到其他任务由当前任务来控制.一个线程可以包含多个协程,对于 ...
随机推荐
- zabbix监控内存
取内存百分比 取出内存的可用的MB大小 / 总的内存大小 = 实际可用的百分比 avilable 710 X 100 / total 974 free -m|awk '/^Mem/{print $NF ...
- CentOS7怎样安装Jenkins
参考 http://pkg.jenkins-ci.org/redhat/ wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org ...
- 题解 洛谷P4779 【【模板】单源最短路径(标准版)】
正权图,貌似看来是一道裸的 \(dijkstra\) \(dijkstra\)的主要步骤: 首先,在\(dijkstra\)中,源点表示一开始的出发点,蓝点表示还未确定的点,白点则表示已经确定的点. ...
- java中List与数组的转换
1.数组转换成List public static <T> List<T> asList(T... a) String[] arr = new String[] {" ...
- js 全选反选
<th><input type="checkbox" id="checkall" name="checkall" oncl ...
- Django --- csrf相关,auth相关
目录 1.csrf相关 1.跨站请求伪造 2.跨站请求伪造问题解决 3.crsf中间件 4.csrf装饰FBV的装饰器 5.csrf装饰CBV的装饰器 6.django settings源码刨析 2. ...
- set的完整用法
#include<bits/stdc++.h> using namespace std; set<int>s; int main () { //begin()--返回指向第一个 ...
- if语句:求x的绝对值
#include<stdio.h>void main(){ int x,y; scanf("%d", &x); printf("x=%d\n" ...
- [React] Write a Custom React Effect Hook
Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, whi ...
- P4357 [CQOI2016]K远点对
题意:给定平面中的 \(n\) 个点,求第 \(K\) 远的点对之间的距离,\(n\leq 1e5,K\leq min(100,\frac{n\times (n-1)}{2})\) 题解:kd-tre ...