Lua基础:协同程序

协同程序是通过协作来完成,在任一指定时刻只有一个协同程序在运行,协同有三个状态:挂起态、运行态、停止态。

  1. co=coroutine.create(            --coroutine协同,create创建协同程序。
  2.     function()                            --匿名函数
  3.         --print("hi")
  4.         for i=1,10 do
  5.             print("co",i)
  6.             coroutine.yield()        --挂起
  7.         end
  8.     end)
  9. print(co)
  10. print(coroutine.status(co))       --打印协同的状态
  11. coroutine.resume(co)              --运行挂起的协同程序
  12. coroutine.resume(co)
  13. print(coroutine.status(co))

管道和过滤器:

  1. function receive (prod)
  2.     local status, value = coroutine.resume(prod)
  3.     return value
  4. end
  5.  
  6. function send (x)
  7.     coroutine.yield(x)
  8. end
  9.  
  10. function producer ()
  11.     return coroutine.create(function ()
  12.         while true do
  13.             local x = io.read() -- produce new value
  14.             send(x)           --send to consumer
  15.         end
  16.     end)
  17. end
  18.  
  19. function filter (prod)
  20.     return coroutine.create(function ()
  21.         local line = 1
  22.         while true do
  23.             local x = receive(prod) -- get new value
  24.             x = string.format("%5d %s", line, x)
  25.             send(x) -- send it to consumer
  26.             line = line + 1
  27.         end
  28.     end)
  29. end
  30.  
  31. function consumer (prod)
  32.     while true do
  33.         local x = receive(prod) -- get new value
  34.         io.write(x, "\n") -- consume new value
  35.     end
  36. end
  37.  
  38. p = producer()
  39. f = filter(p)
  40. consumer(f)

用作迭代器的协同:

  1. function permgen(a,n)
  2.     if n==0 then
  3.         coroutine.yield(a)
  4.     else
  5.         for i=1,n do
  6.             a[n],a[i]=a[i],a[n]
  7.             permgen(a,n-1)
  8.             a[n],a[i]=a[i],a[n]
  9.         end
  10.     end
  11. end
  12.  
  13. function printResult(a)
  14.     for i,v in ipairs(a) do
  15.         io.write(v," ")
  16.     end
  17.     io.write("\n")
  18. end
  19.  
  20. function perm(a)
  21.     local n=table.getn(a)
  22.     local co=coroutine.create(function() permgen(a,n) end)
  23.     --local co=coroutine.wrap(function() permgen(a,n) end)  --wrap返回一个函数,可以抛出错误
  24.     return function()
  25.         local code,res=coroutine.resume(co)
  26.         return res
  27.     end
  28. end
  29.  
  30. for p in perm{"a","b","c"} do
  31.     printResult(p)
  32. end

非抢占式多线程:

  1. require "luasocket"
  2.  
  3. function download(host,file)
  4.     local c=assert(socket.connect(host,80))
  5.     local count=0
  6.     c:send("GET " ..file.. " HTTP/1.0\r\n\\r\n")
  7.     while true do
  8.         local s,status=receive
  9.         count=count+string.len(s)
  10.         if status=="closed" then break end
  11.     end
  12.     c:close()
  13.     print(file,count)
  14. end
  15.  
  16. function receive(connection)
  17.     --return connection:receive(2^10)
  18.     connection:timeout(0)  --timeout(0)对链接的任何操作都不会阻塞
  19.     local s,status=connection:receive(2^10)
  20.      if status=="timeout" then
  21.         coroutine.yield(connection)
  22.     end
  23.     return s,status
  24. end
  25.  
  26. threads={}
  27. function get(host,file)
  28.     local co=coroutine.create(
  29.         function()
  30.             download(host,file)
  31.         end
  32.     )
  33.     table.insert(threads,co)
  34. end
  35.  
  36. function dispatcher()
  37.     while true do
  38.         local n=table.getn(threads)
  39.         if n==0 then break end
  40.         for i=1,n do
  41.             local status, res=coroutine.resume(threads[i])
  42.             if not res then
  43.                 table.remove(threads,i)
  44.                 break
  45.             end
  46.         end
  47.     end
  48. end
  49.  
  50. host="www.w3c.org"
  51. get(host,"/TR/html401/html40.txt")
  52. get(host,"/TR/2002/REC-xhtml1-20020801/xhtml1.pdf")
  53. get(host,"/TR/REC-html32.html")
  54. get(host,"/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt")
  55. dispatcher()

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/88062368
今日推荐