nu_scripts/modules/background_task
2024-02-05 06:28:48 -06:00
..
README.md Rewrite and expand background task module (#691) 2023-12-12 13:06:14 -06:00
task.nu background_task: Fix quoted commands and fix deprecation of implicit spreading and clarify docs (#752) 2024-02-05 06:28:48 -06:00

Background tasks with pueue

Makes Nushell "support" background tasks.

Prerequisite

Install pueue and make sure pueued is running and that pueue is in PATH.

Usage

You will get tab completions and suggestions when you install the module. Please check those.

To install the module, copy the task.nu to the $env.NU_LIB_DIRS directory, then do:

use task.nu

In your Nushell config under ~/.config/nushell.

Q&A

How can I pass data to a background task?

You can use environment variables, since they are inherited from the parent when spawning a process.

$env.FOO = 123

task spawn {
  echo $env.FOO
}

If you want to pass serialized data, you can do this:

let foo = { a: 1 b: 2 c: 3 }

with-env { FOO: ($foo | to json) } {
  task spawn {
    let foo = ($env.FOO | from json)

    echo $foo
  }
}

How can I reuse custom commands in a background task?

You can define these commands in a seperate module, like so:

# --- in foo.nu ---
export def bar [] { echo bar }

# --- in main.nu ---
task spawn {
  use foo.nu

  foo bar
}