我将如何获得目标协程中的协程名称?
How will I be able to obtain the coroutine's name in the targetted coroutine?
我想要这样的东西:
local co1 = coroutine.create(function()
local evt, _, _, nm, arg1 = event.pull("thread_msg", 2)
-- Pull a "thread_msg" event.
if(nm == coroutine_name)then
print(evt, arg1) -- Print the event name and the argument sent by "thread_msg"
end
end)
coroutine.resume(co1)
event.push("thread_msg", "co1", "") -- Sends a message to the coroutine
我需要协程的名称。 “thread_msg”事件发送到所有 运行 协程,coroutine.send 也是如此。我需要获取协程内部的协程名称。
使用 mc 版本 1.12.2 forge 的开放式计算机。
CPU 的架构是 lua 5.3。
谢谢
你可以load()
协程代码。
因为使用 load()
您可以为函数指定一个名称,该名称将存储在 debug.getinfo()
的 source
中 table.
如果 coroutine/function 回溯中发生错误,也会使用此 source
。
我给你一个Lua交互控制台的基本例子,用easy/lazycoroutine.wrap()
函数构造协程函数...
$ /usr/bin/lua
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> code={}
> run={}
> code.co=[[return coroutine.wrap(function(...)
>> local args={...} -- args is holding arguments
>> args[0]=debug.getinfo(1).source -- Name given by load(textcode,'Name')
>> print(args[0],'Going to yielding now')
>> coroutine.yield(args)
>> args=[0]=debug.getinfo(1).source -- Update args[0] here if called more coroutines in loop
>> print('Going to end:',args[0])
>> print(args[0],'Coroutine goes dead now')
>> return args
>> end)]]
> run[1]=load(code.co,'Megacoroutine')()
> run[1]()
Megacoroutine Going to yielding now
table: 0x565e2890
> run[1]()
Going to end: Megacoroutine
Megacoroutine Coroutine goes dead now
table: 0x565e2890
> run[1]()
stdin:1: cannot resume dead coroutine
stack traceback:
[C]: in field '?'
stdin:1: in main chunk
[C]: in ?
>
编辑:args 必须是本地的(更正)
我想要这样的东西:
local co1 = coroutine.create(function()
local evt, _, _, nm, arg1 = event.pull("thread_msg", 2)
-- Pull a "thread_msg" event.
if(nm == coroutine_name)then
print(evt, arg1) -- Print the event name and the argument sent by "thread_msg"
end
end)
coroutine.resume(co1)
event.push("thread_msg", "co1", "") -- Sends a message to the coroutine
我需要协程的名称。 “thread_msg”事件发送到所有 运行 协程,coroutine.send 也是如此。我需要获取协程内部的协程名称。
使用 mc 版本 1.12.2 forge 的开放式计算机。 CPU 的架构是 lua 5.3。 谢谢
你可以load()
协程代码。
因为使用 load()
您可以为函数指定一个名称,该名称将存储在 debug.getinfo()
的 source
中 table.
如果 coroutine/function 回溯中发生错误,也会使用此 source
。
我给你一个Lua交互控制台的基本例子,用easy/lazycoroutine.wrap()
函数构造协程函数...
$ /usr/bin/lua
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> code={}
> run={}
> code.co=[[return coroutine.wrap(function(...)
>> local args={...} -- args is holding arguments
>> args[0]=debug.getinfo(1).source -- Name given by load(textcode,'Name')
>> print(args[0],'Going to yielding now')
>> coroutine.yield(args)
>> args=[0]=debug.getinfo(1).source -- Update args[0] here if called more coroutines in loop
>> print('Going to end:',args[0])
>> print(args[0],'Coroutine goes dead now')
>> return args
>> end)]]
> run[1]=load(code.co,'Megacoroutine')()
> run[1]()
Megacoroutine Going to yielding now
table: 0x565e2890
> run[1]()
Going to end: Megacoroutine
Megacoroutine Coroutine goes dead now
table: 0x565e2890
> run[1]()
stdin:1: cannot resume dead coroutine
stack traceback:
[C]: in field '?'
stdin:1: in main chunk
[C]: in ?
>
编辑:args 必须是本地的(更正)