Skip to main content

Create a Task (sys.task)

Declaration

task = sys.task(exec_path [, args...])

Parameters

  • exec_path
    String. Executable file path.
  • args...
    String. Optional variadic arguments for the executable.

Returns

  • task
    Task object. Used to control or interact with the process.

Task object methods

MethodParamsReturnsDescription
:launch()Launch the task asynchronously
:wait_until_exit()Wait synchronously until the task exits. Blocks all threads until done.
:set_stdin(input)PipeString
:stdin()PipeString
:set_stdout(output)PipeString
:stdout()PipeString
:set_stderr(error)PipeString
:stderr()PipeString
:set_work_dir(dir)StringSet working directory. Calling after launch throws an error
:work_dir()StringGet working directory
:set_env(env)TableSet environment key-values. Calling after launch throws an error
:env()TableGet environment key-values
:pid()IntegerGet PID. Returns 0 before launch
:is_running()BooleanWhether task is running. Even if false, you should call :wait_until_exit() once to avoid zombie processes
:interrupt()BooleanSend SIGINT to main process
:terminate()BooleanSend SIGTERM to main process
:kill()BooleanSend SIGKILL to all processes. Called automatically when task object is GC-ed
:termination_status()IntegerGet termination status. If not exited, returns nil, error
:termination_reason()StringGet termination reason. If not exited, returns nil, error

Pipe object methods

MethodParamsReturnsDescription
:read()StringRead ready data from pipe
:write(data)StringWrite data to pipe
:wclose()Boolean, nilString
:rclose()Boolean, nilString

Description

Create a task and return its object.
To avoid blocking all threads, poll :is_running() in a loop; when it returns false, call :wait_until_exit() once to ensure cleanup.
Available in versions after 2025-07-05.

Examples

-- Simulate: echo "你好世界" | /usr/bin/gzip -fc | /usr/bin/gzip -dfc

local comp_tsk = sys.task(jbroot('/usr/bin/gzip'), '-fc') -- compression task
local decomp_tsk = sys.task(jbroot('/usr/bin/gzip'), '-dfc') -- decompression task

decomp_tsk:set_stdin(comp_tsk:stdout()) -- pipe stdout of comp to stdin of decomp

decomp_tsk:launch() -- launch decomp
comp_tsk:launch() -- launch comp

comp_tsk:stdin():write("你好世界") -- write data to comp stdin
comp_tsk:stdin():wclose() -- close comp stdin write end

comp_tsk:wait_until_exit() -- wait comp
decomp_tsk:wait_until_exit() -- wait decomp

nLog(decomp_tsk:stdout():read()) -- prints "你好世界"