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
Method | Params | Returns | Description |
---|---|---|---|
:launch() | Launch the task asynchronously | ||
:wait_until_exit() | Wait synchronously until the task exits. Blocks all threads until done. | ||
:set_stdin(input) | Pipe | String | |
:stdin() | Pipe | String | |
:set_stdout(output) | Pipe | String | |
:stdout() | Pipe | String | |
:set_stderr(error) | Pipe | String | |
:stderr() | Pipe | String | |
:set_work_dir(dir) | String | Set working directory. Calling after launch throws an error | |
:work_dir() | String | Get working directory | |
:set_env(env) | Table | Set environment key-values. Calling after launch throws an error | |
:env() | Table | Get environment key-values | |
:pid() | Integer | Get PID. Returns 0 before launch | |
:is_running() | Boolean | Whether task is running. Even if false, you should call :wait_until_exit() once to avoid zombie processes | |
:interrupt() | Boolean | Send SIGINT to main process | |
:terminate() | Boolean | Send SIGTERM to main process | |
:kill() | Boolean | Send SIGKILL to all processes. Called automatically when task object is GC-ed | |
:termination_status() | Integer | Get termination status. If not exited, returns nil, error | |
:termination_reason() | String | Get termination reason. If not exited, returns nil, error |
Pipe object methods
Method | Params | Returns | Description |
---|---|---|---|
:read() | String | Read ready data from pipe | |
:write(data) | String | Write data to pipe | |
:wclose() | Boolean, nil | String | |
:rclose() | Boolean, nil | String |
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 "你好世界"