Lua - 协程函数

Lua - 协程函数 首页 / Lua入门教程 / Lua - 协程函数

协程本质上是协作的,它允许两种或多种方法以受控方式执行。对于协程,在任何给定时间,只有一个协程运行,并且此运行的协程仅在明确要求暂停时才暂停其执行。

上面的定义可能看起来很模糊。假设有两种方法,一种是主程序方法,另一种是协程。当使用resume函数调用协程时,它开始执行,而当无涯教程调用yield函数时,它暂停执行。同样,相同的协程可以从挂起的位置继续执行另一个恢复函数调用。

协程函数

下表列出了Lua中协程的所有可用函数及其相应用途。

Sr.No.Method & Purpose
1

coroutine.create(f)

使用函数f创建一个新的协程,并返回一个"线程"类型的对象。

2

coroutine.resume(co [,val1,...])

恢复协程co,并传递参数(如果有)。它返回操作状态和可选的其他返回值。

3

coroutine.running()

返回正在运行的协程;如果在主线程中调用,则返回nil。

4

coroutine.status(co)

根据协程的状态,从运行,正常,暂停或停顿状态返回值之一。

无涯教程网

5

coroutine.wrap(f)

coroutine.create一样,coroutine.wrap函数也创建了一个协程,但是它不返回协程本身,而是返回一个函数,该函数在被调用时将恢复协程。

6

coroutine.yield(...)

暂停正在运行的协程。传递给此方法的参数充当resume函数的其他返回值。

让无涯教程看一个示例来了解协程的概念。

co=coroutine.create(function (value1,value2)
   local tempvar3=10
   print("coroutine section 1", value1, value2, tempvar3)
	
   local tempvar1=coroutine.yield(value1+1,value2+1)
   tempvar3=tempvar3 + value1
   print("coroutine section 2",tempvar1 ,tempvar2, tempvar3)
	
   local tempvar1, tempvar2= coroutine.yield(value1+value2, value1-value2)
   tempvar3=tempvar3 + value1
   print("coroutine section 3",tempvar1,tempvar2, tempvar3)
   return value2, "end"
	
end)

print("main", coroutine.resume(co, 3, 2))
print("main", coroutine.resume(co, 12,14))
print("main", coroutine.resume(co, 5, 6))
print("main", coroutine.resume(co, 10, 20))

当运行上面的程序时,将得到以下输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/lua/lua-coroutines.html

来源:LearnFk无涯教程网

coroutine section 1	3	2	10
main	true	4	3
coroutine section 2	12	nil	13
main	true	5	1
coroutine section 3	5	6	16
main	true	2	end
main	false	cannot resume dead coroutine

如前所述,使用resume函数来启动操作,并使用yield函数来停止操作。另外,您可以看到协程的resume函数接收到多个返回值。

  • 首先,无涯教程创建一个协程并将其分配给变量名co,协程将两个变量作为其参数。

  • 执行函数时,值3和2将保留在临时变量value1和value2中,直到协程结束。

  • 为了使您理解这一点,使用了一个tempvar3,该变量最初为10,并且在后续的协程调用中将其更新为13和16,因为在协程的整个执行过程中value1都保留为3。

  • 第一个coroutine.yieldresume函数返回两个值4和3,这是通过更新yield语句中的输入参数3和2获得的。

  • 关于协程的另一件事是在上面的示例中如何处理恢复调用的下一个参数;您会看到变量coroutine.yield接收到下一个调用参数,这为保留现有参数值的新操作提供了一种强大的方法。

  • 最后,一旦协程中的所有语句执行完毕,后续调用将返回false,并且"cannot  resume dead coroutine"语句作为响应。

看一个简单的协程,它在yield函数和resume函数的帮助下返回1到5之间的数字。如果不存在协程,它将创建协程,否则将恢复现有的协程。

function getNumber()
   local function getNumberHelper()
      co=coroutine.create(function ()
      coroutine.yield(1)
      coroutine.yield(2)
      coroutine.yield(3)
      coroutine.yield(4)
      coroutine.yield(5)
      end)
      return co
   end
	
   if(numberHelper) then
      status, number=coroutine.resume(numberHelper);
		
      if coroutine.status(numberHelper) == "dead" then
         numberHelper=getNumberHelper()
         status, number=coroutine.resume(numberHelper);
      end
		
      return number
   else
      numberHelper=getNumberHelper()
      status, number=coroutine.resume(numberHelper);
      return number
   end
	
end

for index=1, 10 do
   print(index, getNumber())
end

当运行上面的程序时,将得到以下输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/lua/lua-coroutines.html

来源:LearnFk无涯教程网

1	1
2	2
3	3
4	4
5	5
6	1
7	2
8	3
9	4
10	5

通常将协程与多程序语言的线程进行比较,但是需要了解,协程具有类似的线程函数,但它们一次只能执行一个,而不会同时执行。

无涯教程通过暂时保留某些信息来控制程序的执行顺序以满足需求。将全局变量与协程一起使用为协程提供了更大的灵活性。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Java性能调优实战 -〔刘超〕

DDD实战课 -〔欧创新〕

人人都能学会的编程入门课 -〔胡光〕

后端存储实战课 -〔李玥〕

小马哥讲Spring AOP编程思想 -〔小马哥〕

Python自动化办公实战课 -〔尹会生〕

打造爆款短视频 -〔周维〕

流程型组织15讲 -〔蒋伟良〕

快手 · 音视频技术入门课 -〔刘歧〕

好记忆不如烂笔头。留下您的足迹吧 :)