local function f(a) local thread = coroutine.running() ngx.say("status:",coroutine.status(thread)) ngx.say("1") coroutine.yield() ngx.say("2") coroutine.yield() end local function ff(a) ngx.say("ff:",a) end local curr = coroutine.create(f) ngx.say("return type(create()):",type(curr)) ngx.say("status:",coroutine.status(curr)) ngx.say("resume 1") coroutine.resume(curr) ngx.say("status:",coroutine.status(curr)) ngx.say("resume 2") coroutine.resume(curr) --和create类似,返回函数用起来方便一些,但不返回错误码,无法检测运行时错误 local wrap_fun = coroutine.wrap(ff) ngx.say("return type(wrap()):",type(wrap_fun)) wrap_fun(3) return type(create()):thread status:suspended resume 1 status:running 1 status:suspended resume 2 2 return type(wrap()):function ff:3
co = coroutine.create(function () print("hello") end) print(co) print(coroutine.status(co)) coroutine.resume(co) print(coroutine.status(co)) function test_loop() for i=1,3 do print(coroutine.status(co)) print(i) coroutine.yield() end end co = coroutine.create(test_loop) coroutine.resume(co) coroutine.resume(co) thread: 0x67a5ef60 suspended hello dead running 1 running 2