Can the lua – Can I know if it runs within the other?

Is there a way to let a function know whether it is running in a coroutine?

For example, I have a send_message() function that tries to send a message three times. It takes one second between each attempt. If this function is called from a coroutine, I want send_message The () function executes coroutine.yield() as part of its wait-1 second loop. But if it is not in the coroutine, then it should do a POSIX usleep().

Is there a way to do this a little?

I should look better in the Lua reference manual: coroutine.running() The thread call returns nil, which is perfect for this situation.

The original reason for asking was that I mixed coroutine.running() with coroutine.status().

See: http://www.lua.org/manual/5.1/manual.html#pdf-coroutine.running
and: http://www.lua.org/manual/5.1/manual. html#pdf-coroutine.status

Is there a way to let a function know whether it is running in a coroutine?

For example, I have a send_message() function that tries to send a message three times. It takes one second between each attempt. If this function is called from a coroutine, I want send_message The () function executes coroutine.yield() as part of its wait-1 second loop. But if it is not in the coroutine, then it should do a POSIX usleep().

Is there a way to do this a little?

I should look better in the Lua reference manual: coroutine.running() returns nil if called by the main thread, which is Perfect.

The original reason for asking was that I mixed coroutine.running() with coroutine.status().

See: http://www. lua.org/manual/5.1/manual.html#pdf-coroutine.running
and: http://www.lua.org/manual/5.1/manual.html#pdf-coroutine.status

< /p>

Leave a Comment

Your email address will not be published.