lua-coroutine

两个协程,经过resume/yield函数参数(或者return语句)向对方传送数据,经过resume/yield函数返回值来从对方处获取传送数据
 
        function coroutine_func(resume1.Args)
                    yield1  return resume2.Args
                    yield2  return resume3.Args
          end
                             
           resume1() return true,yield1.Args
                resume2() return true,yield2.Args
                resume3() return true,coroutine_func.return
        
                将Resume做为一个服务A, coroutine_func做为一个服务B
                则  A传递数据(resume1.Args)给B, B获取后(由函数的参数获取),把响应数据(yield1.Args)>
                A继续传递数据给(resume2.Args)给B,B获取后(由yield1.return获取),把响应数据(yield2.A
                即,A(resume)和B(yield)都是经过参数来向对方传送数据,而经过返回值来从对方处获取传送
 
    一个异步的实现方法为 
                    function runAsyncFunc(func,...)
                        local current = coroutine.running 
                        func.callback = function() coroutine.resume(current) end
                        coroutine.yield --等待动做完成后经过回调函数来恢复执行
                    end  
 
  一个读写消费者的例子:
  
  function  producer()
    return coroutine.create(function()
        while true do 
          local data = io.read()
          coroutine.yield(data)
        end
      end)
  end 
 
  function consumer(p)
    while true do
      local status, data = coroutine.resume(p)
      print(data)
    end 
  end

  consumer(producer())异步

 

  添加 filter:函数

      function filter(source)协程

    return coroutine.create(function ()回调函数

        for x = 1, 10 doio

          local status, data = coroutine.resume(source)function

          cortine.yield('from filter' .. data)coroutine

        endyield

      end)方法

  end call

  function consumer(filter)

    while true do

      local status, data = coroutine.resume(filter)

      if not status then return end

      print (data) 

    end

     end

 

  comsumer(filter(producer()))