sniperHW / chuck

high performance and easily use asynchronous network library for C/Lua
52 stars 32 forks source link

sched.lua new_task()和run问题 #23

Open sandy1219 opened 7 years ago

sandy1219 commented 7 years ago

local emitter_task = new_task(function() ...... end),不用调用emitter_task:run() 或者 sched.run(emitter_task ) 定时器就已经开始执行了,在new_task中 new_tasks[taskd] = true 这一步已经做了

run这个api 是不是应该是这样的 M.run = function ( task, ... ) local taskd if type(task)=='function' then taskd = new_task( task ) else taskd = task end new_tasks[taskd] = true; return taskd end 把new_tasks[taskd] = true 这行代码 从new_task中移除 加入到 run中 是不是更合适?

sandy1219 commented 7 years ago

如果不这么改,chuck/minilumen/test/test-pause.lua 例子中 emitter_task 先于下边的sched运行,emitter_task第一次发出的 1 下边的sched是收不到的,这应该不是作者的意思吧

sniperHW commented 7 years ago
M.run = function ( task, ... )
    local taskd
    if type(task)=='function' then
        taskd = new_task( task )
    else
        taskd = task
    end
    --M.set_pause(taskd, false)
    --step_task(taskd, ...)  --FIXME can get the task killed: still in new_tasks
    return taskd
end

run并不会执行一个task,task是有sched.loop驱动执行的。 emitter_task:run()这行代码没有任何实际意义

要想第一个1不丢失,

sched.run(function()
    print("unkonw task")
    local waitd={timeout=5, 'ev'}
    while true do
        print("before wait")
        local ev, s = sched.wait(waitd)
        if ev then
            print(s)
        else
            print ('wakeup!')
            emitter_task:set_pause(false)
        end
    end
end)

emitter_task=sched.new_task(function()
    print("emitter_task")
    while true do
        for i=1, 5 do
            sched.signal('ev', i)
            sched.sleep(1)
        end
        sched.running_task:set_pause(true)
    end
end)

这样就行了。这样emitter_task就会在无名task之后运行了

sandy1219 commented 7 years ago

如果我这么改 M.run = function ( task, ... ) local taskd if type(task)=='function' then taskd = new_task( task ) else taskd = task end new_tasks[taskd] = true; return taskd end new_tasks[taskd] = true; 这行代码从new_task api中删掉 加入到 run中

emitter_task:run() 或者 sched.run(emitter_task ) 会有问题么?

sniperHW commented 7 years ago

new_task的本意是创建一个task,在下次调用loop的时候运行,run的本意是立即执行。 但因为有bug原作者将run中立即执行的代码注释掉了。如果你这样改,new_task出来的task如果不调用run就永远不会被执行。这有背原意图。真正的问题在run中注释掉的代码。