๐Ÿงฉ Fork Guide

July 25, 2025 ยท View on GitHub

Lua API Difficulty Version

๐Ÿš€ Quick Start Guide for using Forks with LuaDoTheWorld


๐Ÿ“‹ What You'll Learn

  • โœ… How to run code in a new process (fork)
  • โœ… How to wait for a fork to finish
  • โœ… How to stop a forked process

๐Ÿ› ๏ธ Prerequisites

  • LuaDoTheWorld installed and required in your script
  • Unix-like environment (Linux, macOS) for process management

๐Ÿด Fork a Process

Run code in a new process (fork):

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local fork = dtw.newFork(function()
    print("executed inside fork")
end)

local one_second = 1000 -- milliseconds
fork.wait(one_second)

print("executed in main process")

โน๏ธ Stop a Forked Process

You can stop (kill) a fork if it is still running:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local fork = dtw.newFork(function()
    while true do
        print("executed inside fork")
    end
end)

local half_second = 500 -- milliseconds
fork.wait(half_second)
if fork.is_alive() then
    fork.kill()
end

print("executed in main process")

๐Ÿ“š Quick Reference

FunctionWhat it doesExample
dtw.newFork(func)Create a new forked processdtw.newFork(function() ... end)
fork.wait(ms)Wait for fork to finish (ms)fork.wait(1000)
fork.is_alive()Check if fork is runningfork.is_alive()
fork.kill()Stop the forked processfork.kill()

๐Ÿ†˜ Need Help?

  • ๐Ÿ“– Check the main SDK documentation
  • ๐Ÿ” Look at other example scripts in the SDK
  • ๐Ÿ› Report issues on our GitHub repository

Footer