O'Reilly logo

Mastering the Nmap Scripting Engine by Paulino Calderón Pale

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Coroutines

Coroutines are a very interesting feature of Lua that allow collaborative multitasking. Keep in mind that coroutines are not regular preemptive threads. Coroutines will help you save time when you need different workers that use the same context; they consume very few resources.

Let's learn the basics of coroutines. Later in Chapter 9, Parallelism, we will go into this subject in depth.

Creating a coroutine

To create a coroutine, use the coroutine.create function. This function creates the coroutine without executing it:

local nt = coroutine.create(function()print("w00t!")
end)

Executing a coroutine

To execute a coroutine, use the coroutine.resume function:

coroutine.resume(<coroutine>)

You can also pass parameters to the coroutine function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required